Find the answer to your Linux question:
Results 1 to 4 of 4
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    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:


    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.

  2. #2
    You could write the shell to await a valid return value before continuing. Better than default 5 minutes. If it completes in 2 minutes, it will continue on immediately ...

    Using bash instead of ksh I wrote this shell:

    echo "beginning";
    $?="1"    #$? is the value of returned function, zero means successful. I set to one for this example
    while [ $? -ne 0 ]
    echo "returnvalue is $? do something else...";

  3. #3
    You can save the procid after starting the script and check if it is still running.

    xxx/xxx.ksh <--- program/script called by the script
    while ps -p $procID > /dev/null; do
    sleep 10
    ---- next commands

  4. $spacer_open
  5. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Seattle, WA, USA
    Why are you running the script in the background? The whole point of running a script in the background (with rare exceptions) is that you don't care when it finishes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts