How to use cron job in Linux system

Source: Internet
Author: User

Cron is a Daemon,cron job for Linux that is a task that Cron is scheduled to perform. Cron uses a special configuration file, the crontab file, to set the execution time or frequency of a command or script. Crontab has previously been introduced to the basics of use, no longer detailed.
Re-explain the command format for the next crontab

#┌─────────────min (0-59)
#│┌──────────────hour (0-23)
#││┌───────────────day of month (1-31)
#│││┌────────────────month (1-12)
#││││┌─────────────────day of Week (0-6) (0 to 6 are Sunday to Saturday, or with names; 7 is Sunday, the same as 0)
#│││││
#│││││
# * * * * command to execute

About the first five parameters date and time expression:
Denotes any: * number means "arbitrary" (first-last).
Specifies a number that represents the specified time.
Specify a segment that represents the number contained in start-end: For example, 3-6, which means 3,4,5,6
Specify the list: for example, "1,2,3,4″," 0-4,8-12″
Specify "Step size": 8-14/2 indicates 8,10,12,14

And look at the following crontab expression

0 0 5-20/5 2 2/path/to/command

From February 5 to 20th, every 5 days plus all Tuesday of February, the visible month and week parameters have specified values (non *), they are or.

In addition, some special predefined expressions for crontab parameters are:

@yearly, @annually Run once a year at midnight of January 1 (0 0 1 1 *)
@monthly Run Once a month, at midnight of the the the month (0 0 1 * *)
@weekly Run once a week at midnight of Sunday (0 0 * * 0)
@daily Run once a day at midnight (0 0 * * *)
@hourly Run at the beginning of every hour (0 * * * * *)
@reboot Run once at startup

You can also execute more than one command, separated by;

* * * * * * */PATH/TO/COMMAND-1; /path/to/command-2

If there is a dependency between the commands, you can use the logical operator &&, as follows if the first command fails, the second command does not execute.

* * * * * */path/to/command-1 &&/path/to/command-2

Contab can set redirect output

* * * * * */path/to/php/path/to/the/command >>/var/log/cron.log

You can also prevent cron from sending messages

* * * * * * */path/to/php/path/to/the/command >/dev/null 2>&1

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: info-contact@alibabacloud.com 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.