0x12d3
03-04-2003, 04:45 AM
does anyone know of a bash one liner to run a command and then kill it after some duration? For the example the job is dd if=/dev/zero of=/dev/null aliased to ddzn. I've tried many variations of the following:
for i in 5; do ddzn & sleep $i; exit; done
while [ ddzn ] ; do sleep 5; done
while [ $(ddzn) & ]; do sleep 5; done
ddzn & sleep 5; kill %1 <===this works from command line but not within a script unless run as source scriptname and even then it breaks when put in the background (ie source scriptname &) defining a function within the script also fails.
Tons of other craziness. It seems the command can't be called as the conditional as it needs an exit status before the loop begins. It can't be called within the while because if it is run in the background it doesn't exit when the script ends, and if it's called without the background operator (&) then it never exits for the next command to run. As for the last example it seems that the "%1" argument lacks sufficient scope. I'm looking for a nearly shell only implementation (std sys progs are ok...ie sleep, kill, etc). Any info on bash "exporting" or otherwise handling of pid's will be greatly appreciated. Any suggestions?
for i in 5; do ddzn & sleep $i; exit; done
while [ ddzn ] ; do sleep 5; done
while [ $(ddzn) & ]; do sleep 5; done
ddzn & sleep 5; kill %1 <===this works from command line but not within a script unless run as source scriptname and even then it breaks when put in the background (ie source scriptname &) defining a function within the script also fails.
Tons of other craziness. It seems the command can't be called as the conditional as it needs an exit status before the loop begins. It can't be called within the while because if it is run in the background it doesn't exit when the script ends, and if it's called without the background operator (&) then it never exits for the next command to run. As for the last example it seems that the "%1" argument lacks sufficient scope. I'm looking for a nearly shell only implementation (std sys progs are ok...ie sleep, kill, etc). Any info on bash "exporting" or otherwise handling of pid's will be greatly appreciated. Any suggestions?