重新启动后如何继续执行Shell脚本?

编程入门 行业动态 更新时间:2024-10-28 22:24:52
本文介绍了重新启动后如何继续执行Shell脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个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脚本?

本文发布于:2023-10-24 07:51:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1523362.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重新启动   脚本   Shell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!