crontab – Submit and manage user tasks that need to be executed periodically

The crontab command is used to submit and manage user tasks that need to be executed periodically. These tasks are commonly known as “cron jobs.” Cron jobs are used to automate repetitive tasks, such as backups, system updates, and other administrative tasks. The cron daemon, which is a background process, reads the crontab files and executes the commands at the specified times.

Overview

Syntax

The basic syntax for the crontab command is as follows:

crontab [options] [file]

The options argument specifies the operation to perform on the crontab file, and the file argument specifies the name of the crontab file to use. If the file argument is omitted, the default crontab file for the current user is used.

Creating a new crontab file

To create a new crontab file, use the -e option:

crontab -e

This will open the default crontab file in the default text editor. You can then add your cron jobs to the file. Each line in the file represents a single cron job, and has the following format:

* * * * * command

The five asterisks represent the time and date when the command should be executed. The format is as follows:

  • Minute (0-59)
  • Hour (0-23)
  • Day of the month (1-31)
  • Month (1-12)
  • Day of the week (0-6, where 0 is Sunday)

The command is the command to be executed.

Editing an existing crontab file

To edit an existing crontab file, use the -e option with the -u option to specify the user:

crontab -u username -e

This will open the specified user’s crontab file in the default text editor.

Listing crontab files

To list the contents of a crontab file, use the -l option:

crontab -l

This will list the contents of the default crontab file for the current user.

Removing a crontab file

To remove a crontab file, use the -r option:

crontab -r

This will remove the default crontab file for the current user.

Specific use cases

Here are some specific use cases for the crontab command:

  • Schedule a backup script to run every night at midnight: 0 0 * * * /path/to/backup_script.sh
  • Schedule a system update every Sunday at 3am: 0 3 * * 0 apt-get update && apt-get upgrade -y
  • Schedule a script to run every 30 minutes: */30 * * * * /path/to/script.sh

Options

Here are the available options for the crontab command:

Option Description
-e Edit the current user’s crontab file
-l List the current user’s crontab file
-r Remove the current user’s crontab file
-u Specify a user’s crontab file to edit or list

Troubleshooting tips

Here are some troubleshooting tips for the crontab command:

  • Make sure your crontab file has the correct permissions. It should be owned by the user and have a mode of 0600.
  • Check the system log files for any errors related to the cron daemon.
  • Make sure the commands in your crontab file are correct and executable.

Notes

  • The cron daemon reads the crontab files every minute, so it may take up to a minute for a new or updated cron job to take effect.
  • The crontab files are stored in the /var/spool/cron/crontabs directory.