Problem running a program/script in the background from a script

Hi all,

I have a script that calls another program/script, xxx, to run in the background. Supposedly this program at most should finish within five (5) minutes so after five (5) minutes, I run some other steps to run the script into completion.

My problem is sometimes the program takes longer than five (5) minutes and this is causing problems when running the rest of the steps in the scripts. Can anyone suggest how to re-program my script.

At the moment, the KSH script, i.e. test.ksh, is doing as below:

test.ksh:

....
....
xxx/xxx.ksh <--- program/script called by the script
sleep 300
..... run the rest of the script ......
...... problem is sometimes xxx/xxx.ksh takes longer than 300 seconds ......
...... any way that I can monitor that xxx/xxx.ksh finishes before I run ......
...... the rest of the scripts ......

Any advise will be much appreciated. Thanks in advance.

You can run your xxx/xxx.ksh in background and wait till finished like below.

xxx/xxx.ksh & 
process_ID=$!
wait $process_ID

Pravin27, as he wasn't running the scipt in the background orginally - it appears this script starts background tasks and returns straight away (hence the sleep in the original code).

Newbie_01, you may need to use ps and watch till the commands that xxx.ksh starts in the background have finished.

If the script needs to wait for a single job to complete there is no reason to background the job. In this context a background job issued without a "nohup" might as well be in foreground.
Rather than "ps" try the "jobs" command to monitor your background processes.

"jobs" will only list the child processes of the current script not those started by xxx.ksh, unless it is source in the current shell by . xxx/xxx.ksh

I'm reluctant to recommend sourcing the script or changing it (e.g. adding a "run in foreground" flag, or a "wait before returning" flag) without knowing more about what it is doing and how complex the bacground task(s) it starts are. This is why I recommended using ps to watch the tasks it starts and continuing when the are done.

is it possible to modify the background script to add a line? I would create a file in the last line using touch and in this current script i would wait till this file was created. Then I would remove this file and continue .