The main reasons are that there would be no more starting up the server, logging in over ssh, entering a password, getting a root shell and then manually executing script after script. Instead, harness the power of cron, and set your system to automatically run these scripts at startup! Here’s how to do it. Tip: Check out our regular expressions cheatsheet.

Setting up Cron

Most Linux distributions come with the ability to access cron by just entering crontab -e. However, if you’ve entered this command, and nothing at all has happened, you’re on a Linux distribution that has no way to interact with cron. This means that you’ll need to install a tool to continue. The most popular tool to use in this situation is a daemon known as “cronie.” It’s a very popular tool and resides in most popular Linux distribution repositories. Open up a terminal and install cronie with your package manager. Alternatively, head over to this page and download a package for your distribution.

Setting up the script with Cron

Opening a crontab is very easy. To start, open up a terminal window and enter the following command:

Note: the sudo is important if you want to run script as root. You can omit the sudo if you just want to run the script as a normal user. If the system hasn’t used crontab before, the user will need to specify an editor to work with. Though all the editors are good in their own way, choose “nano” as it’s the simplest text editor and doesn’t require a lot of fussing with. With the editor selected, cron will load up a default file with detailed instructions as to how everything works. Inside the nano editor in the terminal scroll all the way down to the bottom and start off by writing “@reboot.” The reboot command is key here as it tells the cron on reboot this command to run every single time. Directly after reboot, add the full file path to the bash script.

Now that the command is set up, the crontab can be saved. Press “Ctrl + o” on the keyboard. This will prompt the user to “write out the file.” By default, cron names the crontab, so don’t change anything. Press the enter key to save the crontab.

Remove the script from startup

In the same way that the command was added to the crontab, it can be removed. To do this, open up a terminal and enter sudo crontab -e. This will load the crontab file. Just delete the command that was added, save it, and restart the computer (or server).

Troubleshooting Cron

Sometimes cron doesn’t execute commands, and that can be a problem. The easiest way to troubleshoot any issues with cron (should there be any) is to look at the system log. To do that, open the terminal window and enter this command: The syslog shows all system events, and by using the grep command, it is possible to filter out what cron and crontag does. This should allow users to easily troubleshoot and fix anything that may go wrong.

Conclusion

Bash scripting is a wonderful thing, and its one of Linux’s great strengths. It makes administration of servers and even regular Linux computers easier because of the ability to take large amounts of commands and automate them. By adding cron to the picture, these scripts have the power to become even more useful. No more tinkering around after your Linux box boots. Just set it up and forget it! What root scripts would you run at startup on your Linux box? Tell us below!