How to determine the completion of a background process to trigger something else?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to determine the completion of a background process to trigger something else?
# 1  
Old 08-19-2014
How to determine the completion of a background process to trigger something else?

I've been thinking about a peculiar problem, and so far haven't been able to find out a convincing solution to that.

To put it simply, I have a shell script (assume it to be parent_script.sh), calling another shell script (child_script.sh) 5 times, in nohup mode to be executed in the background. The parent script is something like this

Code:
for i in $(seq 1 5)
do
nohup ./child_script.sh &
done

For each run, child_script generates 10 files in a particular folder. So for a single run, parent_script.sh will be having 50 files generated.

After all the 50 files are created in that folder, I need to rename these files. For instance, if the files are created as abc.txt, def.txt.....xyz.txt, then after renaming, these will be abc_01.txt, def_02.txt....xyz_50.txt.

Now in order to start the renaming process, I've thought of adding a small code block in the parent_script.sh, something like below

Code:
i=1
while [ $i -eq 1 ]
do
proc_count= ps -ef | grep child_script.sh | grep -v grep | wc -l

if [ $proc_count -eq 0 ]
then
<the renaming stuff>
i=0

fi

sleep 10

done

This will ensure that the renaming process will wait until all the 5 parallel threads get completed, then start renaming the files, and then finish the parent script.

My problem is, this file generation process for the 5 threads may take quite a few hours to get completed, may be 2-3 hours on an average. But as per the requirement, the parent script is invoked once every hour. So it may very well happen that at a time more than 5 parallel threads of child_script.sh are running, for multiple executions of the parent script. In such a scenario, the count of running processes of child_script.sh will never be zero.

What can be an alternative way out for this? I'm not much of an advanced unix user, and any help will be much appreciated.

N.B. for each hourly run of the parent script, the renaming sequence of 1 to 50 will recycle, i.e. for first hour, the files will be abc_01.txt...xyz_50.txt, similarty for the files created in the second hour will be named ABC_01.txt...XYZ_50.txt
# 2  
Old 08-19-2014
# 3  
Old 08-19-2014
As achenle points out use the shell built-in wait command which will wait for all child processes started in this shell:

Code:
for i in $(seq 1 5)
do
nohup ./child_script.sh &
done

# wait for child processes to finish
wait

<the renaming stuff>

# 4  
Old 08-19-2014
A lock file, testing for an output file, or incron are all workable.

Mike
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Pick file and Trigger informatica process

Hi, I am creating a script in which we will check a folder. If any file placed inside the folder, script will move the file to another folder and Trigger etl process. Now the issue is that the script should continously check the presence of file inside folder after every minute or hour. So how... (1 Reply)
Discussion started by: dgargd2524
1 Replies

2. UNIX for Advanced & Expert Users

Issue with tracking successful completion of Child process running in background

Hello All, I am using Linux. I have two scripts: inner_script.ksh main_wrapper_calling_inner.ksh Below is the code snippet of the main_wrapper_calling_inner.ksh: #!/bin/ksh ppids=() ---> Main array for process ids. fppids=() ---> array to capture failed process ids. pcnt=0 --->... (5 Replies)
Discussion started by: dmukherjee
5 Replies

3. Shell Programming and Scripting

Make background process interact with fg process

Hi, I have written a menu driven shell script in which as per the choice, I run the another script on background. For eg: 1. get info 2)process info 3)modify info All the operations have different scripts which i schedule in background using &. However I wish to display the error... (0 Replies)
Discussion started by: ashima jain
0 Replies

4. Shell Programming and Scripting

Watchdog for a specific process trigger another process

Hi, I am willing to build a script that does a "ps-ef" or "top" on a specific process, and if it finds it running to start another process. Any suggestions to do this in a optimized way? Thanks! (2 Replies)
Discussion started by: liviusbr
2 Replies

5. 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

6. Shell Programming and Scripting

Challenging task : script for mailing process completion timing report to users.

Hi all, This is my first post. I am new to unix scripting. My requirement is as follows : We are using a financial backoffice application. Now at the end of day we have send users a status report stating all timings of EOD processes for all countries. I need timings for following... (0 Replies)
Discussion started by: ammbhhar
0 Replies

7. UNIX for Dummies Questions & Answers

Background Process Completion

I have my unix machine configured to run locate.updatedb on login in the background and after it completes, when I run a command such as ls-- the console returns the results of ls and + Done sudo /usr/libexec/locate.updatedbIs there... (3 Replies)
Discussion started by: Prodiga1
3 Replies

8. 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

9. UNIX for Dummies Questions & Answers

Script to check process completion

Hi, OS - Unix Iam doing the following: after login to the unix box 1. change directory 2. run a shell script "preinstall.sh" 3. This takes apprx 5 mins 4. after which i use to change permission of a file "installhub.sh" (this file is generated from the previous step). Is there anyway... (2 Replies)
Discussion started by: kenkanya
2 Replies

10. Shell Programming and Scripting

capture the process id when starting a background process

Hello all, How do I start a background process and save the process id to a file on my system. For example %wait 5 & will execute and print the process id. I can't figure out how to get it to a file. I've tried: > filename 0>filename 1>filename. Any assistance is most appreciated. Thanks, Jim... (10 Replies)
Discussion started by: jleavitt
10 Replies
Login or Register to Ask a Question