08-crond定时任务

本文详细介绍Linux系统中crond定时任务的配置与使用方法,包括crond服务介绍、开机启动优化、crontab命令使用、定时任务配置及调试技巧等关键信息。
1.1 crond定时任务总略图

clip_image002

1.2 定时任务crond介绍
1.2.1 crond是什么?

crond是linux系统用来定期执行命令/脚本或指定程序任务的一种服务或软件。一般情况瞎,我们安装完操作系统后,默认便会有crond软件(服务)。crond是每分钟检查一次。

1.3 开机启动优化

关掉除network、sshd、rsyslog、crond、sysstat之外的服务的开机自启动。

for sun in `chkconfig --list|grep 3:on|awk '{print $1}'`;do chkconfig --level 3 $sun off;done

  1 [root@AIGE ~]# chkconfig |egrep "sshd|rsyslog|sysstat|crond|network" -v |awk '{print "chkconfig "$1" off"}'|bash
  2 
  3 [root@AIGE ~]# chkconfig |egrep "sshd|rsyslog|sysstat|crond|network"
  4 
  5 crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  6 
  7 network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  8 
  9 rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
 10 
 11 sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
 12 
 13 sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
 14 
 15 [root@AIGE ~]# chkconfig |grep "3:on"
 16 
 17 crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
 18 
 19 network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
 20 
 21 rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
 22 
 23 sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
 24 
 25 sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
 26 
 27 [root@AIGE ~]#
 28 
1.3.1 为什么要使用crond定时任务

凌晨/或固定的每天时间的一些任务的开启,毕竟人会忘记事情,机器定时任务是不会忘记的。凌晨人要睡觉,机器也不用睡觉。

1.4 crontab

# 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
# | | | | |
# * * * * * command to be executed

分 时 日 月 周 命令

1.4.1 定时任务文件说明

文件

说明

/etc/cron.deny

该文件列出的用户不能使用crontab命令

/etc/cron.allow

该文件列出的用户允许使用crontab命令,优先于/etc/cron.deny

/var/spool/cron

所有用户crontab配置文件默认都存放在此目录,文件名以用户名命名。

1. 定时任务的配置文件,定时任务规则

2. 文件名就是用户名(root)

1.4.2 crontab命令

参数

含义

指定示例

-l(小写L)

查看crontab文件内容

crontab -l

-e

编辑crontab文件内容

crontab -e

-i

删除crontab文件内容,删除前会提示确认

crontab -ri

-r

删除crontab文件内容

crontab -r

-u user

指定使用的用户执行任务。

crontab -u aige -l

强调:-i -r基本不用。尽量只用-e

crontab -e 实际操作/var/spool/cron里的文件(以用户名命名)

crontab优点:

1. 检查语法

2. 输入方便

1.5 同步时间的定时任务
  1 [root@AIGE ~]# /usr/sbin/ntpdate ntp1.aliyun.com
  2 
  3 31 Jan 22:05:23 ntpdate[4576]: step time server 182.92.12.11 offset 1495885.186225 sec
  4 
  5 [root@AIGE ~]# crontab -e
  6 
  7 no crontab for root - using an empty one
  8 
  9 crontab: installing new crontab
 10 
 11 [root@AIGE ~]# crontab -l
 12 
 13 #date synchronization
 14 
 15 */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com
 16 
 17 [root@AIGE ~]# crontab -e
 18 
 19 crontab: installing new crontab
 20 
 21 You have new mail in /var/spool/mail/root
 22 
 23 [root@AIGE ~]# crontab -l
 24 
 25 #date synchronization
 26 
 27 */5 * * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
 28 
1.6 定时任务八大要领:

要领1:为定时任务罪责加必要的注释
要领2:执行shell脚本任务前加/bin/sh
要领3:定时任务命令或脚本的结尾加 >/dev/null 2>&1
要领4:定时任务命令或程序最好写到脚本里执行
要领5:在指定用户下执行相关定时任务
要领6:生产任务程序不要随意打印输出信息 tar zcf
要领7:定时任务执行的脚本要规范路径(全路径)
要领8:配置定时任务规范操作过程,防止出错

1.7 调试定时任务
1.7.1 增加执行任务的频率调试任务(尽量不用于生产环境)

1天的任务。调成5分钟执行一次。备份数据库是好几个G,你开玩笑吗?

1.7.2 调整系统时间调试(不能用于生产环境)

生产环境的时间是不允许出现差错的。更何况还主动改。

任务提前5分钟,防止不执行。

1.7.3 通过脚本日志输出调试定时任务

>/app/log/test.txt 2>&1

用重定向就行,追加文件会一直变大。

1.8 注意事项

1. path环境变量问题,尽量需要什么环境变量就自己在脚本添加一下。

2. %调试时间问题,把date +%F扔进脚本,问题不复存在。%在crontab中会出现意想不到问题。

3. 权限问题,用普通用户执行脚本时,权限不够,导致服务出现问题。

1487069514839矮哥运维群93324526,欢迎加入谈论

转载于:https://www.cnblogs.com/can-H/articles/6398948.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值