Processes such as redis and mongodb have to be started manually each time after system restart on Mac and they occupy a terminal instance each. Daemonizing them will make the process to start automatically & without the need for a dedicated terminal.

There are two ways to daemonize a process in osx:

  1. Agents
  2. Daemons

Both of which are managed by Launchd process running on osx.

The main difference between Agents & Daemons (:P Dan Brown) on osx is the time when process is executed. A Daemon process is executed as soon as the os is booted up whereas Agents are executed when a user has logged on.

Behaviour of Daemon/Agent is specified in a XML file called property list (.plist). Depending on where it is stored it will be treated as a Daemon or an Agent.

Here is the demonstration of creating Mongodb as a daemon in OSX (10.9.5) via User Agent. I am making it as a User Agent because I wanted to make it specific to the user.

Setting up MongoDB:

  1. Download MongoDB

  2. Unzip & copy files:

     tar -xf mongodb-osx-x86_64-3.0.6.tgz
     cd mongodb-osx-x86_64-3.0.6
     sudo cp bin/* /usr/local/bin
  3. Create config file at ~/etc/mongo/mongod.conf. (Check out configuration options). Here is a sample config file:

         path: "/Users/username/logs/mongo/mongo.log"
         logAppend: true
         destination: file
         ipv6: true
             enabled: true
             RESTInterfaceEnabled: true
         dbPath: "/Users/username/var/mongo"
  4. Create DB folder ~/var/mongo/ and logs folder ~/logs/mongo/

Creating User Agent:

  1. Create ~/Library/LaunchAgents/mongodb.plist.

     <?xml version="1.0" encoding="UTF-8"?>
     <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
     <plist version="1.0">
  2. Load agent job:

     launchctl load ~/Library/LaunchAgents/mongo.plist
  3. To check if the daemon/agent already exists:

     launchctl list | grep "mongo"
  4. Start the agent job:

     launchctl start mongod
  5. Ensure if the user agent is running:

     ps aux | grep mongod