0

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

Joel G
  • 67
  • 1
  • 7
  • Excecute `{ set ; export ; } | sort > cmdLineEnv` and `{ set ; export ; } | sort> startupEnv` per environment, then `diff *Env` ? Good luck. – shellter Aug 21 '13 at 18:19
  • looks like a similar plist to mine, but I put it in LaunchAgents not LaunchDaemons.. not sure what the difference. – Justin Meiners Aug 21 '13 at 18:20
  • @shellter The only difference is in PIPESTATUS. In startupEnv, PIPESTATUS=([0]="0" [1]="0"); in cmdLineEnv, PIPESTATUS=([0]="0"). I have no idea what that even means... – Joel G Aug 21 '13 at 18:24
  • @JustinMeiners When I put my plist in LaunchAgents, the script runs. However, it only runs on login, not on startup. I want the script to execute on startup, regardless of whether the user logs in. – Joel G Aug 21 '13 at 18:25
  • This is hard to debug without seeing the actual error. Specify `StandardErrorPath` and `StandardOutPath`. I'm sure the solution will be right there. – LCC Aug 22 '13 at 01:38

2 Answers2

0

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.

Community
  • 1
  • 1
Rob Napier
  • 286,113
  • 34
  • 456
  • 610
  • The mysql dump is being executed on the remote server. I suspect the problem may be something to do with my user's ssh key not being used, if the environment is root, but I'm not sure of the exact syntax to test this hypothesis. – Joel G Aug 21 '13 at 18:35
  • I see what you mean. LaunchDaemons run in a somewhat funny state, not tied to a GUI, but they are `root` and should be able to read `~root/.ssh` Do you have any errors in Console? – Rob Napier Aug 21 '13 at 18:38
0

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.

Lri
  • 26,768
  • 8
  • 84
  • 82