[Cialug] Starting two jobs at once
Zachary Kotlarek
zach at kotlarek.com
Tue Nov 27 16:22:34 CST 2012
On Nov 27, 2012, at 2:08 PM, Matthew Nuzum <newz at bearfruit.org> wrote:
> The prob is I'm not sure really how to kill a background task once it's in the background
Grab the PID and kill it by number.
You can do that pretty easily with a shell script like:
echo $$ > /var/tmp/myScript/pid.file
exec 'actual daemon'
That will echo the PID of the shell to the specified file, then replace the shell (keeping the same PID) with the daemon launched via the exec command.
You can then launch that shell entire script in the background (with &) and later go read the PID file to find the process number you want to kill.
--
To make things die when you press ctrl-c you'll want to install a signal handler for SIGINT and SIGTERM. In bash that would look like this:
function quit {
kill `cat /var/tmp/myScript/pid.file`
exit 0
}
trap "quit" SIGINT SIGTERM
--
Zach
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2746 bytes
Desc: not available
URL: <http://cialug.org/pipermail/cialug/attachments/20121127/e17a5c47/attachment-0001.bin>
More information about the Cialug
mailing list