Last updated on Thu, 2011-01-20 14:16. Originally submitted by fabio on 2011-01-10 17:50.
I needed to execute a given command for a specified number of seconds on my Linux system. I wasn't able to find a program for doing that so I ended up writing this little and simple Bash script:
#!/bin/bash
cmd=$1
seconds=$2
echo "Executing: ${cmd} for $seconds seconds"
$cmd&
cmdpid=$!
sleep $seconds
if [ -d /proc/$cmdpid ]
then
echo "terminating program PID:$cmdpid"
kill $cmdpid
fi
If you save this into a file called run_seconds_then_exits.sh you use it with:
bash run_seconds_then_exits.sh "sleep 10" 5
The above will run the command sleep 10 for 5 seconds, then it will send a termination signal (SIGTERM) and the program will be terminated.
See any improvements to the script above? Please leave a comment below and let me know about it!
Recent comments
21 hours 27 min ago
1 day 13 hours ago
1 day 15 hours ago
1 day 15 hours ago
1 day 16 hours ago
1 day 19 hours ago
2 days 1 hour ago
2 days 3 hours ago
2 days 9 hours ago
2 days 9 hours ago