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`.

# filename:

# check if the reboot flag file exists.
# We created this file before rebooting.
if [ ! -f /var/run/resume-after-reboot ]; then
echo "running script for the first time.."

# run your scripts here

# Preparation for reboot
script="bash /"

# add this script to zsh so it gets triggered immediately after reboot
# change it to .bashrc if using bash shell
echo "$script" >> ~/.zshrc

# create a flag file to check if we are resuming from reboot.
sudo touch /var/run/resume-after-reboot

echo "rebooting.."
# reboot here

echo "resuming script after reboot.."

# Remove the line that we added in zshrc
sed -i '/bash/d' ~/.zshrc

# remove the temporary file that we created to check for reboot
sudo rm -f /var/run/resume-after-reboot

# continue with rest of the script




Lead Software Engineer at A Cloud Guru

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

My Trio-Hackathon Experience

API management for GraphQL?

Building a web3 job scraper in python.

Happy New Year to all Jolters

Test Driven Development with PyTest, Django and Docker — Set up

OpenCart vs Shopify: Which One Is Best For Your Business?

OpenCart vs Shopify Comparison

Hybrid Cloud Strategy and How to Quantify ROI

High-velocity software development using nested Git branches

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Subash Adhikari

Subash Adhikari

Lead Software Engineer at A Cloud Guru

More from Medium

Powershell — How to change the Picture for the Whole Organization in Office 365

A step-by-step guide to setting up git config global properties

A step-by-step guide to setting up global Git config properties by Milan Panchal

Installing Gentoo Linux in Apple Macbook Pro M1

Asahi Linux installer, welcome screen

ssh into database is working, but not from apps.