设为首页 收藏本站
查看: 1037|回复: 0

[经验分享] linux定时任务

[复制链接]

尚未签到

发表于 2019-2-17 11:46:54 | 显示全部楼层 |阅读模式
1.定时任务介绍

1.1 crond是什么
  crond是linux系统中用来定期执行命令或指定程序的一种服务或软件。
特殊要求:(秒级别)crond服务就无法搞定了,一般工作中写脚本用守护进程执行

[root@shellbiancheng jiaobenlianxi]# cat while1.sh
#!/bin/sh
while true
do
uptime
sleep 2
done
1.2 linux系统crond的定时任务
  (1)linux系统自身定期执行的任务操作,如轮询系统日志、备份系统数据、清理系统缓存等,这些任务无需我们人为干预。例如:

[root@linzhongniao ~]# ls -l /var/log/messages*
-rw-------. 1 root root  206776 Aug  2 17:43 /var/log/messages
-rw-------. 1 root root  448307 Jul  8 08:54 /var/log/messages-20180708
-rw-------. 1 root root  742560 Jul 16 04:05 /var/log/messages-20180716
-rw-------. 1 root root 1293433 Jul 22 15:15 /var/log/messages-20180722
-rw-------. 1 root root  622193 Jul 30 20:14 /var/log/messages-20180730
[root@linzhongniao ~]# ll /etc/|grep cron
-rw-------.  1 root root541 Aug 24  2016 anacrontab
drwxr-xr-x.  2 root root   4096 Jul 16 14:19 cron.d
drwxr-xr-x.  2 root root   4096 Jul 16 14:18 cron.daily
-rw-------.  1 root root  0 Aug 24  2016 cron.deny
drwxr-xr-x.  2 root root   4096 Jul 16 14:19 cron.hourly
drwxr-xr-x.  2 root root   4096 Jun 14 05:01 cron.monthly
-rw-r--r--.  1 root root457 Sep 27  2011 crontab
drwxr-xr-x.  2 root root   4096 Sep 27  2011 cron.weekly
  2)用户执行的任务操作:某个用户或系统管理员定期要做的任务工作,例如每隔5分钟和互联网上时间服务器进行同步,每天晚上0点备份站点数据及数据库数据,一般这些工作需要由每个用户自行设置才行。
用户执行的任务工作,也就是运维管理员执行的任务工作,因此这个用户执行的任务是我们的重点。

1.3 linux系统下定时任务软件种类
  linux系统下的定时任务还真不少,例如:at,crontab,anacron
  at:适合仅执行一次就结束的调度任务命令,例如:某天晚上需要处理一个任务,仅仅是这一天的晚上,属于突发性任务,要执行at命令,还需要启动atd的服务才行

[root@linzhongniao ~]# chkconfig --list|grep atd
atd 0:off   1:off   2:off   3:off   4:off   5:off   6:off
[root@linzhongniao ~]# chkconfig --list atd
atd 0:off   1:off   2:off   3:off   4:off   5:off   6:off
2.定时任务使用说明

