Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

mysql - How to have a Shell script continue after reboot?

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.

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

So you would be aware of what has been done by testing the value of 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

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...