How To Edit The Linux Crontab File To Schedule Jobs

a business man working at office
Kohei Hara / Getty Images


There is a daemon in Linux called cron which is used to run processes at regular intervals.

The way it does this is to check certain folders on your system for scripts to run. For example there is a folder called /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly and /etc/cron.monthly. There is also a file called /etc/crontab.

By default you can simply place scripts into the relevant folders to get them to run a regular intervals.

For example open a terminal window (by pressing CTRL, ALT and T) and run the following ls command:

ls /etc/cron*

You will see the list of programs or scripts that are run hourly, daily, weekly and monthly.

The trouble with these folders is that they are a bit vague. For instance daily means that the script will run once a day but you have no control over the time that the script will run during that day.

That is where the crontab file comes in. 

By editing the crontab file you can get a script or program to run at the exact date and time you want it to run. For instance maybe you want to backup your files every night at 6 pm. 


The crontab command requires that a user has permissions to edit a crontab file. There are basically two files that are used to manage crontab permissions:

  • /etc/cron.allow
  • /etc/cron.deny

If the file /etc/cron.allow exists then the user wanting to edit the crontab file must be in that file.

If the cron.allow file does not exist but there is an /etc/cron.deny file then the user must not exist in that file.

If both files exist then the /etc/cron.allow overrides the /etc/cron.deny file.

If neither file exists then 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.

Editing The Crontab File

Each user who has permissions can create their own crontab file. The cron command basically looks for the existence of multiple crontab files and runs through them all.

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 <yourname>" will appear otherwise your crontab file will be displayed (this functionality differs from system to system, sometimes it displays nothing at all and other times it displays, "do not edit this file").

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

crontab -e

By default if there is no default editor selected then you will be asked to select a default editor to use. Personally I like to use nano as it is fairly straight forward to use and it runs from the terminal.

The file that opens has a lot of information but the key part is the example just 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/

There are 6 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/

What the above command is saying is at 30 minutes, 18 hours and any day, month and day of week run a command to zip and tar the home directory to the /var/backups folder.

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

30 * * * * command

To get a command to run every minute past 6 pm I can run the following command:

* 18 * * * command

You therefore have to be careful about setting up your crontab commands.

For instance:

* * * 1 * command

The above command would run every minute of every hour of every day of every week in January. I doubt that is what you want.

To run a command at 5 am on the 1st January you would at 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 but you might want to remove some rows from the crontab file.

However if you want 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

This asks the question "are you sure?" before removing the crontab file.