Crontab, get started

Time:2021-3-3

CrontabIt is a time-based task management tool in UNIX system.

This command is different from the traditional UNIX command. The following will introduce its rules and usage one by one.

Crontab or cron

crontabstillcron? Students who first contact crontab may be confused by these two words.

In fact, it can be understood as follows:crontabnamelycronService, andcronIt’s behind the scenescrontabDelivery task service.

file format

The crontab command is used in a fixed time format,

Express meaning minute hour date month week command
Range 0~59(*) 0~23(*) 1~31(*) 1~12(*) 0~7(*) Command to be executed

In addition, some special characters have special meanings

  • *It means to receive at any time. Take chestnuts* 12 * * *It means to execute the specified command no matter what day of the week or month.

Crontab, get started

Common examples

Once per minute:

*/1 * * or * * **

Every five minutes:

*/5 * * * *

Every hour:

0 * * or 0 * / 1 * **

Once a day:

0 0 * *  *

Once a week:

0 0 * *  0

Once a month:

0 0 1 *  0

How to use

first contact crontabI also wonder how this command is used?

There are two ways to use crontab:

  1. Crontab – E: directly accept the commands from the standard input (keyboard) and load them into crontab.
  2. Crontab file: take the file as the task list file of crontab and load it into crontab

There’s nothing to say about the first method. Just add crontab task to the terminal. Let’s talk about the second method (in fact, the core of both methods are the same).

Create crontab file

First, create a file whose contents are expressed inFunction descriptionexecution timeCarry out the taskIt is composed of several parts.

Among them, the first two are not necessarily necessary, just for the convenience of themselves or others to quickly know what this task is doing in the future,#Represents a comment.

Example to create ascript_cronCrontab file for:

#Every minute script.php  script
* * * * * /usr/bin/php ~/script.php

Run crontab

In order to submit the newly created crontab file, you can use the newly created file name as thecrontabParameters of the command:

$ crontab script_cron

List cron services

use-lParameter lists the crontab file:

$ crontab -l
#Every minute script.php  script
* * * * * /usr/bin/php ~/script.php

Edit cron service

$ crontab -e

Delete cron service

$ crontab -r

common problem

Crontab did not take effect immediately

The newly created cron task will not be executed immediately. It will take at least two minutes to execute.

If you want to execute it immediately, you can restart crontab.

// Ubuntu:
$ service cron restart    

// Centos
$ service crond restart

Crontab didn’t do it at all

Sometimes, tasks can be executed directly from the command line, but timing tasks are not executed,

At this time, you need to confirm whether the cron service is normal

// Ubuntu:
$ service cron status    

// Centos
$ service crond status

Then confirm whether the task to be executed contains a path. If so, use the global path.

Finally, restart the cron service. It can normally be executed here. If not, try to introduce environment variables

0 * * * * . /etc/profile; /usr/bin/php /var/www/script.php

Crontab does not have permission to execute

It should be noted that only root and task owner have permission to schedule crontab tasks.

If you want to edit / view / delete tasks of other users, you can use the following command:

$crontab - U < user name > < Options >

Common options:
-e: edit task
-l: view tasks
-r: delete task

View crontab task execution

When a scheduled task is executed at a specified time, a similar log will be output synchronously

$ tail -f /var/log/syslog
Nov 19 12:47:01 gigabit CRON[14521]: (root) CMD (/usr/bin/php /var/www/script.php)

At this point, you can be sure that the task scheduling is normal.

The above way is really effective, but it is not convenient. Is there a better way?

Crontab does not have a log of task execution by default, but it can create a log file manually in other ways.

0 * * * * . /etc/profile; /usr/bin/php /var/www/script.php >> /var/log/cron.log 2>&1 

stayscript.phpAdd an output at the end of the script so that the output will be redirected to thecron.logThe log file is missing.

Reference link