我有一个 unix 脚本,其中需要重复执行命令,但只有在前一个命令成功完成后才能下次运行该命令。我搜索了可以告诉脚本已完成执行但我找不到的命令。我是 unix 脚本的新手,开始喜欢 unix 脚本。
问问题
834 次
2 回答
1
In order for a command to execute only after the previous one has succeeded, you need to write the two as:
command1 && command2
To have this in a loop with a single command, you will need to check the return status of each invocation and exit the loop if it's not successful; the shortest form should be something like:
while your_command; do :; done
You could also insert a sleep
instead of the NOOP :
.
于 2013-10-07T07:23:26.827 回答
0
您需要将后台命令的 PID(进程 ID)存储在某个文件中,以便您的脚本可以在下次启动时检查它是否仍在运行。例如:
if [ ! "kill -0 $(</var/run/myscript-command.pid) 2>/dev/null 1>&2" ]; then
somecommand&
$CMD_PID = $!
echo $CMD_PID >/var/run/myscript-command.pid
fi
为此,somecommand
需要自行守护进程。如果没有,则将其称为:
nohup ./somecommand 0<&- &>/dev/null &
于 2013-10-07T07:28:30.850 回答