Waiting for the background process to finish


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Waiting for the background process to finish
# 1  
Old 10-19-2010
Question Waiting for the background process to finish

Hi,

I have on shell script which internally calls more than one scripts which run in background.
These scripts cannot be modified to run in foreground.

eg. myscript.sh -> bulk_launcher.sh -> acq_launcher.sh
-> bulk_loader.sh

I want the calling shell script myscript.sh to wait till the other finish their successful execution.

So that after this I can trigger another job.

How do I do this?

Thanks.
# 2  
Old 10-19-2010
general approach to this problem is:
Code:
script1  2&>1 > log1 &  
script2  2&>1 > log2 &  
script3  2&>1 > log3 &  
wait   # this will wait until script1 script2 and script3 are finished

Is that what you mean?
# 3  
Old 10-19-2010
Standard interview question, and actually, many variations on the answer. First, classic wait for specific job:

Code:
set -o bgnice off   # ksh thinks bg jobs are second class
Job_1&
pid_job_1=$!
Job_2&
pid_job_2=$!
 .
 .
 .
wait $pid_job_1
 .
 .
 .
wait $pid_job_2

You can also wait for all jobs with a barefoot wait. Check man ksh. When running a bare wait from the command line, be aware that it waits for other, unrelated jobs you ran from that terminal shell.

Also beware, the child processes will be done, but their descendents may still be running!

More sophisticated, and capable of monitoring all descendent processes:
Code:
(
job1 &
job2 &
) 2>&1 | cat -u >>$jobs_log

When every process inside the parentheses closes (or redirects) both stdout and stderr, the cat will end. You can use stdout for payload or logging, but generally, to keep the parent waiting, reserve one of the two for logging. Plus, you have an integrated log, always a nice idea.

You can also run fuser on the log files to see if any pid has it open. Of course, if someone has their vi nose in there, you will think it is still being written unless you check the pids with ps -fp $pid.
# 4  
Old 10-20-2010
Further to DGPickett.

You can check what jobs are still running with the Shell "jobs" command.
# 5  
Old 10-20-2010
Re: jobs: Only if it is interactive -- no shell script can do this unless it has an expect layer or other source of tty. Even then, you get jobs of other tasks also in the background on this tty, like with wait. Just from memory, I think running on the command line in a subshell ( ; wait) allows you to avoid waiting for unrelated processes.

I like my servers to work unattended! Smilie
# 6  
Old 10-20-2010
Quote:
Re: jobs: Only if it is interactive -- no shell script can do this unless it has an expect layer or other source of tty.
This is not true. Try it.
# 7  
Old 10-20-2010
You are right. This allows you to test without blocking, too. Like wait on the command line, you get all the background. If you subshell or are scripting, you are free of them -- Other People's Children OPC. Of course, their children may still be running! The inheritied stdout/err (...)| cat can track the deeper children.

Code:
$ sleep 99&
[1]     21207
$ sleep 98 &
[2]     21211
$ jobs
[2] +  Running                 sleep 98 &
[1] -  Running                 sleep 99&
$ (sleep 97 & jobs)
[1]     21220
[1] +  Running                 (sleep 97 & jobs)
$



---------- Post updated at 02:25 PM ---------- Previous update was at 02:25 PM ----------

PS: Unlike jobs, fg does not make the batch trip.
This User Gave Thanks to DGPickett For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Waiting a job to finish

Hello again, I am runnning a job on a cluster and I submit a job with the command qsub script.sh where script.sh is a script with the command i want o use. When i enter this command the cluster gives a huber like 123456 as the JOB ID. I want the job to run about 12 hours and when it... (5 Replies)
Discussion started by: lengolass
5 Replies

2. Shell Programming and Scripting

How to put FTP process as a background process/job in perl?

Hi, I am using net::ftp for transferring files now i am trying in the same Linux server as a result ftp is very fast but if the server is other location (remote) then the file transferred will be time consuming. So i want try putting FTP part as a background process. I am unaware how to do... (5 Replies)
Discussion started by: vanitham
5 Replies

3. Shell Programming and Scripting

waiting on jobs in bash, allowing limited parallel jobs at one time, and then for all to finish

Hello, I am running GNU bash, version 3.2.39(1)-release (x86_64-pc-linux-gnu). I have a specific question pertaining to waiting on jobs run in sub-shells, based on the max number of parallel processes I want to allow, and then wait... (1 Reply)
Discussion started by: srao
1 Replies

4. UNIX for Dummies Questions & Answers

Cygwin not waiting for program in background (solved)

Hello, I know this isn't exactly a Unix question, but I wasn't able to find much information elsewhere. I'm trying to run a program in the background using Cygwin on a Windows machine, then use the wait command to pause before proceeding. Unfortunately, as I've confirmed using ps aux, the... (0 Replies)
Discussion started by: ocdcollector
0 Replies

5. UNIX for Dummies Questions & Answers

Script to start background process and then kill process

What I need to learn is how to use a script that launches background processes, and then kills those processes as needed. The script successfully launches the script. But how do I check to see if the job exists before I kill it? I know my problem is mostly failure to understand parameter... (4 Replies)
Discussion started by: holocene
4 Replies

6. Shell Programming and Scripting

Waiting for an arbitrary background process (limiting number of jobs running)

Hi, I'm trying to write a script to decompress a directory full of files. The decompression commands can run in the background, so that many can run at once. But I want to limit the number running at any one time, so that I don't overload the machine. Something like this: n=0 for i in *.gz... (15 Replies)
Discussion started by: p.f.moore
15 Replies

7. Shell Programming and Scripting

Waiting for a background process to finish

Hello, I am a novice shell script programmer. And facing this problem any help is appreciated. I m writing a shell script and running few commands in it background as I have to run them simultaneously. Sample code : sql_prog & sql_prog & sql_prog & echo "Process Completed" Here... (2 Replies)
Discussion started by: vineetbhati
2 Replies

8. AIX

Waiting time of Process

Hi all, I am trying to find out the process wait time on Unix(AIX/SOLARIS) machine( only sh/ksh/csh): Like EXAMPLE 1 : $ vmstat 2 System configuration: lcpu=16 mem=32000MB kthr memory page faults cpu ----- -----------... (4 Replies)
Discussion started by: chandrakala.sg
4 Replies

9. UNIX for Advanced & Expert Users

Process(s) ID waiting on IO?

Hello Experts!! My CPU is waiting a lot (around 33%) on I/O. I would like to find out what process(s) are waiting on the i/o. Below is my real time output of vmstat and sar. Thanks for you help !!!! Regards Citrus OS: AIX - 5L : /u2/oracle >oslevel 5.3.0.0 : /u2/oracle... (9 Replies)
Discussion started by: Citrus143
9 Replies

10. Linux

waiting process

how to know the information of the waiting process how to calculate the time of the process that it has taken to execute i want to make a program that Should be able to keep a log of the processes expired(The log should contain the starting time, expiry time, time slices used, total execution... (2 Replies)
Discussion started by: shukla_chanchal
2 Replies
Login or Register to Ask a Question