crontab Command: Tutorial & Examples

Schedule a command to be executed at a specified time

The crontab command is a Linux utility that is used to schedule tasks (also known as "cron jobs") to be executed automatically at specified times. Cron is a daemon that runs in the background on a Linux system, and it uses the crontab command to manage a user's set of cron jobs.

The crontab command can be used to create, edit, and delete cron jobs. When creating a new cron job, you specify the schedule at which the job should run, and the command that should be executed. The schedule is specified using a series of fields, including minute (0-59), hour (0-23), day of the month (1-31), month (1-12), and day of the week (0-7, where both 0 and 7 represent Sunday).

The syntax for creating a new cron job is typically crontab -e this will open the crontab file in the editor specified by the VISUAL or EDITOR environment variable, where you can add new cron jobs.

For example, to schedule a job to run every day at 3:30 am, you would use the following line in the crontab file:

30 3 * * * /path/to/command

It's worth noting that the cron daemon only runs the commands under the user that created the cron job, so if you want to run a cron job as root you'll need to use sudo crontab -e and set up the job accordingly.

You can also use the crontab -l command to list the current cron jobs and crontab -r to remove all the cron jobs for the current user.

The /etc/cron.d directory is an alternative to using the crontab command to schedule tasks on a Linux system. Instead of managing cron jobs through individual user accounts, tasks are scheduled in the /etc/cron.d directory. Using the /etc/cron.d directory is simpler in some cases, as it allows multiple users to manage and schedule tasks on the system, and makes it easier to manage and troubleshoot cron jobs, also it is more organized and centralized.

In addition to the /etc/cron.d directory, there are also three other directories in a Linux system that are used to schedule tasks for commonly used intervals: /etc/cron.hourly, /etc/cron.daily, and /etc/cron.weekly.

Except where otherwise noted, content on this site is licensed under a CC BY-SA 4.0 license CC BY SA