LaunchdAutostart
Starting up a Daemon/Agent on OSX boot/login
This simple recipe, will give some clues on how to have your favorite app/daemon/software automatically started with your OSX session or boot.
OSX uses Launchd to manage daemons. You can define new jobs that can:
- run as a user only for a given user
- run as a user a system wide available daemon/app
- run as root a system wide daemon/app
This current recipe works for a daemon that will be owned by the user logged in, and made available to all users.
Property List
The job is described as a property list. A very simple one that calls a Python script would be:
<?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>xx.mydomain.mydaemon</string>
<key>ProgramArguments</key>
<array>
<string>/path/to/my.py</string>
</array>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Pay attention to the following parameters:
- <string>xx.mydomain.mydaemon</string>: unique name of your job
- <string>/path/to/my.py</string>: points to your daemon/software (make it available system wide, ie /Applications or /usr/bin)
- <key>RunAtLoad</key>: autostart when users login
install your job
- Copy the above in a file called xx.mydomain.mydaemon.plist and save it under /Library/LaunchAgents
- Reboot
- If my.py is a daemon (or simply running in loop) it should be visible in the process list and owned by your user. You can check that with the following command:
ps aux | grep my.py
Troubeshooting
You can always manually start/stop/control your job using the command-line launchd application, just open a terminal and type launchctl, then help.