Why can't launchd run my shell script?

Hi,


I am trying to use launchd to run a shell script backing up my box to an external HD. The script (backupmgr.sh) uses rdiff-backup, and it runs as expected when I launch it from Terminal. This script includes email reporting to email me results of my backup attempt.


I modified an example .plist to run this script at 0330 every day. The .plist is in ~/Library/LaunchAgents, so it will only run when I'm logged in.


My problem is that, although the script runs perfectly from the Terminal, it fails (/Users/mindlab/backup/backup.sh: line 32: rdiff-backup: command not found) when launchd runs it. I cannot figure out why! How could a command be 'not found' from launchd when it works from Terminal?


I've attached the .plist below. Any help would greatly appreciated.


<?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.mindlab.backup</string>

<key>LowPriorityIO</key>

<false/>

<key>Nice</key>

<integer>1</integer>

<key>AbandonProcessGroup</key>

<true/>

<key>OnDemand</key>

<true/>

<key>ProgramArguments</key>

<array>

<string>/Users/mindlab/backup/backupmgr.sh</string>

</array>

<key>StartCalendarInterval</key>

<dict>

<key>Hour</key>

<integer>3</integer>

<key>Minute</key>

<integer>30</integer>

</dict>

</dict>

</plist>

iMac 20", Mac OS X (10.4.9)

Posted on Aug 6, 2011 3:29 PM

Reply
3 replies

Aug 6, 2011 5:24 PM in response to edwardware

Usually you have these problems because you assume that the launchd environment is the same as yours. Generally this is NOT true. The PATH environment variable for finding commands is often different, the working directory is often different, the HOME environment variable is not always what is expected, the actual user id of the running process is not what is expected.


I suggest you add the following to the beginning of your script


( id -a

pwd

/bin/ls -dlaeO@ .

echo $#

echo $0

echo "$@"

printenv

) >/tmp/my_launchd_environment.txt


Now run your script via launchd, and then look at the /tmp/my_launchd_environment.txt file. Does it include all the environmental information that your script depends on?

Aug 6, 2011 6:24 PM in response to edwardware

You are probably relying on your current path to find rdiff-backup. Never rely on paths in scripts. Always specify the full path. Also, launchd is Apple's souped-up replacement for cron. It runs all the time whether you are logged in or not.


In any event, why reinvent the wheel? Why not use Time Machine or some other tool that already has this functionality?

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Why can't launchd run my shell script?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.