Pausing and resuming a Bash script

Is there a way to pause the Bash script and then resume it at a different time, for example after restarting the computer?

+3


source to share


5 answers


The only way to do it AFAIK:



  • Store any variables or other script context information in a temporary file to set the script state just before pause. It goes without saying that the script should include a mechanism to check that file to see if the previous execution is suspended and, if so, get the entire context and resume accordingly.
  • After rebooting, manually run the script again, OR the script will automatically run from your script run profile.
+2


source


Try Ctrl-Z to pause the command. I dont think you can pause it and then resume after reboot unless you save the state somehow.



+1


source


You cannot pause and resume the same script after a reboot, but the script can arrange for another script to run after a while. For example, it can create an init script (or a cron job or login script, etc.) that contains the tasks you want to defer and then delete yourself.

+1


source


Intriguing ...

You can pause in BASH with CTRL-Z, but after reboot, you won't be able to resume. A reboot initializes the machine and the process that was suspended is terminated.

However, it may be possible to force the process to start in coredump using "kill -QUIT $ pid and then use

gdb" to restart the script. I tried for a while but was unable to do so. Perhaps someone else can provide the path.

+1


source


If this applies to your script and the job it is executing, add breakpoints to it - this means that all process state will be saved to disk before continuing. Then ask each individual part to check if there is an output they should produce and skip if there is. This should make rerunning the script almost as efficient as resuming from the same place during execution.

Alternatively, run the script in a virtual machine. Freeze the virtual machine before shutting down the real system and resume after. It probably costs a really huge and complex shell script for this.

+1


source







All Articles