Pages

Saturday, August 31, 2013

Service management utility for Mac OSX (launchctl helper)

Having dealt with services mostly on Linux, I grew accustomed to type service php restart. On Mac, this is more like:

launchctl unload ~/Library/LaunchAgents/homebrew-php.josegonzalez.php55.plist
launchctl load ~/Library/LaunchAgents/homebrew-php.josegonzalez.php55.plist


Which is ugly, hard to remember and launchctl has no way of listing all available services. Plus, those plist can reside in all those directories:
  • /System/Library/LaunchDaemons
  • /System/Library/LaunchAgent
  • /Library/LaunchDaemons
  • /Library/LaunchAgents
  • ~/Library/LaunchAgents
Those in you home directory generally don’t need sudo, while the others do.

This is why I can up with an utility to manage services. It searches in all directories above for your service, prompts for sudo if it is in a system directory and provide goodies like restart, reload and link.

Usage:

service selfupdate
update from the Gist
service php
searches for a plist containing 'php'
service php load|unload|reload
insert or remove a plist from launchctl
service php start|stop|restart
manage a daemon, but leave it in launchctl  (does not work with Agents)
service php link
If you use Homebrew, which you should, it will link the plist of this Formula into ~/Library/LaunchAgents, reloading if needed. Very useful when upgrading.


Manage all optional services at once

If you have several services running, especially if you are a developer, I also recommend to use a script to start/stop all of them at once when you are not working. They may not be using much resources, but having them running keeps the laptop working and can drain you battery very quickly.