[linux] crontab command use


crontab command parameter description

crontab [-u username] [-l|-e|-r]
options and parameters:
-u: only root can perform this task, that is, help other users create/remove crontab job schedule;
-e: edit crontab
-l: check crontab's work content -r
: remove all crontab's work content, if you only want to remove one item, please use -e to edit


crontab command format description

  • The time format is as follows:

f1 f2 f3 f4 f5 program

  • Among them, f1 represents the minute, f2 represents the hour, f3 represents the day of the month, f4 represents the month, and f5 represents the day of the week.
  • When f1 represents *, it means that the program should be executed every minute, when f2 is *, it means that the program should be executed every hour, and so on.
  • When f1 represents ab, it means that it will be executed from the ath minute to the bth minute, and when f2 is ab, it means that it will be executed from the ath to the bth hour, and so on.
  • When f1 is */n, it means that it is executed every n minutes, f2 is */n, it means that it is executed every n hours, and so on.
  • When f1 is a, b, c, ..., it means that it will be executed in a, b, c, ... minutes, and when f2 is a, b, c, ..., it means that it will be executed in a, b, c, ... hours, so that analogy.

The format of each job (each line) has six fields, and the meaning of the six fields is

representative meaning minute Hour date (day) month week Order
range of numbers 0-59 0-23 1-31 1-12 0-7 Order
Special characters representative meaning
* represents any time
. represents the separation period
- represents a period of time
/n n represents a number, that is, every n unit interval
	*    *    *    *    *
	-    -    -    -    -
	|    |    |    |    |
	|    |    |    |    +----- 星期中星期几 (0 - 6) (星期天 为0)
	|    |    |    +---------- 月份 (1 - 12) 
	|    |    +--------------- 一个月中的第几天 (1 - 31)
	|    +-------------------- 小时 (0 - 23)
	+------------------------- 分钟 (0 - 59)

for example

43 21 * * *        21:43 执行
15 05 * * *        05:15 执行
0 17 * * *         17:00 执行
0 17 * * 1         每周一的 17:00 执行
0,10 17 * * 0,2,3  每周日,周二,周三的 17:0017:10 执行
0-10 17 1 * *      毎月1日从 17:0017:10 毎隔1分钟 执行
0 0 1,15 * 1       毎月1日和15日和周一的 0:00 执行
42 4 1 * *         毎月1日的 4:42分 执行
0 21 * * 1-6       周一到周六 21:00 执行
0,10,20,30,40,50 * * * * 每隔10分 执行
*/10 * * * *       每隔10分 执行
* 1 * * *1:01:59 每隔1分钟 执行
0 1 * * *          1:00 执行
0 */1 * * *        毎时0分 每隔1小时 执行
0 * * * *          毎时0分 每隔1小时 执行
2 8-20/3 * * *     8:02,11:02,14:02,17:02,20:02 执行
30 5 1,15 * *      1日和15日的 5:30 执行

Note: Week and day and month cannot coexist at the same time.
"You can use week or day and month as the cycle, but you cannot use the mode of "what is the month and day and the day of the week"".

crontab log path

# 【日志是按照天排列的】
[wqf@b1i10 ~]$ ll /var/log/cron*
-rwxrwxr-x 1 root root 1354975 Apr 25 18:20 /var/log/cron
-rwxrwxr-x 1 root root 3323417 Apr  2 03:21 /var/log/cron-20230402
-rwxrwxr-x 1 root root 3516278 Apr  9 03:43 /var/log/cron-20230409
-rwxrwxr-x 1 root root 3671741 Apr 16 03:37 /var/log/cron-20230416
-rwxrwxr-x 1 root root 3671864 Apr 23 03:47 /var/log/cron-20230423

/var/log/cron will only record whether certain planned scripts are executed, but Linux will send emails to the user every time whether the specific execution is correct and some information during the script execution process.

Precautions for crontab

1. Pay attention to the environment variable problem

crontab does not recognize Java environment variables.
If we use crontab to execute the script regularly, it cannot be executed, but it can be executed normally through commands (such as: ./test.sh), mainly because the environment variables cannot be read.

Solution:

  • 1. All file paths involved in the script need to be written in absolute path form, such as: /usr/local/bin/docker.

  • 2. Use the following code at the beginning of the shell script:

#!/bin/sh
. /etc/profile 
或者 
source /etc/profile   ##此文件为系统的每个用户设置环境信息。source在当前bash环境下执行命令。


. ~/.bash_profile ##在登录时用到的第三个文件是.profile文件,每个用户都可使用该文件输入专用于自己使用的shell信息,当用户登录时,该文件仅仅执行一次,默认情况下,设置一些环境变量,执行用户的.bashrc文件。
或者 
export RUN_CONF=/home/d139/conf/platform/cbp/cbp_jboss.conf
/usr/local/jboss-4.0.5/bin/run.sh -c mev & ##设置环境变量
  • 3. Add environment variables in /etc/crontab, and add the command ./etc/profile;/bin/sh before the executable command to make the environment variables take effect, for example:
20 03 * * * . /etc/profile;/bin/sh /var/www/runoob/test.sh

Summary: When crontab executes the shell, it can only recognize a few environment variables, and ordinary environment variables cannot be recognized. Therefore, when writing a shell, it is best to use export to redeclare variables to ensure script execution.

2. System task scheduling and user task scheduling

The task scheduling operation of the root user can be set through "crontab -uroot -e", or the scheduling task can be directly written into the /etc/crontab file. It should be noted that if you want to define a task to restart the system at a fixed time, you must set The task is placed in the /etc/crontab file, even if a task of restarting the system is created under the root user, it is invalid.

3. Other precautions

1. Time variable: % has a special meaning in crontab, which means newline. If you want to use it, you must escape %. For example, the frequently used date "+%Y%m%d" will not be executed in crontab. It should be replaced with date "+%Y%m%d"

Solution:

## date "+%Y%m%d"在crontab不执行
59 8-23 * * * nohup sh /apps/summary_fz_province/cdc_model/bin/m568_model_2/m568_model2.sh > /apps/summary_fz_province/cdc_model/log/m568_model_2/m568_model_$(date "+%Y%m%d%H").log 2>&1 &


## 换成date "+\%Y\%m\%d"
59 8-23 * * * nohup sh /apps/summary_fz_province/cdc_model/bin/m568_model_2/m568_model2.sh > /apps/summary_fz_province/cdc_model/log/m568_model_2/m568_model_$(date "+\%Y\%m\%d\%H").log 2>&1 &

2. When crontab suddenly fails, you can try /etc/init.d/crond restart to solve the problem. Or check the log to see if a job has been executed/reported an error tail -f /var/log/cron.
3. Do not run crontab -r indiscriminately. It deletes the user's Crontab file from the Crontab directory (/var/spool/cron). All crontabs for that user are gone.
4. Remember to add comments to the timed tasks

Guess you like

Origin blog.csdn.net/sodaloveer/article/details/130368383