linux定时任务

1.定时任务介绍

1.1 crond是什么

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

[root@shellbiancheng jiaobenlianxi]# cat while1.sh 
#!/bin/sh
while true
   do
    uptime
    sleep 2
   done

1.2 linux系统crond的定时任务

(1)linux系统自身按期执行的任务操做,如轮询系统日志、备份系统数据、清理系统缓存等,这些任务无需咱们人为干预。例如:java

[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点备份站点数据及数据库数据,通常这些工做须要由每一个用户自行设置才行。
用户执行的任务工做,也就是运维管理员执行的任务工做,所以这个用户执行的任务是咱们的重点。node

1.3 linux系统下定时任务软件种类

linux系统下的定时任务还真很多,例如:at,crontab,anacronlinux

at:适合仅执行一次就结束的调度任务命令,例如:某天晚上须要处理一个任务,仅仅是这一天的晚上,属于突发性任务,要执行at命令,还须要启动atd的服务才行shell

[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]windows

2.1 指令说明

经过crontab咱们能够在固定的时间执行指定的系统指令或script脚本。时间间隔的单位是分钟,小时,日,月,周及以上的任意组合(注意:日和周不要组合)。缓存

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

linux定时任务

2.3 指令选项说明表

linux定时任务

2.4 指令的使用格式

用户所创建的crontab文件存于/var/spool/cron中如:root用户的定时任务配置文件为/var/spool/cron/root。bash

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语法格式中时间段得含义

linux定时任务

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

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

linux定时任务

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发现磁盘没满,请问为何?

一、修改密码时报错 passwd: Authentication token manipulation error
  二、添加用户报错: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将输出内容定义到空。这样就不会产生垃圾文件了。

相关文章
相关标签/搜索