Crontab Reboot: How to Schedule Jobs for Automatic Execution at System Startup

Try this guide with our instant dedicated server for as low as 40 Euros

crontab reboot

By providing your email address or using a single sign-on provider to create an account, you agree to our Terms of Service and that you have reviewed our Privacy Policy.

Thank you for contacting us! We will get in touch with you soon.

Ease of automation is one of the reasons sysadmins and users opt for Linux as their preferred operating system for crontab reboot. 

crontab is a fantastic utility for automating and scheduling task execution in a Linux environment. All popular Linux distributions include this utility with similar operations across the board. You can set up tasks to execute at any time, including system reboot. 

In this article, we will discuss how to use crontab as a fantastic tool to schedule tasks to run automatically when your system boots up. This is a game-changer for server maintenance, automation of routine tasks, and for everyone who loves to save time and increase efficiency.

Before going into the details of using crontab to set up jobs that would be executed at system reboot, let’s discuss the crontab command in general. 

Table Of Contents

  1. What is the Crontab Command?
  2. Working with Crontab on System Boot
    1. Prerequisites
    2. Schedule a Cron Job at System Boot
    3. Run a Cron Job After Boot With Delay
    4. Remove a Command Scheduled To Be Executed at System Reboot
  3. Uses Cases for Scheduling Cron Jobs for System Boot
    1. Run a Backup Script at Boot
    2. Run Periodic Maintenance Tasks
    3. Start Virtual Machines or Containers
    4. Initialize Networking or Firewall Settings
  4. Best Practices for Effectively Using Cron
  5. Conclusion
  6. FAQs

What is the Crontab Command?

crontab, short for cron table,” is a time-based job scheduler available in most Unix-like operating systems. It allows you to schedule scripts or commands to run automatically at specified times and dates.

You can schedule tasks precisely, down to the minutes and seconds, without complex looping or application setups. The utility is resource-efficient, as it doesn’t reserve system resources (processing and memory) when it’s not actively running.

In addition to a system-wide crontab, each user has their own crontab file. Sysadmins can view the cron jobs for the system and user accounts. On the other hand, a user can only list cron jobs scheduled in their crontab file

The system reads these files and executes the listed commands at the scheduled times. 

Working with Crontab on System Boot

Now that you know how crontab works, let’s see how you can use it to schedule jobs that run at system reboot. 

Prerequisites

Before we begin, make sure you have the following:

  • Working knowledge of Linux commands.
  • Access to a Linux system where you can edit crontab.
  • Permissions to modify Crontab entries (typically root or sudo privileges).

Schedule a Cron Job at System Boot

Start by accessing the list of scheduled tasks with the following command:

# crontab -e

This command opens the crontab configuration file in the default system editor. If you have several text editors installed, the system will ask you to pick one to view the file. Simply enter the number associated with your preferred editor.

You need to use the crontab’s @reboot directive that tells the system to run the specified command or script once right after the system starts up.

The proper format for using the @reboot directive is as follows:

@reboot [path to command] [argument1] [argument2] … [argument n]

@reboot [part to shell script]

You need to provide the complete path (starting from the root) to the job, script, or command you wish to execute.

For instance, the following statement in the crontab file saves the system’s date in a file named date.txt upon Linux reboot:

@reboot date >> ~/date.txt

reboot date

Similarly, if you intend to execute a backup script during reboot, you can include the following statement in use:

@reboot /root/backup.sh

Once the job is added, save and exit the editor. 

You will see a status message confirming the successful installation of the new crontab entry.

crontab -e

Important: You need to make sure that the crond service is enabled on your system to ensure the cron jobs are executed on system boot. 

Start by verifying that the crond service is enabled on your system:

# sudo systemctl status cron.service

sudo systemctl status cron

If it’s not enabled, you can enable it using the following command:

# sudo systemctl enable cron.service

Run a Cron Job After Boot With Delay

Sometimes, you might want to delay the execution of a task after boot. This can be useful if the task requires network connectivity or other services to be up and running.

In this case, you can use the sleep command in the crontab entry to add a delay in execution. 

