LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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

Reply
 
Search this Thread
Old 12-02-2009, 02:43 AM   #1
devmoc
Member
 
Registered: Sep 2005
Posts: 38

Rep: Reputation: 15
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!
 
Old 12-02-2009, 03:09 AM   #2
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
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
 
Old 12-02-2009, 05:29 AM   #3
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
Code:
   my_PID=$$
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.
 
Old 12-02-2009, 11:02 AM   #4
devmoc
Member
 
Registered: Sep 2005
Posts: 38

Original Poster
Rep: Reputation: 15
Thanks for the replies.

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.
 
Old 12-02-2009, 11:12 AM   #5
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
No problem. Just run "xterm -e foo" instead of just "foo".
 
Old 12-02-2009, 07:29 PM   #6
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
By default a backgrounded proc will still write to the original terminal, unless you re-direct it.
 
Old 12-03-2009, 10:40 AM   #7
devmoc
Member
 
Registered: Sep 2005
Posts: 38

Original Poster
Rep: Reputation: 15
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?
 
Old 12-03-2009, 07:01 PM   #8
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
If it works it works.
Personally I'd
1. update that comment $$ => $! (not many people know $!)
2. add error checking if this is for work

if [[ $? -ne 0 ]]

blah blah

3. use [[ ]] instead of [ ], more robust http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
 
Old 12-04-2009, 01:12 AM   #9
devmoc
Member
 
Registered: Sep 2005
Posts: 38

Original Poster
Rep: Reputation: 15
I tested it tonight, it works great. Thanks to all for the help.
 
Old 12-04-2009, 08:25 AM   #10
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
Quote:
Originally Posted by chrism01 View Post
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...

Quote:
2. add error checking if this is for work

if [[ $? -ne 0 ]]

blah blah

3. use [[ ]] instead of [ ], more robust http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
True.

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

Last edited by i92guboj; 12-04-2009 at 08:30 AM.
 
Old 12-04-2009, 10:48 AM   #11
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
How many people take the time to truely read the man page?

Most people learn in an adhoc fashion picking up information, one piece at a time (I know I do!)
 
Old 12-06-2009, 07:54 PM   #12
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
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.

Last edited by chrism01; 12-06-2009 at 07:55 PM.
 
Old 12-07-2009, 03:57 AM   #13
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
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
 
Old 12-07-2009, 11:58 AM   #14
Disillusionist
Senior Member
 
Registered: Aug 2004
Location: England
Distribution: Ubuntu
Posts: 1,013

Rep: Reputation: 83
Quote:
Originally Posted by i92guboj View Post
So if it's not famous we should never teach it even if the script is slightly incorrect otherwise....
I'm sure that wasn't Chris' point. Just as I'm sure he wasn't calling me "Fik"
 
Old 12-07-2009, 06:19 PM   #15
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,225

Rep: Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021Reputation: 2021
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....
 
  


Reply

Tags
bash, input, kill, limit, script, time


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
bash script to run commands dormant Programming 1 08-25-2009 01:35 PM
commands for bash script that handles files of varying number of lines BBFeltham Linux - Newbie 1 07-26-2008 10:18 AM
Run bash commands script as cron anjanesh Linux - General 4 04-03-2007 06:25 AM
how can i run a shell scripts (or a number of commands )when a user logs in to a linu rddreamz Programming 4 05-20-2004 03:08 PM


All times are GMT -5. The time now is 10:16 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration