How to check in a bash script if something is running and exit if it is

后端 未结 9 1944
悲&欢浪女
悲&欢浪女 2021-02-06 00:24

I have a script that runs every 15 minutes but sometimes if the box is busy it hangs and the next process will start before the first one is finished creating a snowball effect.

9条回答
  •  囚心锁ツ
    2021-02-06 00:39

    I had recently the same question and found from above that kill -0 is best for my case:

    echo "Starting process..."
    run-process > $OUTPUT &
    pid=$!
    echo "Process started pid=$pid"
    while true; do
        kill -0 $pid 2> /dev/null || { echo "Process exit detected"; break; }
        sleep 1
    done
    echo "Done."
    

提交回复
热议问题