How To Edit The Linux Crontab File To Schedule Jobs

a business man working at office
Kohei Hara / Getty Images

A daemon in Linux called cron runs processes at regular intervals. It checks certain folders on your 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.

Placing Scripts in Cron Folders

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

For example, open a terminal window 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 example, 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.

Permissions

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 appropriate permissions can create their own crontab file. The cron command basically looks for all crontab files and runs through each of them.

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

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/

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

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 have to be careful about setting up your crontab commands.

For example:

* * * 1 * command

This command will run every minute of every hour of every day of every week in January.

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

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