banner
三文字

方寸之间

居善地,心善渊,与善仁,言善信,正善治,事善能,动善时。
github
email
misskey

An Analysis of the Cron Command in Linux

Cron is a useful tool in Linux and a favorite among developers because it allows you to automatically run commands using generic scripts and task-specific scripts at specific times, dates, and intervals. With this description, you can imagine how system administrators use it to automate tasks such as backups, directory cleanups, notifications, and more.

Cron jobs run in the background and constantly check the /etc/crontab file, /etc/cron.*/ directories, and /var/spool/cron/ directory. It is best not to edit the cron file directly because each user has a unique crontab.

So how do you create and edit cron jobs? We can use the crontab command. crontab is a method for creating, editing, installing, uninstalling, and listing cron jobs.

The command to create and edit cron jobs is the same and very simple. And the cool thing is, you don't need to restart cron after creating a new file or editing an existing file.

$ crontab -e

Cron Syntax#

Just like using any language, using cron becomes much easier when you understand the syntax of cron. It has two formats:

A B C D E USERNAME /path/to/command arg1 arg2
OR
A B C D E USERNAME /root/backup.sh

Explanation of the above cron syntax:

  • A: Minutes Range: 0 - 59
  • B: Hours Range: 0 - 23
  • C: Days Range: 1 - 31
  • D: Months Range: 1 - 12 or JAN-DEC
  • E: Days of the week Range: 0 - 6 or SUN-SAT, Sunday=0 or 7.
  • USERNAME: Username
  • /path/to/command - Name of the script or command you want to schedule

A more visual representation is:

┌───────────── minute (0 - 59)
│ ┌───────────── hour (0 - 23)
│ │ ┌───────────── day of the month (1 - 31)
│ │ │ ┌───────────── month (1 - 12 or JAN-DEC)
│ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT, Sunday=0 or 7)
│ │ │ │ │
│ │ │ │ │
│ │ │ │ │
* * * * *

In addition, Cron uses 3 operators to specify multiple values in a field:

  1. Asterisk (*): Specifies all possible values for the field, * * * * * runs every minute of every day.
  2. Comma (,): Specifies a list of values, 2,10 4,5 * * * runs at the 2nd and 10th minute of the 4th and 5th hour every day.
  3. Hyphen (-): Specifies a range of values, 0 4-6 * * * runs at the 0th minute of the 4th, 5th, and 6th hour.
  4. Slash (/): Specifies a step value, 20/15 * * * * runs every 15 minutes starting from the 20th minute (i.e., at the 20th, 35th, and 50th minute).

That's about it for the syntax and operators of Cron. Let's now look at some examples.

Cron Job Examples#

The first step to running cron commands is to install crontab using the following command:

#crontab -e

Run /root/backup.sh every day at 3:00 AM:

0 3 * * * /root/backup.sh

Run script.sh every 2nd day of the month at 4:30 PM:

30 16 2 * * /path/to/script.sh

Run /scripts/phpscript.php every weeknight at 10:00 PM:

0 22 * * 1-5 /scripts/phpscript.php

Run perlscript.pl at 23 minutes past midnight, 2 AM, and 4 AM every day:

23 0-23 / 2 * * * /path/to/perlscript.pl

Run a Linux command every Sunday at 04:05:

5 4 * * sun /path/to/linuxcommand

Cron Options#

List cron jobs.

crontab -l
# OR
crontab -u username -l

Remove all crontab jobs.

crontab -r

Remove cron jobs for a specific user.

crontab -r -u username

Strings in Crontab#

Strings are a developer's favorite thing because they help save time by eliminating repetitive writing. Cron has specific strings that can be used to create commands faster:

  1. @hourly: Runs once every hour, equivalent to "0 * * * *"
  2. @midnight: Runs once every day, equivalent to "0 0 * * *"
  3. @daily: Same as midnight
  4. @weekly: Runs once every week, equivalent to "0 0 * * 0"
  5. @monthly: Runs once every month, equivalent to "0 0 1 * *"
  6. @annually: Runs once every year, equivalent to "0 0 1 1 *"
  7. @yearly: Same as @annually
  8. @reboot: Runs once on every startup

For example, this is how you would schedule a daily system backup:

@daily /path/to/backup/script.sh

With this, you have all the information you need to create and manage system tasks using Cron. Now you can start using scheduled commands to set up and maintain multiple environments.

When you have enough understanding of how Crontab works, you can use these handy Crontab Generator utilities to generate crontab lines for free.

Additionally, you can read this article on how to use Cron in Ubuntu.

References#

https://www.tecmint.com/create-and-manage-cron-jobs-on-linux/

https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows


Welcome to my personal blog: 方寸之间

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.