Removing items from launchctl

My OSX work machine was running the fans constantly, overheating, and being a brat in general.

When anything like this happens in OSX, I run Console to see if there any errors being logged. Upon doing this, I instantly saw that postgresql was trying to launch every ten seconds, and was failing. Here are the approximate error messages; if this is happening to you, your errors will most likely be slightly different:

12/7/14 5:45:22.555 PM com.apple.launchd.peruser.501[237]: (homebrew.mxcl.postgresql) Throttling respawn: Will start in 10 seconds
12/7/14 5:45:32.572 PM com.apple.launchd.peruser.501[237]: (homebrew.mxcl.postgresql[88282]) Exited with code: 1

So, what happened? I’m not 100% certain, but I think I forgot to shut down my local rails development server before closing my terminal. No big deal – here’s how to fix it:

The part of the error that reads “com.apple.launchd.peruser.501[237]” suggests to me that launchd is controlling my machine’s postgres process(es). To verify this, let’s start up a terminal (I use iTerm2) and enter:

launchctl list

You should see a long list of results – but what is going on and what is this launchd thing you’re telling me about? [launchd wikipedia page] Regardless of what operating system you’re using, computers nowadays come with a system to manage all the invisible nitty gritty behind the scenes things associated with starting up the OS or application. Many *nix boxes use init or systemd (a hotly debated issue) – OSX uses launchd. To help users manage launchd services, launchd developers included a program called launchctl. The launchctl list command will send our terminal a list of all the running services that launchd manages. But we can do a little better. This list can be quite long, so let’s do a simple search of that list output, limiting it to lines including postgres:

launchctl list | grep postgresql

This returns:

- 1 homebrew.mxcl.postgresql

I copied the directive (homebrew.mxcl.postgresql) into the launchctl remove command:

launchctl remove homebrew.mxcl.postgresql
Et voilà, one problem fixed. I’ll eventually write up how to make this a more permanent solution, but for the time being, you can just add it to an alias in your .bash_profile like so:
alias pghb='launchctl remove homebrew.mxcl.postgresql';
 The alias name isn’t really important but I try to prefix with the name of the target process/binary/etc., and then what I’m doing with it.
 

2 thoughts on “Removing items from launchctl

  1. A bit off topic, but would you happen to know how `launchctl remove` differs from `launchctl stop` and `launchctl unload`?

  2. Sure do:
    Where `launchctl remove` removes an agent from the launchd plist file used for configuration,
    `launchctl stop` simply stops the service from running, temporarily, through launchd.
    `launchctl unload` unloads the plist file entirely.

    My own off topic note:
    It looks like homebrew includes a tool to do some of these tasks internally. I believe it still uses the same plist file/s. You can read about that and more great dev/devops/sysadmin stuff at http://robots.thoughtbot.com/starting-and-stopping-background-services-with-homebrew
    I have yet to try this approach, but I’ll probably write a followup and reference this post when I do.

    I hope that helps, and thanks for asking, Steve.

Leave a Reply