Need help using Homebrew 'Services' command

I installed MongoDB using homebrew recently. A few articles on the web advise to start mongo using the following command,

brew services start mongo

But when I use this command, I get the following message,

Error: Unknown command: services

I tried searching about this problem online. But no luck so far. What could be the problem here? I installed homebrew as it was specified on their website. Why is this feature not working for me?

And the man page has no information regarding the 'services' command.


Solution 1:

services was a "hidden" command in Homebrew. There were a bunch of them that weren't present in the brew help output. It has, as undocumented commands are wont to do, gone away in the official repository and has become an "external command" maintained in another add-on repository (in this case a gist as it is so simple).

You can install it in your Homebrew setup by running:

> curl -o /usr/local/bin/brew-services.rb https://gist.githubusercontent.com/lwe/766293/raw/75a7907004bbff0eb3b072d1d951be2cfe7e5020/brew-services.rb
> chmod +x /usr/local/bin/brew-services.rb
> brew services help
usage: [sudo] brew services [--help] <command> [<formula>]

Small wrapper around `launchctl` for supported formulas, commands available:
   cleanup Get rid of stale services and unused plists
   list    List all services managed by `brew services`
   restart Gracefully restart selected service
   start   Start selected service
   stop    Stop selected service

Options, sudo and paths:

  sudo   When run as root, operates on /Library/LaunchDaemons (run at boot!)
  Run at boot:  /Library/LaunchDaemons
  Run at login: /Users/ian/Library/LaunchAgents

Alternatively you can skip services and just make a plist file for it. For example, create ~/Library/LaunchAgents/org.mongodb.mongod.plist with:

<?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>org.mongodb.mongod</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/local/Cellar/mongodb/2.6.4/bin/mongod</string>
    <string>run</string>
    <string>--config</string>
    <string>/usr/local/Cellar/mongodb/2.6.4/mongod.conf</string>
  </array>
  <key>RunAtLoad</key>
  <true/>
  <key>KeepAlive</key>
  <false/>
  <key>UserName</key>
  <string>{your_username}</string>
  <key>WorkingDirectory</key>
  <string>/usr/local</string>
  <key>StandardErrorPath</key>
  <string>/usr/local/var/log/mongodb/output.log</string>
  <key>StandardOutPath</key>
  <string>/usr/local/var/log/mongodb/output.log</string>
</dict>
</plist>

Just change {your_username} to your actual username then run:

launchctl load ~/Library/LaunchAgents/org.mongodb.mongod.plist 

to register the plist with launchd. You can now start and stop MongoDB with:

launchctl start org.mongodb.mongod
launchctl stop org.mongodb.mongod

Note, the above plist solution was taken from this excellent Stack Overflow answer.

Solution 2:

It is external now:

brew tap homebrew/services

brew services install, brew services install now work.