我有一个Shell脚本,该脚本需要循环运行,并执行一系列命令,完成后要重复执行循环.在每个命令之间有几分钟的睡眠命令. 工作"绝不应该终止.我可以让脚本开始启动时间,但是它需要在重新启动系统时按照命令顺序从上次中断的地方继续.
I have a Shell script that needs to run in a loop, and perform a series of commands, and when it's finished repeat, hence the loop. Between each command there is a sleep command for a few minutes. The "job" should never terminate. I can have the script start a boot time, but it needs to continue where it left off in the sequence for the commands when the system is rebooted.
我怎样才能最好地做到这一点?我应该创建一个包含命令队列的MySQL表,并让它在每次成功执行后删除每一行吗?然后,当它完成循环时,它将重新填充队列表并从顶部开始.
How can I best accomplished this? Should I create a MySQL table of the queue of commands, and have it delete each row after each time it successfully executes it? Then when it completes the loops it would re-populate the queue table and start from the top.
似乎我缺少使它变得更简单的东西.在此先感谢您提供有用的见解!
It seems like I'm missing something to make this more simple. Thanks in advance for your helpful insight!
推荐答案您可能希望重写代码,使其看起来像这样:
You may want to rewrite your code so that it looks like this:
while: ; do case $step in 0) command_1 && ((step++)) ;; 1) command_2 && ((step++)) ;; ... 9) command_9 && step=0 ;; *) echo "ERROR" >&2 ; exit 1 ;; esac done因此,您将通过测试step的值来了解已完成的操作.
So you would be aware of what has been done by testing the value of step.
然后,您可能需要在执行while循环之前设置trap,以便在退出时将step的值写入日志文件:
Then, you may want to set a trap before the while loop is executed, so that, on exit, the value of step is written to a log file:
trap "echo step=$step > log_file" EXIT然后,您所需要做的就是在脚本的开头source日志文件,最后一个将在停止的位置继续工作.
Then, all you need to do is to source the log file at the beginning of the script, and the last one will continue its job where it has been stopped.
更多推荐
重新启动后如何继续执行Shell脚本?
发布评论