Share your knowledge at the LQ Wiki.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-11-2012, 11:46 AM   #1
LQ Newbie
Registered: Oct 2012
Posts: 3

Rep: Reputation: Disabled
Help with "wait" or "timeout" in bash

I've read the man page for wait and timeout but I'm new, and don't quite understand how to use it in my application.

I have a set of batch scripts that call each other, and I want to kill a child (grandchild?) process if it runs for too long. Then, after all processes are finished, I want to echo that the script is finished. So, here's my structure.

The top-level script is called "master"
echo "START TIME\t\t\t\t\t\t" $(date +%T) > ../output/timelog
./master_2 #other scripts are called, but I've simplified it for you
echo "END TIME\t\t\t\t\t\t" $(date +%T) >> ../output/timelog
The next level is "master_2"
./run_1 &
./run_2 &
it runs two scripts in the background, and one (the slowest) in the foreground, so that when run_3 ends, usually run_1 and run_2 have ended, and "master" will echo the end time.

now, each run_* batch script is something like this:
cd ../target/1
for dir1 in $(find -maxdepth 1 -mindepth 1 -type d)
cd $dir1
cd ../
Lastly, "child" calls "youngestchild" in series a few times.

By running master, multiple master_2 scripts are called in series. Each master_2 script calls some run_* scripts in parallel, and each run_* script calls some child scripts in series, which then call some youngestchild programs in series. The youngestchild program typically takes a few minutes, but if there are errors it can go for hours. So, I would like to use the wait (or timeout) command in one of the upper level scripts (preferably master_2) to tell the system to kill any child/grandchild/great-grandchild process that lasts longer than 10 minutes. I've tried to implement this by changing calls to "youngestchild" from "./youngestchild" to "timeout 10m youngestchild" but I have a lot of calls, and it would be very tedious to add this to every file. Also, if I ever need to change the timeout from 10 minutes to 20 minutes, it would be a huge hassle.

I know the series/parallel structure is strange, but it's necessary to make this computation run quickly.

Old 11-11-2012, 03:15 PM   #2
Registered: Sep 2003
Posts: 382

Rep: Reputation: 87
Hi jeffy_weffy!

One simple thing to do would be to change the master to be this:

echo "START TIME\t\t\t\t\t\t" $(date +%T) > ../output/timelog
export TIME_LIMIT='10m'
./master_2 #other scripts are called, but I've simplified it for you
echo "END TIME\t\t\t\t\t\t" $(date +%T) >> ../output/timelog
and put this:

1,$s/\.\/youngestchild/timeout \$TIME_LIMIT .\/youngestchild/
in a file named yc.sed

then do this:

mkdir NEW
for filename in run_*; do echo "$filename:"; sed -f yc.sed < $filename > NEW/$filename ; done
diff -r  NEW .
This procedure uses the "Stream EDitor" sed in a programmatic loop from a shell command prompt, to place a timeout command in front of each occurrence of "./youngestchild" in each file with a name like run_*.

Instead of using a fixed timeout, a shell environment variable is used to hold the timeout, which can then be set in the master script. It's "exported" into the shell environment, so it's value can be accessed from programs run from the shell.

When I followed that procedure with one file name run_1 I got this output from the diff:

< ./youngestchild
> timeout $TIME_LIMIT ./youngestchild
which shows the differences between the version of the file in the NEW directory and the original file version.

I realize that the "youngestchild" programs might not actually be named "youngestchild", they might well each have a different name. The actual approach you would be able to use might have to be modified in order to do this same general type of thing, with different program/script names. But hopefully this has served to show that it shouldn't be very difficult to do what you want.

The purpose of the NEW directory is to keep the changed versions of the files separate from the originals until you're sure the changed versions are exactly what are needed. Then the changed files can be used in place of the originals. But I'd suggest keeping the originals safe, just in case you need to refer back to them.

If the "youngestchild" programs have some pattern to their names, the pattern might be used in place of the specific name I used in my example. If the names are actually very different from one another, an awk program could be substituted for sed, such that the awk program looks for any of a list of program names to change.

Likewise, even though I used a single pattern in the for loop to select the filenames in which to place the timeout commands, multiple patterns, or a list of filenames could be used in place of the single pattern I used, if what you showed as run_1 run_2 etc., actually have completely distinct names, that don't match any single pattern.

Hope this helps.

Last edited by kakaka; 11-11-2012 at 03:44 PM.


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: using "select" to show multi-word options? (like "option 1"/"o zidane_tribal Programming 7 12-19-2015 02:03 AM
"irq timeout" and "failed opcode was: unknown" dmesg messages kenneho Linux - Server 0 06-17-2009 03:35 AM
Standard commands give "-bash: open: command not found" even in "su -" and "su root" mibo12 Linux - General 4 11-11-2007 11:18 PM
C/C++ functions similar to BASH's "cp", "mv", "mkdir", etc? kornerr Programming 10 04-23-2006 10:48 AM
"My posts" and "My threads" should be exempt from the wait time before searches vharishankar LQ Suggestions & Feedback 6 03-06-2006 09:10 PM

All times are GMT -5. The time now is 07:10 PM.

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