Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to run the CoffeeScript and the Sass compilers in the background, and be able to kill them both at once. I have a bash script start, like this:

coffee --watch -o assets/ -c src/ &
sass --watch src:assets &

When started from a script file, they don't appear in the jobs list, so I can't kill them with:

kill `jobs -p`

as suggested in this post. They do appear in the ps output as being attached to my tty.

What is a good way to kill the processes started by my start script?

My aim is to start the compilers with ./start and kill them with a ./stop script. I'm using OSX Lion.

Edit: Based on grawity's answer, now I'm using:

coffee --watch -o assets/ -c src/ &
sass --watch src:assets
kill $coffeepid

The Sass compiler complains if it is stopped using kill, so I'm stopping it with Ctrl-C then killing the background CoffeeScript compiler.

share|improve this question
FWIW, pressing Ctrl-C is equivalent to kill -INT. – grawity Sep 1 '11 at 9:28
up vote 4 down vote accepted

You're using MacOS X. The system comes with a way to run user daemon processes, starting and stopping them as desired. (The proper name for them in MacOS is user agents.) It's launchd, and the interface to it is launchctl. Do this once per login session (modifying the pathnames for your compilers into absolute form as appropriate):

 launchctl submit -l -- ${BINDIR}/coffee --watch -o assets/ -c src/
 launchctl submit -l com.example.sass -- ${BINDIR}/sass --watch src:assets

And in your start/stop scripts:

 launchctl start
 launchctl start com.example.sass
 launchctl stop
 launchctl stop com.example.sass

No horrible accident-prone pid files. No grep mismatches in the process table. No muss. No fuss.

Of course you can get creative with a property list, and adding it to your Library/LaunchAgents directory, if you like — a simple property list file is not hard to knock together. But launchctl submit is there for nonce user agents like this.

share|improve this answer
Thanks, I'd forgotten about launchd. I'll have a look at that. – Douglas Sep 1 '11 at 8:48

filter out the PID from ps and then feed it to kill (killing ping in this example):

ps -A | grep ping | awk '{print $1}' | xargs kill
share|improve this answer
-1 because of a very unreliable pipeline. – grawity Aug 31 '11 at 23:33
And another -1 because it's overly complicated and indirect. – Chris Page Sep 1 '11 at 2:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .