2

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.

1
  • FWIW, pressing Ctrl-C is equivalent to kill -INT. Sep 1, 2011 at 9:28

2 Answers 2

5

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.

1
  • Thanks, I'd forgotten about launchd. I'll have a look at that.
    – Douglas
    Sep 1, 2011 at 8:48
-2

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
2
  • 1
    -1 because of a very unreliable pipeline. Aug 31, 2011 at 23:33
  • And another -1 because it's overly complicated and indirect.
    – Chris Page
    Sep 1, 2011 at 2:15

You must log in to answer this question.

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