How to have a Shell script continue after reboot?

前端 未结 3 722
野的像风
野的像风 2020-12-20 07:14

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 co

3条回答
  •  鱼传尺愫
    2020-12-20 08:04

    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.

提交回复
热议问题