Get exit code of a background process

前端 未结 12 1838
别跟我提以往
别跟我提以往 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 17:48

    My solution was to use an anonymous pipe to pass the status to a monitoring loop. There are no temporary files used to exchange status so nothing to cleanup. If you were uncertain about the number of background jobs the break condition could be [ -z "$(jobs -p)" ].

    #!/bin/bash
    
    exec 3<> <(:)
    
    { sleep 15 ; echo "sleep/exit $?" >&3 ; } &
    
    while read -u 3 -t 1 -r STAT CODE || STAT="timeout" ; do
        echo "stat: ${STAT}; code: ${CODE}"
        if [ "${STAT}" = "sleep/exit" ] ; then
            break
        fi
    done
    

提交回复
热议问题