How to Edit the Linux Crontab File to Schedule Jobs

Use a crontab file to precisely schedule important commands

What to Know

  • Display the contents of crontab with: crontab -l
  • Edit the crontab with: crontab -e
  • Timing works with: minute, hour, day of month, month, day of week. Use an asterisk (*) to run cron every day, hour, etc.

This article explains how to use cron, which is a daemon in Linux that runs processes at regular intervals. It checks certain folders on a system for scripts to run in a series of folders, including /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, and /etc/cron.monthly; there is also a file called /etc/crontab.

Place Scripts in Cron Folders

Place scripts into the relevant folders to get them to run at regular intervals. For example, open a terminal window and run the following ls command:

ls /etc/cron*

The command lists the programs or scripts that run hourly, daily, weekly, and monthly.

Linux cron list

These folders are vague. For example, daily means that the script runs once a day, but you have no control over the time that the script runs during that day.

That is where the crontab file comes in. By editing the crontab file, you run a script or program at the exact date and time you want it to run.


The crontab command requires that a user has permission to edit a crontab file. There are two files that manage crontab permissions: /etc/cron.allow and /etc/cron.deny.

  • If the file /etc/cron.allow exists, the user who wants to edit the crontab file must be listed in that file.
  • If the cron.allow file doesn't exist, but there is an /etc/cron.deny file, the user must not exist in that file.
  • If both files exist, the /etc/cron.allow file overrides the /etc/cron.deny file.
  • If neither file exists, it depends on the system configuration whether a user can edit the crontab.

The root user can always edit the crontab file. You can either use the su command to switch to the root user or the sudo command to run the crontab command.

Edit the Crontab File

Each user who has appropriate permissions can create a crontab file. The cron command looks for all crontab files and runs through each file.

To check whether you have a crontab file, run the following command:

crontab -l

If you don't have a crontab file, the message no crontab for appears, otherwise your crontab file displays.

This functionality differs from system to system. Sometimes it displays nothing at all, and other times it displays do not edit this file.

Linux crontab list

To create or edit a crontab file, run the following command:

crontab -e

The file that opens has a lot of information, but the key part is the example before the end of the comments section (comments are denoted by lines beginning with #).

# m h dom mon dow command
0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
Linux edit crontab

There are six pieces of information to fit on each line of the crontab file:

  • The minute of the day the command is to run (m).
  • The hour of the day the command is to run (h).
  • The day of the month the command is to run (dom).
  • The month the command is to run (mon).
  • The day of the week the command is to run (dow).
  • The command.

For each item (except for the command), you can specify a wildcard character. Look at the following example crontab line:

30 18 * * * tar -zcf /var/backups/home.tgz /home/

At 30 minutes, 18 hours, and any day, month, and day of the week, this crontab entry will zip and tar the home directory to the /var/backups folder.

In the below examples, replace the word command with the command you want to run.

To get a command to run at 30 minutes past every hour, run the following command:

30 * * * * command

To get a command to run every minute past 6 p.m., run the following command:

* 18 * * * command

You, therefore, must be careful about setting up crontab commands.

For example:

* * * 1 * command

This command runs every minute of every hour of every day of every week in January.

To run a command at 5 a.m. on January 1, enter the following command to the crontab file:

0 5 1 1 * command

How to Remove a Crontab File

Most of the time, you won't want to remove the crontab file. However, you might want to remove some rows from the crontab file.

To remove your user's crontab file, run the following command:

crontab -r

A safer way to do this is to run the following command:

crontab -i

The operating system asks for verification before it removes the crontab file.

Was this page helpful?