Setting up daemons on Mac OS X

I've recently given in to temptation and bought a new MacBook - I've upgraded the RAM, got AppleCare etc. and am really enjoying myself! I've been setting up my development environment - compiling my own Apache (built in one is terrible, or so I've heard), PHP and getting everything set up the way I want to.

As I'm not using the default Apache, I wanted to know how I setup Apache, and other applications as daemons; running in the background, much like Windows' services.

I was wondering if any of you guys have any experience with this sort of stuff, and if you could give me a hand.


Solution 1:

OSX uses launchd to manage all daemon starting and stopping. If you google for "launchd" the first result is apple's article on how to use it (I would have just linked, but stackoverflow wont let me)

For what you're trying to do, I suggest you simply edit the already existing apache launchd file to point to your own binary. Then you can control it using the Sharing preference pane. The file is at /System/Library/LaunchDaemons/org.apache.httpd.plist

Solution 2:

You need to use launchd, Apple's tool for managing all system processes. Launchd is a unified replacement for a whole slew of specialized unix utilities which managed different pieces of this (init, rc, etc, etc).

Apple covers some of this on their website, but they have a bunch of out-of-date documents mixed in which describe obsolete techniques. Dan Benjamin has a nicely written tutorial on how to install MySQL. This is a very well written guide which will teach you the general steps of installing unix/linux software on the Mac (without relying on MacPorts or Fink). Dan's MySQL tutorial goes through the creation of a launchd config file.

Basically, you'll create an xml configuration file (a plist file) which specifies the working directory, which user to run as, etc. You'll put that file in a particular directory and then tell launchd to load it, using launchctl.

He also has tutorials for setting up Ruby on Rails, which you might find useful, but I don't think that involves any daemons.

Solution 3:

You'll want to look into using launchd. Look up that on Apple's website.