Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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/ &
coffeepid=$!
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

2 Answers 2

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 com.example.coffee -- ${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 com.example.coffee
 launchctl start com.example.sass
 launchctl stop com.example.coffee
 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  
-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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.