Why do we need to create Startup Script to run after the operating system starts?
If you have a home server, or maybe even just a Linux desktop, and you run the same fine-tuning commands each time you restart, running scripts automatically can make your life easier.
The main reasons are that there will no longer be starting the server, logging in via SSH, entering a password, getting a root elevation and then manually executing script after script. Instead, we are going to use the power of Crontab and set your system to run these scripts automatically at startup! Here’s how to do it.
To get started, open a terminal window and enter the following command:
If the system has not previously used crontab, the user will need to specify an editor to work with. Although all editors are good in their own way, choose “nano” as it is the simplest text editor and does not require much confusion. With the editor selected, cron will load a default file with detailed instructions on how everything works.
Inside the nano editor in the terminal, scroll down to the bottom and start typing “@reboot”. The reboot command is key here, as it tells cron to restart this command to run each time. Immediately after the reboot, add the full file path to the bash script.
Code language: CSS (css)
Now that the command is set up, the crontab can be saved. Press “Ctrl + x” on the keyboard. This will prompt the user to “write the file”. By default, cron calls crontab, so don’t change anything. Press the enter key to save the crontab.
In the same way that the command was added to crontab, it can be removed. To do this, open a terminal and enter sudo crontab -e. This will load the crontab file. Just delete the command you added, save it, and restart your computer (or server).
Troubleshoot with Cron
Sometimes cron does not execute commands and this can be a problem. The easiest way to fix all cron problems (if any) is to review the system log. To do this, open a terminal window and enter this command:
grep CRON / var / log / syslog
Syslog displays all system events and through grep it is possible to filter what cron and crontag do. This should allow users to easily troubleshoot and fix anything that may go wrong.
In conclusion, we can say that:
Bash scripts are a great thing and one of the great strengths of Linux. This makes the administration of servers and even ordinary Linux computers easier because of the ability to accept large amounts of commands and automate them. By adding cron to the picture, these scripts have the power to become even more useful. You will no longer walk after installing your Linux box. Just adjust it and forget it!
Here is a 60 second video tutorial on How To Create a Startup Script using Crontab in Lubuntu Linux