Linux_定时任务

定时任务概念
  :相当于生活中的闹钟 设定好时间 定时执行任务【备份..】

分类
1. linux系统自身定期执行的任务工作
/etc/crontab

# 系统的日志
[root@localhost oldboy]# ls -l /var/log/messages*
-rw------- 1 root root 1763775 9月 18 12:01 /var/log/messages
-rw-------. 1 root root 5041782 9月 15 09:21 /var/log/messages-20190915

# 用户登录日志:
[root@localhost cron.daily]# ls -l /var/log/secure*
-rw------- 1 root root 35567 9月 18 21:52 /var/log/secure
-rw-------. 1 root root 83447 9月 15 08:46 /var/log/secure-20190915
#
[root@localhost oldboy]# ll /etc|grep cron
-rw-------. 1 root root 541 4月 11 2018 anacrontab
drwxr-xr-x. 2 root root 54 9月 8 00:05 cron.d
drwxr-xr-x. 2 root root 57 9月 8 00:05 cron.daily
-rw-------. 1 root root 0 4月 11 2018 cron.deny
drwxr-xr-x. 2 root root 41 9月 8 00:05 cron.hourly
drwxr-xr-x. 2 root root 6 6月 10 2014 cron.monthly
-rw-r--r--. 1 root root 451 6月 10 2014 crontab
drwxr-xr-x. 2 root root 6 6月 10 2014 cron.weekly


[root@localhost oldboy]# vi /etc/logrotate.conf

# see "man logrotate" for details
# rotate log files weekly
weekly

【保留4周的记录】
# keep 4 weeks worth of backlogs
rotate 4

# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext


# create new (empty) log files after rotating old ones
create

# use date as a suffix of the rotated file
dateext

# uncomment this if you want your log files compressed
#compress


【RPM包将日志轮询信息放入此目录】
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d


【没有拥有wtmp和btmp的包--我们将在这里轮询它们】
【做定制化】
# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}

/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}

# system-specific logs may be also be configured here.

2.用户执行的任务工作(安装系统基础优化)
#服务器时间同步



【cronie定时任务使用方法】
crontab : 单用户的定时任务指令

crontab [-u user] file :【指定使用的用执行任务】
crontab [-u user] [-l 【list】| -r 【remove】 | -e【edit】] [-i【delete】] [-s]
crontab -n [ hostname ]
crontab -c

root用户:
crontab -e(文件语法检查功能) === vi /etc/spool/cron/root :编写定时任务
crontab -i === cat /etc/spool/cron/root :查看定时任务编写信息

【crontab{-l|-e}:实际上就是在操作 /var/spool/cron/ 当前用户这样的文件】


【定时任务两个文件】
/var/log/cron ----> 定时任务日志文件

/var/spool/cron/用户名称(root oldboy) -- 定时任务配置文件



01: 产生日志时间信息
02:系统主机名称
03:触发定时任务操作信息:anacron crond crontab(编辑)
04:具体完成什么任务




【查看定时任务是否运行】



【查看crond是否开机自启动】

查看进程号】

【重启定时任务】

【Contes6:/etc/init.d/crond restart】
[root@localhost oldboy]# systemctl restart crond

【查看状态】

 

# * * * * * 【cmd】user-name command to be executed
分 时 日 月 周 任务
【*】:代表任意时间 00 23 * * * cmd
【-】:代表时间范围 00 17-19 * * * cmd 
【,】:分割时段 00 17,18,19 * * * cmd / 30 3-5, 17-19 * * * cmd
【/n】:按n的倍数 按照n的倍数进行执行 ***

 

 


【错误写法】:* 23,00-07/1 * * * cmd 注意:每小时的时候 前面不能有*

【周和日 尽量不要同时用,否则可能达不到想要的效果】 【00 11 * 4 1-3 cmd】

【工作中调试定时任务的方法:】
1.增加执行任务频率调试任务
每分钟或者系统时间之后 5 分钟执行,看看结果对不对
某些任务不用用于生产环境没有测试机会

【代码,配置变更发布流程】

个人开发环境-->办公测试环境-->IDC机房测试环境-->IDC正式环境(分组,灰度发布)
2.调整系统时间调试任务(不能直接用于生产环境),保持5分钟
3.通过脚本日志输出调试定时任务
4.注意一些任务命令带来的问题
*/1 * * * * echo "==" >> /tmp/oldboy.log > /dev/null 2>&1 【X】:两次的标准化输出
*/1 * * * * tar zcf /tmp/oldboy_$(date +%F).tar.gz /etc/hosts 【X】:百分号转义(脚本无需转义)

5.【注意】:环境变量导致的定时任务故障(java)
6. 通过crond定时任务服务日志调试定时任务(/var/log/cron)
7.其他稀奇古怪的问题调试的办法
8 sh- x 调试脚本

【编写定时任务常见错误:】
1). 定时任务中命令信息建议使用绝对路径, 因为识别环境变量和系统中默认环境变量不一致
定时任务识别的PATH环境变量信息: /usr/bin:/bin

练习: 编写定时任务每隔5分钟,进行时间同步操作 ntpdate
方式一: 错误方式
*/5 * * * * ntpdate ntp1.aliyun.com
方式二: 正确方式
*/5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com

2). 定时任务中执行的命令有些特殊符号无法识别
练习: 每分钟编写定时任务, 进行数据备份 cp /etc/hosts /backup/hosts_时间信息.txt
方式一: 错误方式
* * * * * /usr/bin/cp /etc/hosts /backup/hosts_$(date +%F).txt
方式二: 正确方式
* * * * * /usr/bin/cp /etc/hosts /backup/hosts_$(date +\%F).txt
方式三: 将定时任务编写成脚本
[root@oldboy scripts]# cat /server/scripts/backup.sh
#!/bin/bash

/usr/bin/cp /etc/hosts /backup/hosts_$(date +%F).txt

3). 当定时任务出现错误信息或者有输出信息, 导致占用block 占用inode
postfix服务开启: /var/spool/mail/root 不断增大 占用block
postfix服务关闭: /var/spool/postfix/maildrop/大量小文件 占用inode

* * * * * /usr/sbin/ntpdata ntp1.aliyun.com &>/dev/null
* * * * * /usr/sbin/ntpdata ntp1.aliyun.com >/dev/null 2>&1

4). 完成一个任务步骤比较多, 建议将多个步骤编写成一个脚本执行
* * * * * /bin/sh /server/scripts/test.sh &>/dev/null

5). 将每个定时任务设置注释说明信息

猜你喜欢

转载自www.cnblogs.com/zhanghongqi/p/11642561.html
今日推荐