Tuesday, April 24, 2012

services on a Mac

Say there's a program that needs to run on your Mac all the time forever and ever. This post describes how to set that up. The example here sets up the script that runs elasticsearch.*

On a Mac, services are controlled by launchd, which is configured using launchctl. This example uses launchctl to set up a service that starts as soon as it's configured, starts up automatically at system startup, and gets restarted every time the job dies.

1) Create a configuration (plist) file for it. Name the file like: com.yourorganization.whateverpackage.jobtitle.plist. In the example, mine is org.elasticsearch.test.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN"
<plist version="1.0">

Some important bits explained:
org.elasticsearch.test - the Label is the job's name. After the job is loaded into launchctl, this is how you can refer to it for start, stop, list.
/Volumes/Projects/elasticsearch/bin/elasticsearch - the ProgramArguments contains the name of the program you want to run, and then any arguments to pass to it. (there's an alternative to put the program or script name in a separate place, but this is easy. It keeps it right next to its arguments.)
-f - this argument is specific to elasticsearch, but the meaning of it is important. Your program or script should not fork and then exit. This means it should run everything in the foreground and never exit. launchd has a job to keep your script running, so your script needs to keep running.
/Volumes/Projects/elasticsearch -  launchd will cd to the WorkingDirectory before running your program. Your script should not cd.

2) load your plist configuration:
  • copy your plist file to /Library/LaunchDaemons
  • Load it: launchctl load /Library/LaunchDaemons/org.elasticsearch.test.plist (your plist filename)
    • note that whatever user you are when you run this launchctl load will, by default, be thu user the job runs as. If you don't like that, you can configure the UserName in your plist (man launchd.plist for more info). If you sudo launchctl load, your job will run as root.
  • Check it: launchctl list org.elasticsearch.test (your job label)
    • if you just do launchctl list, it'll list all the jobs set up by your user. sudo launchctl list to see everything. Grep for yours.
    • listing the specific job gives you some status information. Look for a PID - that means your script is running.
    • if you see no PID and a LastExitStatus of 256, then your job might be forking and exiting. Don't do that.
  • Now ps -ef | grep for your program, and see that it's running.
    • Kill it. See whether it gets started again with no action on your part. It should.
    • Check your program's logs.
  • If you need your program to NOT run constantly, you'll need to unload it.
    • launchctl unload /Library/LaunchDaemons/org.elasticsearch.test.plist

Great! That's the whole thing. For way more options, check the man pages. Also, here's a poorly organized but nonetheless useful reference page.

* there is a service wrapper for elasticsearch, but it didn't work for us. It doesn't set the job up to run continually.


  1. Each professional football season unofficially begins with all the Australian Available. There are usually always a couple of smaller tourneys in early on January to start up a time of year, but only once the participants take their particular places on the first Fantastic Slam with the year can the actions really heat. www.scorespro.com

  2. The Breitling Navitimer watch is a part of the a lot of replica watches accepted styles of the anytime fashionable Breitling label, which has taken men's timepieces into a new chic of elite. Using Breitling's Navitimer is simple, but acquirements to do it accurately will advice ensure your watch retains its value. Untwist the axial bulge on the appropriate ancillary of the face, affective the punch counterclockwise and out of its defended position so that it is in the ambagious position. The http://www.brearcadiacove.com bulge will pop chargeless if you accept askance far enough. Anchor the bulge and cull until you feel it bang one cleft outward. This aboriginal cleft is the ambience for the date; aberration the bulge to move the day to the able number. Know that you cannot aberration backward; if you canyon a amount you will charge to complete addition circling and acknowledgment to the actual date if you absence it the aboriginal time. Carefully anchor the bulge and cull already added to feel it achieve into the additional and extreme position. This extreme position sets the hour and minute of your watch.Turn the bulge to move the hour duke and set the able time. Press in on the bulge adjoin the bounce until you feel it hit the base. Without removing the pressure, aberration clockwise to defended the knob. Wait to verify that your additional duke begins moving, advertence that your watch is befitting the actual time. For alarm models, the the top button will activate and stop timing, and the basal button will displace the timers. Always accumulate the paperwork that came in the watch's box in a safe location. The cartier replica consecutive amount and the affidavit will be important for repairs, resale and service. Bring your watch into the abundance for any aliment acute automated adjustments. This includes wristband alterations; models of the Navitimer were fabricated in both affiliated and solid straps. If your Navitimer has links, they will be adjustable and captivated in abode by baby pins. These pins are best removed and adapted by a jeweler or watch artisan so as not to accident the superior and amount of your Navitimer watch. Bring your alarm in for chargeless adjustments area you purchased the watch, or to addition accustomed retailer.Examine the book on the caseback. The carving on the caseback should be acutely printed with calmly authentic lettering. It should aswell be almost abysmal back these casebacks are laser-etched to burdensome standards.