How to avoid outages by automatically restarting your script on Linux
This is a quick tutorial to show you how to set a script or application to start up on a reboot of your machine. This will be done through a cron job. The method should work the same on most/all Linux distros.
This tutorial presumes you have an application/script that you can start via command line. In this case, we want to make sure that a script called “tutorial.sh” in the folder: /root/tutorial/ starts up on reboot without manual intervention.
Connect via SSH to the VPS that will be running the script.
Then, use the command:
This will open a text file called the “crontab”. Then you’d add the following line (altered to match your application) to the end of the file.
@reboot /bin/bash /root/tutorial/tutorial.sh
Exit the editor. Ensure that the crontab editor validated the new crontab and that there were no errors.
[STEP 5 – Optional]
Reboot your server to verify the script automatically started.