6

On OSX using launchctl to start and stop a service seems great however I don't see anyway to check the return value to make sure it really did start. Return value always seems to come back as zero even when the service failed to start

This plist file contains an error and thus will not start Drizzle as expected

% launchctl start /Library/LaunchDaemons/org.drizzlebad.plist

% echo $?

0

Any suggestions other than checking the existence of the pid file for this service?

Matthew Murdoch
  • 30,874
  • 30
  • 96
  • 127

3 Answers3

10

You must be doing something wrong, because launchctl only returns 0 on success:

$ launchctl start foobar
launchctl start error: No such process
$ echo $?
1

Also, launchctl start takes the job title (generally a reverse-DNS-style name), not the path to a plist. It's launchctl load and unload that take a plist path.

If you insist that it doesn't work for you, you could try just grepping the output of launchctl list for your job name and, if necessary, parsing the fields.

Jeremy W. Sherman
  • 35,901
  • 5
  • 77
  • 111
1

when you do a launchctl list from the terminal, you'll notice - in the second column... there is a list of mostly 0's and -'s... if your program exited incorrectly, it will show the exit code in that second column.... usually a -1 or sometimes a -251, or whatever the case may be... and remember... just cause your launchd item might have crashed... launchctl may still feverishly be trying to "make it work" - every 10 seconds..... so don't let a borked launchd process linger forever, as it definitely impacts performance..

unload it with a -w.... and the full path to the plist, or at least stop (and optionally remove it) with the bundle identifier. -w will make a clandestine entry in a plist in your /var/db/launchd folder, telling the kernel not to load it next time the machine boots.. irrespective of what the "enabled" XML entry may say in the Plist.... so beware that there is more than just that flag at play as to whether the item is resurrected the next time you startup...

if you want you can back up that overrides.plist file deep in tat var folder and start over if it has gotten too convoluted with loads and unloads over time... there's other ways to do it, but i'm going to shut up now.. before I incidentally instruct you how to completely hose your computer...

Alex Gray
  • 16,007
  • 9
  • 96
  • 118
0

For posterity. I seem to get a return of zero on load/unload (not start/stop), when there is an "Input/output error".

$ launchctl load foobar
Load failed: 5: Input/output error
Try running `launchctl bootstrap` as root for richer errors.
$ echo $?
0

This leaves me grep'ing for the error

$ launchctl load foobar 2>&1 | grep 'Load failed'
PLG
  • 448
  • 5
  • 21