"hello world" launchd plist not working

Luke Griffiths picture Luke Griffiths · May 20, 2012 · Viewed 8.4k times · Source

I have the following plist file at ~/Library/LaunchAgents:

<?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.yogapo.test_launchd</string>

    <key>Program</key>
    <string>. /Users/luke/dev/data_yogapo/script/test_launchd.sh</string>

    <key>StartInterval</key>
    <integer>10</integer>

    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>

And the test_launchd.sh file is contains the following:

#! /bin/bash 

echo "hello world from test_launchd.sh" >> /Users/luke/dev/data_yogapo/log/development.log

When I run test_launchd.sh manually with
. /Users/luke/dev/data_yogapo/script/test_launchd.sh the results are as expected: the line appears at the end of development.log

But when I load up this plist file, nothing happens:

$ cd ~/Library/LaunchAgents
$ launchctl load com.yogapo.test_launchd.plist
$ launchctl list | grep yogapo
  -       1       com.yogapo.test_launchd

I've tried this with and without the RunAtLoad key. I've looked at other answers here on SO as well as elsewhere on the internet. I've followed tutorials, and there's simply nothing happening. Any help much appreciated - thanks!

Answer

David J. Liszewski picture David J. Liszewski · May 23, 2012

You are asking launchd to run progam called

". /Users/luke/dev/data_yogapo/script/test_launchd.sh"

It will take the entire value of the Program key as the first argument to execvp(see man execvp(3) for more details)

If you were to examine the system log, you would see something like:

May 22 21:17:38 dented com.apple.launchd.peruser.501[202] (com.yogapo.test_launchd[32986]): posix_spawn(". /Users/luke/dev/data_yogapo/script/test_launchd.sh", ...): No such file or directory May 22 21:17:38 dented com.apple.launchd.peruser.501[202] (com.yogapo.test_launchd[32986]): Exited with exit code: 1

launchd is not a shell. However, it can interpret hash-bang character sequences to identify which program to use to interpret your script. Therefore, simply specify your script as the program to run:

<key>Program</key>
<string>/Users/luke/dev/data_yogapo/script/test_launchd.sh</string>

Note: Should you need to pass arguments to your script, use the ProgramArguments key instead and put the whole command line there. For example:

<key>ProgramArguments</key>
<string>/Users/luke/dev/data_yogapo/script/test_launchd.sh arg1 arg2 arg3</string>