Linux/unix timed Task Cron

Source: Internet
Author: User
Tags crontab example mail account crontab syntax

A timed task (cron job) is used to schedule commands that need to be executed periodically. With it, you can configure certain commands or scripts to run periodically over a set period of time. Cron is one of the most useful tools in Linux or Unix-like systems. The Cron Service (daemon) runs in the background of the system and continuously checks the/etc/crontab file and the/etc/cron.*/directory. It will also check the/var/spool/cron/directory.

crontab command

Crontab is the command used to install, uninstall, or list scheduled tasks. The cron configuration file is used to drive the cron (8) Daemon of the Vixie cron. Each user can have their own crontab files, although these files are located in the/var/spool/cron/crontabs directory, but that doesn't mean you can edit them directly. You need to edit or configure your own scheduled tasks via the crontab command.

Types of timing Profiles

The configuration file is divided into the following different types:

    • System-level crontab for UNIX or Linux : This type is typically used by system services and important tasks that require root or similar permissions. The sixth field (described in the field below) is the user name that specifies which user identity this command executes. As a result, the crontab of the system can perform operations as any user.

    • User's crontab: Users can use the crontab command to install their own scheduled tasks. The sixth field is a command that needs to be run, and all commands run as the user who created the crontab task.

Note : The Cron implementation of this question-and-answer form is written by Paul Vixie and is included in many Linux distributions and Unix-like systems, such as the popular version fourth BSD. Its syntax is compatible with the implementation of various crond.

So how do I install, create, or edit my own scheduled tasks?

To edit your crontab file, you need to type the following command at the shell prompt for Linux or Unix:

$ crontab-e

crontab Syntax (field description)

The syntax is:

1 2 3 4 5/path/to/command arg1 arg2


1 2 3 4 5/root/


    • 1th field: minutes (0-59)

    • 2nd field: Hours (0-23)

    • 3rd field: Date (0-31)

    • 4th field: Month (0-12 [12 for December])

    • 5th field: One day of the week (0-7 [7 or 0 for Sunday])

    • /path/to/command– the name of the script or command you plan to execute

Memory-friendly format:

* * * * * to execute the command----------------| | | | | | | | | Day of the----Week (0-7) (Sunday 0 or 7) | | | ------month (1-12) | | --------the day of January (1-31) | ----------hours (0-23)------------minutes (0-59)

A simple crontab example:

# # # Run once every 5 minutes backupscript Script ##*/5 * * * */root/ daily 1 o'clock in the morning run Backupscript script # #0 1 * * */root/backu run Backupscript script at the first 3:15 monthly # #15 3 1 * */root/
How to use Operators

operator allows you to specify multiple values for a field, there are three operators to use:

    • asterisk (*) : This operator specifies all available values for the field. For example, in the hour field, an asterisk is equal to every hour, and in the Month field, an asterisk is equivalent to a month.

    • comma (,) : This operator specifies a list that contains multiple values, for example: 1,5,10,15,20,25.

    • Bar (-) : This operator specifies a range of values, for example: 5-15, equivalent to 5,6,7,8,9,..., 13,14,15 typed with the comma operator.

    • delimiter (/) : This operator specifies a stepping value, for example: 0-23/Can be used in the hour field to specify that a command is executed once per hour. The step value can also follow the asterisk operator, and you can use */2 if you want the command line to be executed every 2 hours.

How to disable message output

By default, the output of a command or script, if any, is sent to your local mailbox account. To stop receiving messages sent by crontab, you need to add >/dev/null 2>&1 to the back of the command you are executing, for example:

0 3 * * */root/ >/dev/null 2>&1

If you want to send the output to a specific mail account, such as [email protected] This mailbox, you need to define a MAILTO variable as follows:

mailto= "[email protected]" 0 3 * * */root/ >/dev/null 2>&1

Visit the "Disable mailtips for Crontab command" to see more information.

Task: List all of your scheduled tasks

Type the following command:

# crontab-l# Crontab-u username-l

To delete all scheduled tasks, you can use the following command:

# # # Delete current Scheduled task Crontab-r
# # # Delete a timed task under a user's name, this command needs to be executed as root crontab-r-u username
Use special strings to save time

You can use one of the following 8 special strings to replace the first five fields, which will not only save you time, but also improve readability.

Special Characters meaning
@reboot Run once on each boot
@yearly Run once a year, equivalent to "0 0 1 1 *".
@annually (Same @yearly)
@monthly Run once a month, equivalent to "0 0 1 * *".
@weekly Run once a week, equivalent to "0 0 * 0".
@daily Run once a day, equivalent to "0 0 * * *".
@midnight (Same @daily)
@hourly Run hourly, equivalent to "0 * * * *".


Run the ntpdate command once per hour
More about the/etc/crontab file and the/etc/cron.d/* directory

/etc/crontab is the system's crontab file. Typically only the root user or daemon is used to configure system-level tasks. Each individual user must use the crontab command to install and edit their own tasks as described above. The/var/spool/cron/or/var/cron/tabs/directory holds the crontab file of the individual user, which should be backed up in the user's home directory.

Understanding the default/etc/crontab file

The typical/etc/crontab file content is this:

shell=/bin/bashpath=/sbin:/bin:/usr/sbin:/usr/binmailto=roothome=/# RUN-PARTS01 * * * * Root run-parts/etc/ CRON.HOURLY02 4 * * * Root Run-parts/etc/cron.daily22 4 * 0 Root run-parts/etc/cron.weekly42 4 1 * * Root run-parts/e Tc/cron.monthly

First, the environment variables must be defined. If the shell line is ignored, Cron uses the default SH SHELL. If the path variable is ignored, there is no default search path, and all files need to be located using an absolute path. If the home variable is ignored, Cron replaces it with the home directory of the caller (user).

In addition, Cron reads the files in the/etc/cron.d/directory. Typically, a system daemon such as Sa-update or Sysstat will store their scheduled tasks here. As the root user or superuser, you can configure your scheduled tasks using the following directory. You can just put the script here. The Run-parts command runs a script or program that is located in a directory through the/etc/crontab file.

Catalogue Description
/etc/cron.d/ Place all the script files here and call them from the/etc/crontab file.
/etc/cron.daily/ Run a script that needs to run once a day
/etc/cron.hourly/ Run scripts that need to run every hour
/etc/cron.monthly/ Run a script that needs to run once a month
/etc/cron.weekly/ Run a script that needs to run once a week
Backup Scheduled Tasks
# crontab-l >/path/to/file# crontab-u user-l >/path/to/file

This article is from the "Ops Boy" blog, please make sure to keep this source

Linux/unix timed Task Cron

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.