Get exit code of a background process

前端 未结 12 1845
别跟我提以往
别跟我提以往 2020-11-22 17:14

I have a command CMD called from my main bourne shell script that takes forever.

I want to modify the script as follows:

  1. Run the command CMD in parallel
12条回答
  •  误落风尘
    2020-11-22 18:01

    #/bin/bash
    
    #pgm to monitor
    tail -f /var/log/messages >> /tmp/log&
    # background cmd pid
    pid=$!
    # loop to monitor running background cmd
    while :
    do
        ps ax | grep $pid | grep -v grep
        ret=$?
        if test "$ret" != "0"
        then
            echo "Monitored pid ended"
            break
        fi
        sleep 5
    
    done
    
    wait $pid
    echo $?
    

提交回复
热议问题