2

im very new to OSX shell programming (so be gentle) however i am a computer science uni graduate (albeit 15 years ago!) so programming around unix systems is nothing new to me.

I have been stuck on why my plist in /Library/LaunchDaemons/ is not running. I have spent half the day chasing down loose ends that have not gone anywhere hence me posting here to you fine folk.

My very simple plist is below. Please note that "MyApp" is just an apple script that (at the moment) just terminates and reloads a specific application via the shell command "killall". This part works just fine (ie when i double click it in finder, the desired app does indeed terminate and reload, and also from within the applescript editor when i click "compile" and "run"). FYI i have also set this applescript to now write a time/datestamp to a log file every time it is called, to make it clear to me when it is invoked....

.... and it never is automatically!
the only time i can get it to run is when i:
a) double click on MyApp in finder.
b) "run" it from within the applescript editor
c) command prompt: 'launchctl start MyApp'
I confirm that the script does run by any of the above by 'tail -f' on my log file, and a close eye on console output.....

HOWEVER, what i really want is:
d) for it to run automatically at boot time, and then invoked once every subsequent hour.

heres my plist 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>MyApp</string>
    <key>ProgramArguments</key>
    <array>
        <string>/Applications/MyApp.app/Contents/MacOS/applet</string>
    </array>
    <key>StartInterval</key>
    <integer>3600</integer>
    <key>WorkingDirectory</key>
    <string>/Applications/MyApp.app/Contents/MacOS/</string>
</dict>
</plist>

and heres the directory permissions

iMac:LaunchDaemons keiran_harris$ ls -las
total 24
 0 drwxr-xr-x   5 root  wheel   170 25 Jan 19:38 .
 0 drwxr-xr-x+ 64 root  wheel  2176 24 Jan 12:19 ..
16 -r--r--r--@  1 root  wheel   732 26 Jan 13:32 MyApp.plist

has anybody got any ideas on what the heck i am doing wrong?
i would be oh so grateful.
thanks in advance gurus!
Keiran.

PS> have scrutinised other similar problems in this forum, to no avail:
VERY simple Launchd plist not running my script
launchd file runs manually but not automatically
My mac osx launched plist won't run

Community
  • 1
  • 1

1 Answers1

1

AppleScripts can only run as part of an Aqua (GUI) session, while LaunchDaemons run in system context, independently of GUI sessions. You'll either need to rewrite the script using some other (non-GUI-dependent) scripting technology (shell scripting would work), or use a LaunchAgent (which normally run inside user GUI sessions) instead of a Daemon. See the Execution Context Summary section of Technical Note TN2083: Daemons and Agents -- Bootstrap Namespace is the relevant column.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
  • thanks Gordon that certainly gives me something to work with! Although, i thought if i could invoke the same plist file from the shell with 'launchctl start MyApp' (which interacts with the same launchd process, just this time its user triggered), then it should work from the LaunchDaemons folder also? – keiran_harris Jan 26 '14 at 05:31
  • @keiran_harris: `launchctl start` does not interact with the same launchd proces -- it talks to your user instance of launchd (the one managing LaunchAgents in your GUI session). If you want to manage the system instance of launchd (the one that manages LaunchDaemons), use `sudo launchd` instead. – Gordon Davisson Jan 26 '14 at 06:15
  • Gordon mate, thanks, you were spot on. As soon as i moved the .plist to /Library/LaunchAgents/ it all sprang to life. Thank you so much. This has been a great learning process for me, and ive enjoyed (this admittedly little bit) of coding again (my career branched in the network engineering direction). – keiran_harris Jan 27 '14 at 03:10