Find the answer to your Linux question:
Results 1 to 4 of 4
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2009
    Posts
    11

    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.

  2. #2
    Just Joined!
    Join Date
    Mar 2011
    Posts
    4
    Hi,
    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:

    Code:
    #!/bin/bash
    
    echo "beginning";
    
    $?="1"    #$? is the value of returned function, zero means successful. I set to one for this example
    while [ $? -ne 0 ]
    do
    ./test2.sh
    done
    
    echo "returnvalue is $? do something else...";
    exit

  3. #3
    Linux User
    Join Date
    Dec 2009
    Posts
    264
    You can save the procid after starting the script and check if it is still running.

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

  4. #4
    Linux Guru Cabhan's Avatar
    Join Date
    Jan 2005
    Location
    Seattle, WA, USA
    Posts
    3,252
    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
  •