Linux Crontab and management with salt

I. Introduction:

  Recently, I accidentally saw that Salt has a cron module. Today, I will introduce linux crontab and the management of crontab through salt's cron.

Second, the introduction of Linux crontab:

  crontab is used to set the command to be executed periodically. This command reads instructions from the standard input device and stores them in the "crontab" file for later reading and execution. The instructions stored in crontab are activated by the daemon, and crond often runs in the background, checking every minute if there are scheduled jobs to execute.

2.1, crond startup and shutdown:

copy code
#View crond status 
[root@A01-R07-I165-88 ~] # service crond status 
#Close crond 
[root@A01-R07-I165-88 ~] # service crond stop 
#Start crond 
[root@A01-R07- I165-88 ~] # service crond start #Restart crond 
[root@A01-R07-I165-88 ~] 
# service crond restart 
#Reload crond [root@A01-R07- I165-88 
~] # service crond reload
copy code

2.2, all configuration files

  Crontab has five directories, cron.hourly, cron.daily, cron.weekly, cron.monthly, cron.d and two files crontab and cron.deny under the /etc directory.

  cron.daily is a Job that is executed once a day;

  cron.weekly is a Job that is executed once every week;

  cron.monthly is a Job that is executed once a month;

  cron.hourly is a Job that executes every hour;

  cron.d is a task that the system needs to do on a regular basis, but it is not executed on an hourly, daily, weekly, or monthly basis.

  The contents of /etc/crontab are as follows:

copy code
[root@A01-R07-I165-88 ~]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
copy code

  The /etc/cron.deny file is used to control which users are not allowed to use crontab.

2.3. User profile:

  Each user has his own cron configuration file, which can be edited through crontab -e. Generally, after we edit the user's cron configuration file, save it and exit, the system will automatically store it in the /var/spool/cron/ directory. , the file is named after the username.

  Linux's cron service reads everything under /var/spool/cron/, /etc/crontab, and /etc/cron.d every minute.

2.4, cron command format:

crontab [-u user] file
crontab [ -u user ] { -l | -r | -e }
-u:指定某一用户
-e:执行文字编辑器来设定用户(当前用户或指定用户)时程表,内定的文字编辑器是vi.
-r:删除用户时程表.
-l:列出用户时程表.

2.5、cron文件格式:

copy code
*  *  *  *  *  command
分 时 日 月 周   命令
第1列表示分钟1~59, 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
copy code

2.6、特殊用户:

[root@A01-R07-I165-88 cron]# crontab -l
# Lines below here are managed by Salt, do not edit
@reboot /root/cgroup_mkdir.sh

  @reboot是指在开机后运行,且只运行一次,效果跟设置在/etc/rc.local中一样。

另外还有:

copy code
string            meaning 
------           ------- 
@reboot        Run once, at startup. 
@yearly         Run once a year, "0 0 1 1 *". 
@annually      (same as @yearly) 
@monthly       Run once a month, "0 0 1 * *". 
@weekly        Run once a week, "0 0 * * 0". 
@daily           Run once a day, "0 0 * * *". 
@midnight      (same as @daily) 
@hourly         Run once an hour, "0 * * * *".
copy code

2.7、每10秒运行一次:

三、通过salt来管理crontab:

copy code
新增一条记录:
salt -L '*' cron.set_job yarn '10' '9' '1' '*' '*' 'source /home/yarn/.bashrc;find /sys/fs/cgroup/cpu/hadoop-yarn -mtime +15 -name "container_*" -exec rm -rf {} \;' "rm the container files"
查看记录:
salt '*' cron.raw_cron yarn
删除记录:
salt '*' cron.rm_job yarn 'source /home/yarn/.bashrc;find /sys/fs/cgroup/cpu/hadoop-yarn -mtime +15 -name "container_*" -exec rm -rf {} \;' minute='10'
copy code

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326170317&siteId=291194637