暂停 bash 脚本直到 nohup 完成

Pause bash script till a nohup is finished

我有以下脚本来跟踪 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

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

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

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

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

修改后的脚本如下所示

#!/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 [=10=] > /dev/null; do sleep 10; done && mv  _done' $curpid $file &>> $pidfile &