Resume bash script after reboot

“Close-up of lines of code on a computer screen” by Ilya Pavlov on Unsplash

I often write bash scripts to automate common tasks in my projects. Often the script makes changes that require a reboot. And when the machine finishes rebooting the script needs to resume from where it left.

Following script creates a temporary file that we use as a flag in the script to check if the script is resuming from a reboot. We also temporarily update the .bashrc or .zshrc to trigger the script automatically after reboot. When the script resumes, we remove the temporary file and the extra line we added in the `bashrc` or `zshrc`.

Lead Software Engineer at A Cloud Guru