21, regular tasks

1. Overview of the basic task of the timing

1. What is the crond

crond is scheduled tasks, similar to our usual alarm clock life. Fixed point execution

2. Why use crond

crond mainly to do some periodic tasks, such as: Cold morning 3:00 timed to prepare the data. For example: 11:00 snapped open network interfaces, 12:00 snapped shut down the website interface.

3. Scheduled tasks are divided into the following two use cases:

1. Timing system-level tasks: cleaning up temporary files, system information collection, log files, cut
a scheduled task 2. User level: the timing to synchronize Internet time, regular backup system configuration files, database backup timing data

2. Timing task time management

1.Crontab profile records the time period meaning

[root@xuliangwei    ~]#    vim /etc/crontab
SHELL=/bin/bash 执行命令的解释器
PATH=/sbin:/bin:/usr/sbin:/usr/bin  环境变量
MAILTO=root 邮件发给谁
#   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=0or7) OR sun,mon,tue,wed,thu,fri,sat 星期
#   |   |   |   |   |
#   *   *   *   *   * command to be executed
#   *   表示任意的(分、时、日、月、周)时间都执行
#   -   表示一个时间范围段,如5-7点
#   ,   表示分割时间段,如6,0,4表示周六、日、四
#   /1  表示每隔n单位时间,如*/10 每隔10分钟

2. Understand the preparation of specifications crontab time

00 02   *   *   *   ls  每天的凌晨2点整执行
00 02 1 *   *   ls  每月的1日的凌晨2点整执行
00 02 14 2  *   ls  每年的2月14日凌晨2点整执行
00 02   *   *   7   ls  每周天的凌晨2点整执行
00 02   *   6 5 ls  每年的6月周五凌晨2点整执行
00 02 14    *   7   ls  每月的14日或每周日的凌晨2点整执行
00 02 14 2 7    ls  每月的2月14日或每年2月的周天的凌晨2点整执行  
*/10    02  *   *   *   ls  每天凌晨2点,每隔10分钟执行一次
*   *   *   *   *   ls  每分钟都执行
00 00 14 2  *   ls  每年2月14日的凌晨执行命令
*/5 *   *   *   *   ls  每隔五分钟执行一次
00 02   * 1,5,8 *   ls  每年的1、5、8月凌晨2点执行
00 02 1-8   *   *   ls  每月1-8号凌晨2点执行
0 21    *   *   *   ls  每天晚上21点执行
45 4 1,10,22 *  *   ls  每月1、10、22日的4:45执行
45 4 1-10   *   *   l   每月1-10日的4:45执行
3,15 8-11   */2 *   *   ls  每隔2天的上午8点到11点的第3和第15分钟执行
0 23-7/2    *   *   *   ls  晚上11点到早上7点之间,每隔2小时执行
15 21   *   * 1-5   ls  周一到周五每天晚上21:15执行

3. Use the crontab write cron cron job

parameter meaning
-e Editing a scheduled task
-l View regular tasks
-r To delete a scheduled task
-u Specify a different user

3. Practice writing Scheduled Tasks

1. root user every five minutes time synchronization

1.如何同步时间
[root@xuliangwei    ~]#    ntpdate time.windows.com    &>/dev/null
2.配置定时任务
[root@xuliangwei    ~]#    crontab -e  -u  root
[root@xuliangwei    ~]#    crontab -l  -u  root
*/5 *   *   *   *   ntpdate time.windows.com    &>/dev/null

2. 3,5 point in the afternoon every day, every half hour to perform a sync command

[root@xuliangwei    ~]#    crontab -l
*/30 15,17  *   *   *   sync    &>/dev/null

3. Case: 3:00 every day to do a backup? Backup / etc / directory to / backup / below

1) backup command to write a script
2) requires a daily backup file name format: 2019-05-01_hostname_etc.tar.gz
3) in the implementation of scheduled tasks, do not print task information
4) Backup storage required to retain only the contents of a directory days of data

1.实现如上备份需求
[root@xuliangwei    ~]#    mkdir   /backup
[root@xuliangwei    ~]#    tar zcf $(date	+%F)_$(hostname)_etc.tar.gz  /etc
[root@xuliangwei    ~]#    find    /backup -name   “*.tar.gz”  -mtime  +3  -exec   rm  -f  {}\;
2.将命令写入至一个文件中
[root@xuliangwei    ~]#    vim /root/back.sh
mkdir   /backup
tar zcf $(date	+%F)_$(hostname)_etc.tar.gz  /etc
find    /backup -name   “*.tar.gz”  -mtime  +3  -exec   rm  -f  {}\;
3.配置定时任务
[root@xuliangwei    ~]#    crontab -l
00 03   *   *   *   bash    /root/back.sh   &>/dev/null
4.备份脚本

Note that matters 4.crond

1) to the timer task Note
2) The need to perform the task of periodically writing a shell script, to avoid direct use of the command execution of tar data
end 3) the timing of the task must be &> / dev / null or appending the results redirect >> / tmp / data.log file
4) Note that some echo command is not successful implementation of "123" >> / tmp / test.log &> / dev / null
5) If you must use the command, the command must be used absolute path

How to back up 5.crond

1) performed by finding the recording / var / log / cron is, the estimated time to perform the task
2) Backup timer / var / spool / cron / { username}

How to reject a user 6.crond

1.使用root将需要拒绝的用户加入/etc/cron.deny
[root@xuliangwei    ~]#    echo    "xuliangwei"    >>  /etc/cron.deny
2.登录该普通用户,测试是否能编写定时任务
[oldboy@xuliangwei  ~]$   crontab -e
You (xuliangwei)    are not allowed to  use this    program (crontab)
See crontab(1)  for more    information

4. How does regular tasks debugging

1.crond debugging

 1) 调整任务每分钟执行的频率, 以便做后续的调试。
 2) 如果使用cron运行脚本,请将脚本执行的结果写入指定日志文件, 观察日志内容是否正常。
 3) 命令使用绝对路径, 防止无法找到命令导致定时任务执行产生故障。
 4) 通过查看/var/log/cron日志,以便检查我们执行的结果,方便进行调试。

Writing ideas 2.crond

 1.手动执行命令,然后保留执行成功的结果。
 2.编写脚本
  脚本需要统一路径/scripts
  脚本内容复制执行成功的命令(减少每个环节出错几率)
                脚本内容尽可能的优化,使用一些变量或者简单的判断语句
                脚本执行的输出信息可以重定向到其他位置保留或写入/dev/null
 3.执行脚本
  使用bash命令执行, 防止脚本没有增加执行权限(/usr/bin/bash /bin/bash)
  执行脚本成功后,复制该执行的命令,以便写入cron
 4.编写计划任务
  加上必要的注释信息, 人、时间、任务
  设定计划任务执行的周期
  粘贴执行脚本的命令(不要手敲)
 5.调试计划任务
  增加任务频率测试
  检查环境变量问题
  检查crond服务日志




Guess you like

Origin www.cnblogs.com/Forever-x/p/25b159dcf3a402e22d2e772bb6da8744.html