Crontab Reboot: How to Execute a Job Automatically at Boot

October 28, 2020

Introduction

The Cron daemon is a Linux utility used for scheduling system tasks and processes. It uses cron tables (crontab) to store and read scheduled jobs.

This tutorial will cover how you can use crontab to schedule jobs to be run at system reboot.

Crontab reboot: how to execute jobs automatically at boot

Prerequisites

Crontab Command Overview

With the crontab command, you have full control of when and how jobs are executed. Use crontab to set job execution time down to the minute, without the need for looping and timing logic in the task.

crontab has low resource requirements since it doesn’t reserve system memory when it isn’t running.

Crontab on Boot: Run a Cron Job at Boot Time

Open the cron task list by using the following command:

crontab -e

If you have multiple text editors installed, the system prompts you to select an editor to update the cron task list with. Use the number in the brackets to choose your preferred option. We will be using the default option, Nano.

Opening cron jobs ist with the crontab command

Note: Leaving the field blank and pressing enter chooses the first option available.

To run a cron job at every system boot, add a string called @reboot to the end of the task list. The job defined by this string runs at startup, immediately after Linux reboots.

Use the following syntax when adding a @reboot string:

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

Note: Always use the full path to the job, script, or command you want to run, starting from the root.

Press Control + X to exit Nano, then Y and Enter to save any changes you made.

For example, if we wanted to have the system date written in a file called date.txt when Linux restarts, we would add the following string:

@reboot date >> ~/date.txt

If we wanted to run the backup shell at reboot, we would add:

@reboot /root/backup.sh
Updating the cron job list

Note: In some cases, the crond service needs to be enabled on boot for the configuration to function.
To check if the crond service is enabled, use:

sudo systemctl status cron.service

To enable this service, use:

sudo systemctl enable cron.service

Run a Cron Job at Boot With Delay

To run a job with a delay after the system reboots, use the sleep command when adding the @reboot string:

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

If you want to create a text file with the system date five minutes after reboot, add:

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

Remove a Reboot Command

Each @reboot string you add to the cron task list runs a job every time Linux restarts. If you no longer wish to run a job, remove it from the task list.

To do this, open the task list using the crontab -e command. Scroll down to the bottom to review the jobs you added.

To remove a task from the list, delete the appropriate line from the appropriate string. Press Control + X to exit Nano, then Y and Enter to save changes.

Note: Learn more about the Linux at command, the alternative for cron job for scheduling jobs.

Conclusion

After following this tutorial, you understand how to use crontab to schedule jobs to run at system reboot.

For more ways to schedule jobs in crontab, check out our guide to setting up cron jobs.

Next, read our article on cron expressions.

Was this article helpful?
YesNo
Aleksandar Kovačević
With a background in both design and writing, Aleksandar Kovacevic aims to bring a fresh perspective to writing for IT, making complicated concepts easy to understand and approach.
Next you should read
Introduction to the Linux File System
October 24, 2024

A file system is a set of processes that controls how, where and when data is stored and retrieved from...
Read more
How to Use the hostname Command in Linux
October 8, 2020

The Linux hostname command lets you view your computers domain, hostname, and IP address. You can also use it...
Read more
How to Create Partitions in Linux
April 25, 2024

In Linux systems, in order to use storage devices such as Hard Drives and USB drives, you need to understand...
Read more
Linux Commands Cheat Sheet: With Examples
November 2, 2023

A list of all the important Linux commands in one place. Find the command you need, whenever you need it or...
Read more