The following code kills off a backgrounded process in a bash shell after 4 seconds:
Code:
#!/bin/sh
dd if=/dev/zero of=/dev/null &
sleep 4
kill $!
I also want to write additional scripts that background many processes at the same time, and selectively kill different backgrounded processes/jobs selectively (based on some arbitrary criteria that I make in my script).
How can I do this?
The 'kill %N' construct as per
http://www.tldp.org/LDP/abs/html/x6659.html#JOBIDTABLE does not appear to function inside the script.
Code:
mengland@matts-laptop 12:33am [/data/tmp] 25> cat ks2.sh
#!/bin/sh
dd if=/dev/zero of=/dev/null &
sleep 1
kill %1
mengland@matts-laptop 12:33am [/data/tmp] 26> ./ks2.sh
kill: illegal pid: %1
mengland@matts-laptop 12:33am [/data/tmp] 27> ps -ef | grep dd
mengland 3316 1 0 00:33:35 /usr/bin/dd
mengland@matts-laptop 12:33am [/data/tmp] 28> killall dd
mengland@matts-laptop 12:33am [/data/tmp] 29> !ps
ps -ef | grep dd
mengland@matts-laptop 12:33am [/data/tmp] 30>
Notice the:
Code:
kill: illegal pid: %1
Why does that not work? If it could, I could (seemingly) selectively kill processes I put in background via the bash script.
Any thoughts?
fyi, the same thing happens for a plain 'kill %' in the script.
-Matt