[root@linzhongniao ~]# crontab --help
crontab: invalid option -- '-'
crontab: usage error: unrecognized option
usage:  crontab [-u user] file
crontab [-u user] [ -e | -l | -r ]   《==指令语法
(default operation is replace, per 1003.2)
-e  (edit user's crontab)  《==编辑用户的定时任务
-l  (list user's crontab)   《==列出用户的定时任务
-r  (delete user's crontab)   《==删除用户的定时任务
-i  (prompt before deleting user's crontab) 《==在删除用户的crontab之前提示
-s  (selinux context)
  命令格式
  crontab   –u (指定用户默认是root)-[e|l|r]

2.1 指令说明
  通过crontab我们可以在固定的时间执行指定的系统指令或script脚本。时间间隔的单位是分钟,小时,日,月,周及以上的任意组合(注意:日和周不要组合)。

2.2 使用者权限及定时任务文件
DSC0000.png


2.3 指令选项说明表
DSC0001.png


2.4 指令的使用格式
  用户所建立的crontab文件存于/var/spool/cron中如:root用户的定时任务配置文件为/var/spool/cron/root。
  crontab用户的定时任务一般分为6段空格分隔。系统的定时任务则/etc/crontab分为7段,前5段为时间设定段,第六段以哪个用户执行crontab,第七段为所要执行的命令段如下

01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
  系统的crontab文件是/etc/crontab

[root@linzhongniao ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# 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
分 时 日 月 周   用户  任务
2.5 crontab语法格式中时间段得含义
DSC0002.png

  提示:最后一次执行任务的时间是23:30

2.6 crontab语法格式中特殊符号含义
DSC0003.png


2.7 开启定时任务服务

[root@linzhongniao ~]# chkconfig --list crond
crond   0:off   1:off   2:on3:on4:on5:on6:off
[root@linzhongniao ~]# /etc/init.d/crond status
crond (pid  1837) is running...
[root@linzhongniao ~]# ps -ef|grep crond|grep -v grep
root   1837  1  0 21:58 ?00:00:00 crond
[root@linzhongniao ~]# /etc/init.d/crond restart
Stopping crond:   [  OK  ]
Starting crond:[  OK  ]
2.8 编辑定时任务注意事项
  (1)编辑定时任务分钟位上必须用00格式表示
  例如,6月3日上午9:00去培训,规则为

00 09 03 06 *
  (2)周和日不能同时使用
  强调周和日尽量不要同时用,否则可能达不到想要的效果
  例如:
  每周日上午9:30去上课
  30 09 * * 7或者 30 09 * * 0

2.9 服务器时间同步
  (1)手动同步时间

[root@linzhongniao ~]# date
Sat Aug  4 12:08:20 CST 2018
[root@linzhongniao ~]# date -s "23:00"
Sat Aug  4 23:00:00 CST 2018
[root@linzhongniao ~]# date
Sat Aug  4 23:00:03 CST 2018
[root@linzhongniao ~]# which ntpdate
/usr/sbin/ntpdate
[root@linzhongniao ~]# /usr/sbin/ntpdate ntp1.aliyun.com
4 Aug 12:10:21 ntpdate[1700]: step time server 52.163.118.68 offset -39052.961525 sec
[root@linzhongniao ~]# date
Sat Aug  4 12:10:30 CST 2018
  (2)用定时任务自动同步

[root@linzhongniao ~]# crontab -l
#sync sys time by linzhongniao at 2018-08-04
*/2 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
[root@linzhongniao ~]# /etc/init.d/crond restart
Stopping crond:   [  OK  ]
Starting crond:[  OK  ]
[root@linzhongniao ~]# date -s "23:00"
Sat Aug  4 23:00:00 CST 2018
[root@linzhongniao ~]# date
Sat Aug  4 23:00:06 CST 2018
[root@linzhongniao ~]# date
Sat Aug  4 12:21:53 CST 2018   
  机器少还可以和互联网上时间同步,如果有很多服务器,可以搭建一个内部的时间同步服务器ntp server。
  提示:如果不加“>/dev/null 2>&1”会因产生垃圾文件导致磁盘inode耗尽的问题。

3.生产环境crontab专业案例
  每天晚上12点打包站点目录/var/www/html备份到/data目录下(最好每次按时间生成不同的备份包)

[root@linzhongniao scripts]# cat httpd.sh
#!/bin/bash
cd /var/www/
tar zcfp /data/html_$(date +%Y%m%d%H%M).tar.gz ./html
[root@linzhongniao scripts]# crontab -l|tail -2
#tar /var/www/html by shell scripts by linzhongniao at 201808
00 00 * * * /bin/bash /server/scripts/httpd.sh >/dev/null 2>&1
4.书写定时任务5个基本要领

4.1 为定时任务规则加必要的注释
  加必要注释,写定时任务规则时尽可能加上注释(最好是英文注释),这是个好的习惯。

4.2 执行脚本任务前加/bin/sh
  执行定时任务时,如果是执行脚本,请尽量在脚本前面加上/bin/sh命令,否则有可能忘了给脚本加执行权限,而无法完成任务。

4.3 在指定用户下执行相关的定时任务
  需要root权限执行的任务可以登录到root用户下然后设置,如果不需要root权限,可以登录到普通用户下(也可以直接在root下crontab –u linzhongniao –e的写法直接设置)
  切换到linzhongniao用户

[linzhongniao@linzhongniao ~]$ whoami
linzhongniao
[linzhongniao@linzhongniao ~]$ crontab -l
* * * * * /bin/sh tar.sh
  不切换用户直接查看定时任务

[root@linzhongniao ~]# crontab -u linzhongniao -l
* * * * * /bin/sh tar.sh
  看一下crond用户的配置文件

[root@linzhongniao ~]# ll /var/spool/cron/
total 8
-rw-------. 1 root root  25 Aug  4 14:25 linzhongniao
-rw-------. 1 root root 222 Aug  4  2018 root
  平时工作中尽量多用crontab –e和crontab –l去编辑和查看定时任务,因为会有语法错误检查。
如果给1000台服务器同时添加系统时间实时同步,不可能一个一个改,此时就需要批量分发工具或批量运维脚本。

4.4 定时任务结尾加>/dev/null 2>&1
  /dev/null是特殊的设备,表示黑洞设备或空设备;2>&1表示标准错误输出和标准输出的输出的路径都一样。>/dev/null 2>&1相当于1>/dev/null,2>/dev/null

5.系统定时任务配置文件/etc/crontab
  系统定时任务分七段,如果某一台服务器上的用crontab –l查看没有定时任务,就上系统定时任务里面用cat查看。

[root@linzhongniao ~]# cat /etc/crontab
SHELL=/bin/bash   shell解释器
PATH=/sbin:/bin:/usr/sbin:/usr/bin  PATH变量
MAILTO=root  定义如果任务有输出,发给哪个用户默认是root用户
HOME=/
# 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
分 时 日 月 周   用户          脚本
  通过run-parts使得系统可以定时执行目录下的所有可执行文件
  按周执行的配置文件

[root@linzhongniao ~]# tree /etc/cron.weekly
/etc/cron.weekly
0 directories, 0 files
  按天执行的配置文件

[root@linzhongniao ~]# ll /etc/cron.daily/
total 24
-rwx------. 1 root root  180 Jul 10  2003 logrotate
-rwx------. 1 root root  927 Mar 22  2017 makewhatis.cron
-rwx------. 1 root root  189 Jan 26  2015 mlocate.cron
-rwxr-xr-x. 1 root root 2126 Jul 19  2013 prelink
-rwxr-xr-x. 1 root root  563 Nov 23  2013 readahead.cron
-rwxr-xr-x. 1 root root  433 Nov  7  2015 tmpwatch
6.生产场景如何调试crontab定时任务

6.1 增加执行频率调试任务
  在调试时,把任务执行频率调快一些。如:每分钟、每5分钟执行一次,或者比当前时间推迟5分钟以后或者每2钟执行。按己想象的去执行了,如果没问题再改成需要的任务执行的时间。
  强调:有些任务是不允许频繁执行的,例如:定时往数据库里插入数据,这样的任务在测试机上测试好,然后正式线上出现问题的机会就少了。

6.2 调整系统时间调适任务
  用正确的执行任务的时间。设置完成后,可以修改下当前时间,改成任务执行时间的前几分钟来测试(或者重启定时任务服务)如:定时任务9:00执行,我们可以把系统时间改成8:55分,然后观察是不是正确执行了,当前时间要比任务时间提前足够长,只在测试服务器上操作,如果生产服务器不要这样处理。

6.3 通过日志输出调试定时任务
  在脚本中加入日志输出,然后把输出打到指定的日志中,然后观察日志内容结果。看是否执行或正确执行,或向下面的内容把脚本结果重定向到一个log文件里。比如 tar zcvf命令加-v参数,在把输出放到日志里面,通过日志就可以查看脚本有没有执行。

*/2 * * * * /usr/sbin/ntpdate time.windows.com >>/app/ntpdate.log
6.4 注意一些任务执行带来的问题

*/1 * * * * echo “==”>>/tmp/oldboy.log >/dev/null 2>&1
  这是隐蔽的无法执行的任务配置,原因是前面多了一个>>/tmp/oldboy.log,或者去掉>/dev/null 2>&1。

6.5 注意环境变量导致的定时任务故障
  在调试java程序的时候,注意环境变量,要把环境变量的定义追加到脚本里,重新export一下。一般都放在全局变量/etc/profile里面,但是用定时任务执行脚本还需要重新加载一下环境变量。

export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
6.6 通过定时任务日志/var/log/cron调试定时任务

[root@linzhongniao app]# tail -f /var/log/cron
Aug  5 11:01:01 localhost run-parts(/etc/cron.hourly)[2054]: starting 0anacron
Aug  5 11:01:02 localhost anacron[2068]: Anacron started on 2018-08-05
Aug  5 11:01:02 localhost run-parts(/etc/cron.hourly)[2070]: finished 0anacron
Aug  5 11:01:02 localhost anacron[2068]: Will run job `cron.daily' in 34 min.
Aug  5 11:01:02 localhost anacron[2068]: Jobs will be executed sequentially
Aug  5 11:02:01 localhost CROND[2075]: (root) CMD (/usr/sbin/ntpdate time.windows.com >/app/ntpdate.log)
7.生产定时任务注意事项

7.1 export变量问题
  crontab执行shell时只能识别不多的系统环境变量,普通变量是无法识别的。如果在编写的脚本中需要使用变量,最好使用export重新声明一下该变量,脚本才能正常执行。例如生产中和java相关的服务任务和脚本。也可以在脚本中添加PATH环境变量加完PATH环境变量就不用写执行命令全路径了。例如下面的/bin/tar,就可以不写了。

[root@linzhongniao ~]# cat /server/scripts/tar.sh
#!/bin/bash
export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"
cd /server/
/bin/tar zcf backup_$(date +%Y%m%d%H%M).tar.gz ./scripts
7.2 任务路径问题
  一定要用绝对路径不要用相对路径。推荐定时执行脚本。

7.3 脚本权限问题
  要加/bin/sh执行,也可以不在定时任务中用/bin/sh就要给脚本可执行权限。

7.4 时间变量问题
  “%”百分号在crontab任务中认为是newline,需要用“\”转义。crontab任务命令中,如果有“date +%Y%m%d%H%M”(或date +%Y-%m-%d-%H:%M),必须替换为“date +\%Y\%m\%d\%H\%M”,但写在脚本中的“%”百分号就不需要转义了。

7.5 定时任务加注释
  写定时任务要加上注释如:什么人,什么时间,因为谁,做了什么事都要标记清楚如谁与2018-08-01日在http服务器上做了10分钟同步的操作。

7.6 使用脚本程序替代命令
  使用脚本执行任务可以减少错误,提升效率,规范,是个好习惯。

7.7 定时任务脚本的问题
  定时任务脚本中的程序命令尽量用全路径。

8.生产环境定时任务重现生产no space left
  企业inode被填满的企业案例
  问题:修改用户密码和添加用户时出现下面错误,但是用df –h发现磁盘没满,请问为什么?

  1、修改密码时报错 passwd: Authentication token manipulation error
2、添加用户报错:unable to lock password file
  分析思路:查看/etc/passwd和/etc/shadow的文件权限没有问题,再使用命令strace -f passwd 追踪分析原因,看到关键报错信息:“No space left on device”。最后用df -hi查看发现根分区的inode满了。
  解决办法:
  (1 打开邮件服务,打开邮件服务就把邮件目录清空了不要直接删除文件,生产环境邮件服务是不开的。
  (2 在定时任务最后加>/dev/null 2>&1将输出内容定义到空。这样就不会产生垃圾文件了。




运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-673539-1-1.html 上篇帖子: linux基本配置 下篇帖子: Linux系统安装
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表