LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-12-2008, 01:34 PM   #1
hamtavs
Member
 
Registered: Aug 2004
Location: Italy,near Milan
Distribution: red hat 9
Posts: 63

Rep: Reputation: 15
[Bash] Waiting for a process to terminate or stopping it after some time


Hi folks,

I've got a bunch of files to be processed by a program, one after the other.
File processing can take some time. I want the program not to take more than 1 hour for each file.
So I put up the following bash script. It calls iteratively the program and stops it after 1 hour. The program is expected to perform a clean exit upon receiving the SIGHUP signal.
Code:
	
a_program $file & 
sleep 3600
kill -s SIGHUP $!
The problem with the script is that it always waits 1 hour before processing the next file, independently from program termination.
How can I tell the shell to resume the script when the program terminate as well?

thanks

Tavs
 
Old 12-12-2008, 05:19 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Blue Ridge Mountain
Distribution: Linux Mint 17, Debian 8
Posts: 7,864

Rep: Reputation: 311Reputation: 311Reputation: 311Reputation: 311
Run both the a_program command and the sleep command in the background. Then issue a wait command to wait for whichever background command completes first.

------------------
Steve Stites

P.S. If the program can process the files in parallel then you could start all of the a_programs in the background simultaneously. Then start the sleep in the background. Then wait on all background jobs and keep track of which a_programs have completed. If all a_programs have completed kill the sleep process. If the sleep process completes before all of the a_program processes have completed then kill the remaining a_programs.

Last edited by jailbait; 12-12-2008 at 06:50 PM.
 
Old 12-12-2008, 06:25 PM   #3
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
A dirty way is to check for the elapsed time of the background process using the ps command:
Code:
a_program $file &

while [ $(ps -p $! -o etime= | tr -d ": ") -lt 10000 ]
do
  sleep 60
done 2> /dev/null

kill -s SIGHUP $!
The while loop will end up when the elapsed time is greater than (or equal to) 1 hour or when the process is terminated. The format of the elapsed time is MM:SS or HH:MM:SS with trailing blank spaces, hence the tr command to delete commas and blank spaces, leaving a pure number. The value 10000 corresponds to 1 hour.

If the program terminates before 1 hour, the output of the ps command is null and the shell would give a "unary operator expected" error (redirected to /dev/null). In this case the while loop is broken due to the error.
 
  


Reply


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how ctrl+c works to terminate a process rithik_ghoshal Linux - General 2 11-02-2008 09:01 AM
how to terminate dispatch.fcgi process properly sathiyamoorthy Debian 0 02-27-2008 12:20 AM
ctrl-c to terminate a process does not work geek1234 Linux - Newbie 1 12-26-2007 03:34 PM
time a process within bash script aubrey-calm2 Linux - General 2 06-15-2007 02:39 PM
"Starting System Logger" during boot time, stopping boot process. quickNitin Linux - Newbie 1 06-08-2006 02:31 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 02:35 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration