Why can't launchd run my shell script?

Discussion in 'Mac Basics and Help' started by edwardware, Aug 6, 2011.

  1. edwardware macrumors newbie

    Joined:
    Aug 6, 2011
    #1
    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.

    Code:
    <?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>
    
     
  2. Intell macrumors P6

    Intell

    Joined:
    Jan 24, 2010
    Location:
    Inside
    #2
    To run a shell script, you have to make it run is via sh first (see red). Make it like this:
    Code:
    <?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>
    		[COLOR="Red"]<string>/bin/sh</string>[/COLOR]
    		<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>
    
    
     
  3. edwardware thread starter macrumors newbie

    Joined:
    Aug 6, 2011
    #3
    Intell: I've read about needing to add the shell path, and I've tried it. I just tried it again as you've noted; it still doesn't work!

    Edit: Yes, sh is the correct shell. The script works fine from Terminal in the sh shell.

    Any other ideas?

     
  4. Intell macrumors P6

    Intell

    Joined:
    Jan 24, 2010
    Location:
    Inside
    #4
    Does your shell script have the proper permissions, 777?
     
  5. edwardware thread starter macrumors newbie

    Joined:
    Aug 6, 2011
    #5
    Yes, permissions are set to 777.

     
  6. nelz886 macrumors member

    nelz886

    Joined:
    Oct 23, 2010
    Location:
    New Jersey
    #6
    I've attached the .plist below. Any help would greatly appreciated.

    Code:
    <?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/[COLOR="red"]backupmgr.sh[/COLOR]</string>
    	</array>
    	<key>StartCalendarInterval</key>
    	<dict>
    		<key>Hour</key>
    		<integer>3</integer>
    		<key>Minute</key>
    		<integer>30</integer>
    	</dict>
    </dict>
    </plist>
    
     
  7. edwardware thread starter macrumors newbie

    Joined:
    Aug 6, 2011
    #7
    Actually backupmgr.sh manages the email reporting, and backup.sh does the backup. That part works.

     
  8. edwardware thread starter macrumors newbie

    Joined:
    Aug 6, 2011
    #8
    Solution!

    Folks: thanks for the input, etresoft and BobHarris over at discussions.apple.com solved it. My PATH included /opt/local/bin, and launchd's PATH does not. As soon as I specified the full command PATH, the script works under launchd.

    Cheers!
     

Share This Page