Friday, August 9, 2013

linux: demonizing a process and killing it when you want

This is very useful for processes which do not write an PID file. This is extreme stupidness... but it could be work-arounded.

For example I want to start ant kill later the jasmine server, which is started this way:
bundle exec rake jasmine:server --trace

The whole batch script will be:
[ -f tmp/jasmine.pid ] && kill -9 `cat tmp/jasmine.pid` && rm tmp/jasmine.pid
bundle exec rake jasmine:server --trace &
echo $! > tmp/jasmine.pid
sleep 20
bundle exec rake jasmine:run
kill -9 `cat tmp/jasmine.pid`

rm tmp/jasmine.pid

My strings:
tmp/jasmine.pid - my pid file
bundle exec rake jasmine:server --trace - command to be deamonized
sleep 20 - this ensures time for the server to start
bundle exec rake jasmine:run - my task to be executed on the demonised process

Other information:
[-f file] && command - executes command if file exists
"echo $!" prints the pid of the last command in current console.

No comments:

Post a Comment