暂停bash脚本直到nohup完成

时间:2015-02-04 02:38:22

标签: bash unix nohup

我有以下脚本来跟踪nohup的pid -

#!/bin/bash
random=$RANDOM
file=nohup_$random
echo "PID and output will be stored in nohup_$random"
echo nohup "$@" &> $file &
nohup "$@" &> $file &
curpid=$!
echo "PID is "$curpid
pidfile=nohup_${random}_$curpid
echo "-- PID for this process is "$curpid >> $pidfile
echo "-- Running command "$@ >> $pidfile
echo "-- From location "$PWD >> $pidfile

我可以以某种方式让脚本在进程结束后显示通知吗? (或者,可能会在rename $file ${file}_done完成后将文件重命名为$curpid?)

1 个答案:

答案 0 :(得分:1)

您可以通过向脚本中添加另一个nohup命令来实现此目的

nohup sh -c 'while ps -p $0 > /dev/null; do sleep 10; done && mv $1 $1_done' $curpid $file &>> $pidfile &

这仍将退出您的脚本,它将运行一个while循环检查该进程是否每10个单位时间运行一次。工作完成后,它将重命名附加“完成”的文件名。

修改后的脚本如下所示

#!/bin/bash
random=$RANDOM
file=nohup_$random
echo "PID and output will be stored in nohup_$random"
echo nohup "$@" &> $file &
nohup "$@" &> $file &
curpid=$!
echo "PID is "$curpid
pidfile=nohup_${random}_$curpid
echo "-- PID for this process is "$curpid >> $pidfile
echo "-- Running command "$@ >> $pidfile 
echo "-- From location "$PWD >> $pidfile
nohup sh -c 'while ps -p $0 > /dev/null; do sleep 10; done && mv $1 $1_done' $curpid $file &>> $pidfile &