Bash script to run commands for specified number of seconds?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Bash script to run commands for specified number of seconds?
I want to make a very simple bash script to run a command for a user-specified number of seconds and then kill it. The purpose is to limit the amount of time the program runs. Example in pusedocode:
Code:
#!/bin/bash
#$1 is the user input number of seconds
for $1 seconds
do
ARBITRARY COMMAND HERE
after $1 seconds has passed
kill command
exit
I've been looking all over the net and cannot find a simple answer to this problem. Please help. Thanks!
You would need to run the command in background, so that the next part of the script continues.
Example (not tested):
Code:
#!/bin/bash
###
### Run command for given number of seconds then kill it
###
read -p "How long should I run for? ==> " count_secs
if [ $count_secs -gt 0 ]
then
###
### number of seconds greater than zero
###
/my/command &
###
### assume that the PID of the command is $$
###
my_PID=$$
sleep $count_secs
kill -15 $my_PID
fi
You probably want "$!", which is expanded to the PID of the latest command that has been run asynchronously (background), in this case "/my/command".
"$$" holds the PID of the shell, and killing it might not be a good idea if you want to do some other task before exiting the script. It will not make a (noticeable) difference for the sample code you are using though.
Running the program in the background is going to be a problem. Is there some way to spawn it in a new terminal?
The programs I'm running are strictly command line based and I need to watch the output while it's running. No GUI programs will be executed with this script.
Okay, this is what I've tried so far and I'm not sure yet if it's working properly since I'm not at my desktop to try it, but it does appear to work.
Code:
#!/bin/bash
###
### Run command for given number of seconds then kill it
###
read -p "How long should I run for? ==> " count_secs
if [ $count_secs -gt 0 ]
then
###
### number of seconds greater than zero
###
rec -r 22050 -c 1 -p | sox -p output.mp3 silence 1 0.1 1% -1 0.5 1% &
###
### assume that the PID of the command is $$
###
my_PID=$!
sleep $count_secs
kill -15 $my_PID
fi
I found that I did need to use $! for the pid otherwise sox would just keep running forever and Ctrl-C wouldn't even stop it, only a kill -9 from another terminal would stop it. Do I have to do anything special since I'm using a lot of CLI switches and a pipe?
If it works it works.
Personally I'd
1. update that comment $$ => $! (not many people know $!)
Uh?! It's exactly on the same place that "$$", the bash man page. What makes it easier to know about "$$" than "$!"? *puzzled*
If it works, it works, yeah, until you decide to add some more code at the end of the script and you find you asking yourself why it doesn't ever get executed...
ps. We could as well use the same argument here though, and say that "not many people know" about the slight difference between [ and [[, which doesn't make the excuse any better
As Disillusionist pointed out, most people learn the Special Vars like $$ in an ad-hoc fashion (monkey see, monkey do). Few read (& remember!) the relevant man page contents.
Also, ime I've seen $$ used a lot in scripts, $! very rarely...
YMMV
PS
As for [[ ]], that was taught to me on a (ksh) course quite early in my *nix career. I've always used them since.
So if it's not famous we should never teach it even if the script is slightly incorrect otherwise.... well, I'll remember that... but will never agree with it and will never follow that rule. I've already explained why that can be problematic above, if you want to learn it it's ok, if not let's forget about the issue. I am sure the OP if it's still hearing can take his/her own decission
Ugh; my orig point, way back when (post #8), was that the OP had correctly changed the code to use $! instead of $$, but had not updated the comment to match, which would cause confusion later as well as being wrong in it's own right.
I certainly wasn't trying to start an argument with anyone, not sure how I got interpreted that way.
I just had a throwaway comment that IME, more people know the answer to 'what's the current process pid var' => $$; than the qn 'what's the variable to hold the last backgrounded proc' => $!.
As I said YMMV....
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.