@reboot sleep [time in seconds] && [path to job]

For instance, the following command creates a text file containing the system date five minutes after the system reboot:

@reboot sleep 300 && date >> ~/date.txt

Remove a Command Scheduled To Be Executed at System Reboot

To remove a job containing the @reboot directive, edit the crontab file with the crontab -e command and simply delete the line containing the job. 

Remember to save the file before exiting the editor.

Uses Cases for Scheduling Cron Jobs for System Boot

Let’s look at some examples of tasks suitable for execution at system boot.

Run a Backup Script at Boot

You can run backup scripts or commands to ensure critical data is backed up right after the system boots. 

Add the following directive to the system crontab file:

@reboot /path/to/backup_script.sh

Update System Packages

You can make sure that you have the latest packages on your system by adding the following entry in the crontab file. When executed, the system will automatically check for and install package updates at boot.

@reboot sudo apt update && sudo apt upgrade -y

If you are using a different package manager, adjust the command accordingly.

Run Periodic Maintenance Tasks

When the system starts, many sysadmins prefer to run tasks like disk cleanup, log rotation, or database maintenance. You can include relevant commands in a script and run it at system boot with the following statement in the crontab file:

@reboot /path/to/maintenance_script.sh

Start Virtual Machines or Containers

If you have virtual machines or containers hosted on your system, you can use the @reboot directive to launch them automatically.

@reboot virsh start my_vm

Initialize Networking or Firewall Settings

Use the following statement in the crontab file to set up network configurations or firewall rules at system startup.

@reboot /path/to/network_setup_script.sh

Remember to use the full paths for commands and scripts in the @reboot entries to ensure they execute correctly. Additionally, exercise caution and thoroughly test these commands to avoid any unintended consequences during the system startup process.

These use cases illustrate just a few application areas of the idea of using crontab job scheduler. We have a detailed article on crontab that covers scheduling and viewing cron jobs in detail.

Best Practices for Effectively Using Cron 

Let’s look at some tips to use the cron efficiently.

  • Test new cron entries thoroughly before deploying them in a production environment. Validate a new cron entry by rebooting the system and ensuring the job runs correctly. If a job is complex, conduct comprehensive testing covering various combinations and scenarios.
  • Consider turning lengthy or complicated commands into a script for better management. Configure the cron entry to call the script, specifying its full path, as exemplified by @reboot /path/to/backup_script.sh
  • In certain circumstances, the init.d system may be a preferable alternative to @reboot, allowing more precise control over starting or stopping services and sequencing events. The @reboot service is user-friendly, but be aware that the cron daemon launches at boot time, potentially causing failures if dependent services haven’t started. Add sleep time to the script as a workaround.
  • The cron utility sends emails if a job fails or encounters errors, with the default recipient being the crontab file owner.

    You can override the default recipient by using a MAILTO directive, such as MAILTO=[email protected].

For a local user, set MAILTO to the local username: MAILTO=localuser. To disable emails entirely, set MAILTO to an empty string. However, this is generally not recommended as it may result in missing important error messages: MAILTO=””.

Conclusion

By the end of this tutorial, you will have learned to utilize crontab effectively for scheduling tasks to execute during system reboot. Automating tasks using crontab can significantly streamline your server management and personal projects. 

It’s a powerful tool, especially when combined with reliable hosting solutions like RedSwitches Bare Metal Hosting Provider, offering robust and scalable hosting options perfect for any automated task you throw at it.

So, If you’re looking for a robust server for your Linux projects, we offer the best dedicated server pricing and deliver instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated servers, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.

FAQs

Q. Can I use Crontab as a regular user?

 Yes, each user can have their own crontab file.

Q. How do I see all my current cron jobs?

 Use crontab -l to list all cron jobs for the current user.

Q. Can I edit the crontab file in any text editor?

 Yes, but crontab -e opens it in the default system editor, which varies.

Q. Are there any limitations to what can be run at boot?

 The limitations are generally around permissions and dependencies on other services.

Try this guide with our instant dedicated server for as low as 40 Euros