Mac OSX LaunchDaemon on Startup, Shell Script with SSH

2.2k views Asked by At

I'm hoping someone can help me with this. I've been working on it literally all day...

I want a LaunchDaemon to execute a shell script at startup. Here is my plist file, located at /Library/LaunchDaemons/com.mhi.backup.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.mhi.backup</string>
  <key>UserName</key>
  <string>Joel</string>
  <key>GroupName</key>
  <string>Admin</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/mhi_websites_backup.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

It executes correctly when I load it from the terminal (launchctl load /Library/LaunchDaemons/com.mhi.backup.plist), but not on startup.

Here is my script, for reference:

#!/bin/bash
sleep 15 #delay script to ensure time for network connection
ssh user@hostname << HERE
  mysqldump -u <user_name> -pPASSWORD --all-databases | lzma > alldatabases.sql.lzma 
  tar cfa backup-`date '+M%mD%dY%y'`.tar.lzma webapps alldatabases.sql.lzma 
  exit
HERE
scp user@hostname:backup-`date '+M%mD%dY%y'`.tar.lzma /Users/Joel/Desktop

Could someone please help?

Thanks so much,

JG

2

There are 2 answers

0
Lri On

Is the plist owned by root? If a plist in /Library/Launch{Agents,Daemons}/ is not owned by root, it can be loaded with launchctl without sudo, but it is not loaded at login.

You could also try moving the plist to /Library/LaunchAgents/ and adding a LimitLoadToSessionType key:

<key>LimitLoadToSessionType</key>
<array>
  <string>LoginWindow</string>
  <string>Aqua</string>
</array>

See the Daemons and Agents tech note.

2
Rob Napier On

What errors are you seeing? I would expect that you may have a PATH problem here. Where is mysqldump? If it's in /usr/local/bin, then you probably want to make that explicit, or set the default path in /etc/launchd.conf.