0

I would like to ask about creating new processes in bash script.

I have script which will execute external programs tests and then check its output status and print if test was successful or not. These test will execute sequentially.

And now I need a few other tests which script create new process for each of these tests max processes of how many tests there are lets say for now I want to run 3 tests in parallel and even with first sequentially tests executions. And I would like to know if there is any how possible to get output status of these separate proceses to my script back and log them? I need to get these value separately of each running process And I do not want to stop these sequentially running processes. And these 3 tests will be running still in cycles with sequentially tests.

So to summarize I need in bash somehow create regression script which will run in cycles TESTS1 whill will be executed sequentially and TESTS2 which will be running in background because their execution si longer then TESTS1. But I need somehow to run TESTS2 in backround, check if are still running get their output status back to parent script to log values and in parallel still executing TESTS1.

Now I am using associative array to log PID of child processes of TESTS2 and testing if particular test id running and if not I start it again. But I do not not How to get its ID. because read of last process output status does not need to be of this test which I am testing is exists.

     # Create array 
    declare -A processes
    for test in ${TESTS2[@]};
    do
        processes[$test]=0
    done
# background prcess
fork()
    {
        OUT=$($TEST)
        STATUS=$?
        if [ $STATUS -eq 0 ]; then
             # do staff if ok
            ((OK++))            
        else
             # do staff if fail
            ((FAIL++))
        fi   
    }

    # -----------------------------------------------------------------------------

    # Repeat Tests i-times
    for ((i=1; i<=100; i++))
    do
        # RUN backgroung proceses
        for TEST in ${TESTS2[@]}; do
            # check if PID lives
            kill -0 ${processes[$TEST]} &> /dev/null
            stat=$?

            # if 0 or not running run it...
            if [[ ${processes[$TEST]} == 0 || $stat > 0 ]]; then  
                fork $TEST $cnt &
                processes[$TEST]=$!   # log PID
            fi           
        done

        # Cycle through TESTS1
        for TEST in ${TESTS1[@]}; do
            OUT=$($TEST)
            STATUS=$?
            if [ $STATUS -eq 0 ]; then
                # do staff if ok
                ((OK++))            
            else
              # do staff if fail
               ((FAIL++))
            fi
            echo -ne "** NOTE: Running Regress Tests: Cycle: $i PASS: $ccount FAIL: $fcount ...\r"

        done

Can anyone has any ideas? Thanks

0

To get the exit code for a background process, use wait:

$ (sleep 1; false) & pid_1=$!; (sleep 1; true) & pid_2=$!
[1] 29111
[2] 29112
$ wait $pid_1
$ echo $?
1
$ wait $pid_2
$ echo $?
0
  • Yes Thank you, But wait will stop my parent script and waiting for particular bg process, And I need executing other tests so far and do not wait for bg processes. I need get output status when bg process ends and to not wait for it to end. – TomyOne Nov 19 '14 at 10:00
0

How about writing the PIDs and output status to a temp file, e.g. via 'mktemp'. After finishing a TEST and checking its status, simply delete the temp file and create a new one when the next TEST runs. Or keep all the temp files for later reference and check the timestamps of the temp files to get the one related to the currently running TEST.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.