crontab

1.1 crond定义 php

crond 按期执行命令,服务或软件,默认每分钟检查一次,如同咱们的生活中的闹钟。定时任务 也叫计划任务 html

【知识点回顾】 java

程序:程序代码组成,可是没有在计算机内执行,当前没有执行。 node

进程:计算机正在执行的程序 面试

守护进程:一直运行的程序 shell

【精简开机启动操做】 数据库

[root@Songge ~]# chkconfig --list |grep '3:on' vim

crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off 缓存

network 0:off 1:off 2:on 3:on 4:on 5:on 6:off 安全

rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off

sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off

sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off

4.1.2 定时任务存在的意义

数据库或者代码程序,须要晚上0点作一次全备,天天执行周期性的工做,咱们就须要Linux系统的定时任务crond,实时备份,定时备份,时间同步。

4.1.3 Linux系统crond 的定时任务

* Linux 系统自身按期执行的任务工做,如轮询系统日志,备份系统数据,清理系统缓存。这些任务无需咱们人为干预。

[root@Songge ~]# ls -l /var/log/messages*

-rw------- 1 root root 485799 May 30 22:28 /var/log/messages

-rw-------. 1 root root 594171 May 4 13:12 /var/log/messages-20160504

-rw------- 1 root root 494732 May 19 21:25 /var/log/messages-20160519

-rw------- 1 root root 391369 May 22 14:42 /var/log/messages-20160522

-rw------- 1 root root 1177379 May 29 07:03 /var/log/messages-20160529

[root@oldboyedu data]# ll /var/log/secure*

-rw------- 1 root root 18731 May 28 21:56 /var/log/secure

-rw-------. 1 root root 7075 May 4 13:13 /var/log/secure-20160504

-rw------- 1 root root 7745 May 19 20:36 /var/log/secure-20160519

-rw------- 1 root root 4905 May 22 14:45 /var/log/secure-20160522

[root@Songge logrotate.d]# cd /etc/logrotate.d;ll

total 16

-rw-r--r--. 1 root root 139 Jul 24 2015 dracut

-rw-r--r--. 1 root root 329 Jul 17 2012 psacct

-rw-r--r--. 1 root root 210 Dec 10 2014 syslog

-rw-r--r--. 1 root root 87 Jul 24 2015 yum

查看系统自带任务内容 syslog

[root@Songge logrotate.d]# cat syslog

/var/log/cron

/var/log/maillog

/var/log/messages

/var/log/secure

/var/log/spooler

{

sharedscripts

postrotate

/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true

endscript

}

本身添加时间同步任务:

[root@oldboyedu data]# crontab -l

#time sync by oldboy at 2010-2-1

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

Linux系统下定时任务的命令 有crontab at anacron

crontab 能够周期性的执行任务工做

at 适合执行一次就结束的调度任务命令

anacron 适用于非7*24小时开机服务器准备的。

提示:1. crond 服务是运行的程序

contab 命令用户用来设置定时规则的命令

2. crond 服务是企业生产工做中经常使用的重要服务,at 和 anacron不多使用。

3.几乎每一个服务器都会用到crond 服务

4.2 定时任务crond 使用说明

命令crontab -l 罗列编辑任务信息

-e 编辑定时任务

*经过查看系统下日志,发现不少任务都是自带时间格式

[root@Songge ~]# cd /var/log;ls

anaconda.ifcfg.log maillog-20160522

anaconda.log maillog-20160529

anaconda.program.log messages

anaconda.storage.log messages-20160504

anaconda.syslog messages-20160519

anaconda.xlog messages-20160522

anaconda.yum.log messages-20160529

同理查看系统的任务日志

[root@Songge log]# ll -h cron*

-rw------- 1 root root 51K May 31 10:30 cron

-rw-------. 1 root root 8.7K May 4 13:44 cron-20160504

-rw------- 1 root root 13K May 19 21:26 cron-20160519

-rw------- 1 root root 51K May 22 15:45 cron-20160522

-rw------- 1 root root 147K May 29 08:09 cron-20160529

查看系统的天天 每日 每个月 任务日志

[root@Songge log]# ls -l /etc/|grep cron

-rw-------. 1 root root 541 Mar 30 2015 anacrontab

drwxr-xr-x. 2 root root 4096 Apr 21 12:35 cron.d

drwxr-xr-x. 2 root root 4096 Apr 21 12:35 cron.daily

-rw-------. 1 root root 0 Mar 30 2015 cron.deny

drwxr-xr-x. 2 root root 4096 Apr 21 12:34 cron.hourly

drwxr-xr-x. 2 root root 4096 Apr 21 12:35 cron.monthly

-rw-r--r--. 1 root root 457 Sep 27 2011 crontab

drwxr-xr-x. 2 root root 4096 Sep 27 2011 cron.weekly

查找以上文件所处路径

[root@Songge log]# find / -type f -name "cron.weekly"

/var/spool/anacron/cron.weekly

[root@Songge log]# find / -type f -name "crontab"

/etc/crontab

/usr/bin/crontab

[root@Songge log]# find / -type f -name "anacrontab"

/etc/anacrontab

#有的备注在etc 配置文件里

[root@Songge log]# ls -hild /usr/bin/crontab

18291 -rwsr-xr-x. 1 root root 51K Mar 30 2015 /usr/bin/crontab

#suid

[root@Songge log]# ls -hild /etc/crontab

142740 -rw-r--r--. 1 root root 457 Sep 27 2011 /etc/crontab

anacrontab就是系统计划任务的扩展文件:在一个指定时间间隔错事后自动执行任务 这个是系统设置好了,清理系统垃圾或者是自动执行某些脚本的系统任务,不要更改 配置文件是/etc/anaconrtab

[root@Songge log]# cat /etc/anacrontab

# /etc/anacrontab: configuration file for anacron

# See anacron(8) and anacrontab(5) for details.

SHELL=/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

# the maximal random delay added to the base delay of the jobs

RANDOM_DELAY=45

# the jobs will be started during the following hours only

START_HOURS_RANGE=3-22

#period in days delay in minutes job-identifier command

1 5 cron.daily nice run-parts /etc/cron.daily

7 25 cron.weekly nice run-parts /etc/cron.weekly

@monthly 45 cron.monthly nice run-parts /etc/cron.monthly

日志切割(系统切割的配置文件,里边内容会调用脚本)

[root@Songge log]# cat /etc/logrotate.d/syslog

/var/log/cron

/var/log/maillog

/var/log/messages

/var/log/secure

/var/log/spooler

{

sharedscripts

postrotate

/bin/kill -HUP `cat /var/run/syslogd.pid 2> /dev/null` 2> /dev/null || true

endscript

}

4.2.1 指令语法

crontab 【-u user(为哪一个用户)】 {-e |-l|-r}

#以上为系统的定时任务,须要添加用户

咱们自带的定时任务不须要用户

4.2.2 指令说明

* * * * * * 分时日月周 crond 服务经过crontab 命令实现

4.2.3 经常使用的几个配置文件

/etc/cron.deny 文件中所列用户禁止使用crontab命令#经过vi编辑,而后在文本中直接输入普通用户

/etc/cron.allow 所列用户容许使用crontab命令,优先于/etc/cron.deny

/var/spool/cron/ 全部用户crontab配置文件默认存放此目录,文件名以用户名命名

[root@Songge log]# cat /var/spool/cron/root

#time sync by oldboy at 2010-2-1

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

##### 练习

#*/1 * * * * echo syy >>/server/log/oldboy.log

### 每一个一分钟,打印一个+号到oldboy.log

#* * * * * echo 'syy' >> /server/scripts/oldboy.log

### 打包任务

#00 */2 * * * tar zcvf /data/www/html.tar.gz /var/www/html

#####send qingshu mail

#* * * * * /bin/sh /server/scripts/send_qingshu.sh>/dev/null 2>&1

以上两种结果同样,crontab 读取的是/var/spool/cron/ 用户(root)的信息。

crontab 为何不直接在/var/spool/cron/路径编辑?

1.有语法检查功能 2.输入便捷

4.2.4 指令说明

crontab -e 编辑

crontab -l 罗列信息

4.2.5 定时任务指令使用格式

定时任务的格式分为6个段

* * * * * cmd

image

 

注意小结:

1.依次对应分时日月周

2.每一个*号之间至少一个空格

3. cmd 为要执行的命令或者脚本 例如经常使用:

/bin/sh /server/scripts/oldboy.sh

4. root 用户定时任务配置文件为 /var/spool/cron/root

crontab 特殊符号含义解释:

* 表示 每 意思

, 表示单个列出时间

- 表示从 到 的意思

/n 表示每隔n的意思

4.2.6 crontab 编辑定时任务依赖的服务

[root@Songge log]# chkconfig --list crond

crond 0:off 1:off 2:on 3:on 4:on 5:on6:off

[root@Songge /]# /etc/init.d/crond status

crond (pid 1257) is running...

[root@Songge /]# ps -ef |grep crond

root 1257 1 0 10:28 ? 00:00:00 crond

root 1715 1311 0 11:54 pts/0 00:00:00 grep --color=auto crond

[root@Songge /]# ps -ef |grep crond|grep -v grep

root 1257 1 0 10:28 ? 00:00:00 crond

#以上的gerp –v 是不显示 grep(做为名称) 内容

[root@Songge /]# /etc/init.d/crond restart

Stopping crond: [ OK ]

Starting crond: [ OK ]

4.2.7 查看几个例子

①30 3,12 * * *

②30 */6 * * *

③30 8-18/2 * * *

④30 21 * * *

⑤45 4 1,10,22 * *

⑥ 10 1 * * 6,0

⑦0,30 18-23 * * *

如下方式表示不规范

* 23,00-07/1 * * *

以上任务表示在晚上11点到第二天7点 每分钟都要执行任务

※ 周和日尽可能不要同时用,不然可能达不到想要的结果

4.3 生产环境crontab 专业实例

例题:4.3.1 每分钟 打印一次本身的名字拼音到“/server/log 本身的名字命名的文件”中。

mkdir -p /server/log

touch /server/log/syy.log

[root@Songge /]# crontab –e

### 每隔一分钟,打印本身姓名到syy.log

* * * * * echo 'syy' >> /server/log/syy.log

[root@Songge log]# tail -f syy.log

syy

syy

syy

【解答知识小结】

※ 定时任务加注释

※ 定时任务 尽可能不要在同一行出现两个命令

※此例题不能接入 >/dev/null 2>&1,不然不能出现结果。

※ /server/log 目录存在才能出结果

※ 定时任务必须的路径必定要绝对路径。

※ crond 服务必需要开启

※ 查看定时任务日志: tail /var/log/cron

例题:4.3.2 每周六 周日 9:00 和14:00 来老男孩Linux 培训学习(执行/server/scripts/ oldboy.sh)要求:此脚本的功能是打印当天的日期。

mkdir /server/scripts/ -p

echo ‘date +%F’ >> /server/scripts/oldboy.sh

crontab -e

00 9,14 * * 6,7 bin/sh /server/scripts/oldboy.sh

sh /server/scripts/oldboy.sh

【补充】

bash 格式 #! /bin/bash

history 查询历史命令

给文本加上权限 chmod +x 文件,而后用./文件名 就能够执行

【******】

[root@Songge ~]# cd /var/spool/postfix/maildrop

#以上路径为 邮箱发送失败后的邮件存储地。

[root@Songge server]# ls /var/spool/postfix/maildrop/

17FDE3F6BC B9F183F6A5 E2AA23F6BA

小结:

以上是错误的邮件信息

情形模拟:当此前的目录文件太多时,消耗掉inode ,

措施:给定时任务加>/dev/null 2>&1或者 定时任务加个任务 (sh oldboy.sh >>/dev/null 2>&1)

4.4书写定时任务规范要领:

为定时任务规则加必要的注释

执行shell 脚本任务前加 /bin/sh

定时任务命令或脚本结尾加

>/dev/null 2>&1【经常使用】

1>/dev/null 2>/dev/null

&>/dev/null

错误的写法
/bin/sh /server/scripts/oldboy.sh  >>/server/file/zhangyao &>/dev/null
正确的写法
[root@oldboyedu scripts]# cat oldboy.sh
#! /bin/bash
date +%F >>/server/log/zhangyao
* * * * * /bin/sh /server/scripts/oldboy.sh >/dev/null 2>&1

查看邮箱服务状态:

[root@Songge maildrop]# /etc/init.d/postfix status

master is stopped

邮箱开启

[root@Songge maildrop]# /etc/init.d/postfix start

邮箱关闭(通常经常使用设置)

[root@Songge maildrop]# /etc/init.d/postfix stop

若是邮箱服务开启,

或者将执行的命令定向到一个文本

或者定向到黑洞

则在/etc/init.d/postfix/maildrop 下面不会生成大量垃圾文件

案例:若是maildrop下面的文件太多,用rm直接删除会出现如下错误提示:Argument list too lon

解决方案:

1. 标准删除 ls|xargs rm -f

2. 方法将MAILTO=root的root去掉

[root@Songge ~]# vi /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

"/etc/crontab" 16L, 457C

定时任务命令超过两条的,最好写到脚本里执行

如下写法不专业

* * * * * sleep 1;echo oldboy >> /server/log/oldboy.log

标准写法:

cat /server/log/oldboy.log

sleep 1

echo oldboy >> /server/log/oldboy.log

在指定用户下执行相关的定时任务

*要注意不一样用户环境变量的问题

生产任务程序不要随意打印输出信息

tar zcvf echo 123 >a.log

例 下面是定时任务调用的脚本:

cat /server/scripts/tar.sh

tar zcf /server/scripts/backup_$(date +%F-%H-%M).tar.gz //server/scripts

定时任务执行的脚本要规范路径(/server/scripts)

配置定时任务规范操做过程

首先在命令行操做成功,而后复制成功的命令到脚本里。

2)而后测试脚本,测试成功后,复制脚本的规范路径到定时任务配置里

3)先手动测试,在放到正式环境部署

4.5 系统定时任务配置/etc/crontab

[root@oldboyedu ~]# ll /var/log/messages*

-rw------- 1 root root 1157016 Jun 2 09:40 /var/log/messages

-rw-------. 1 root root 594171 May 4 13:12 /var/log/messages-20160504

-rw------- 1 root root 494732 May 19 21:25 /var/log/messages-20160519

-rw------- 1 root root 391369 May 22 14:42 /var/log/messages-20160522

-rw------- 1 root root 1177379 May 29 07:03 /var/log/messages-20160529

#该文件夹的文件数不超过5个,只保留最新的5个

[root@oldboyedu ~]# cat /etc/crontab

SHELL=/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

MAILTO=root

HOME=/

shell 指 在那个环境下执行命令

path 环境变量

home 系统定时任务默认目录-家目录

咱们建立的定时任务存放在 /var/spool/cron/

root@Songge cron]# tailf /var/log/cron 查看定时任务的日志

企业生产场景如何调试crontab 定时任务

4.6.1 增长执行任务频繁调试任务(某些任务不能用于生产环境)

强调:有些计划任务是不容许频繁执行的,例如:定时往数据库里插入数据,这样的任务就要在测试机上测试好,而后部署到正式线上,这样正式工做出现的问题就会不多了

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

能够修该下系统当前时间

4.6.3 经过脚本日志输出调试定时任务

[root@oldboyedu ~]# crontab –e

# study task by oldboy at 20160602

00 9,14 * * 6,0 /bin/sh /server/scripts/oldboy.sh >/app/log.log 2>&1

脚本里输出 vim /oldboy.sh

cd /

tar zcvf /tmp/etc_$(date +%Y%m%d).tar.gz ./etc/tmp.log 2>&1

4.6.4 注意一些命令带来的问题

※ * * * * * echo “==” >> /tmp/oldboy.log >/dev/null 2>&1

以上是没法执行的任务配置,由于前面多了>> ,只能有一个重定向符号。要去掉后面的>/dev/null 2>&1

4.6.5 注意环境变量致使的定时任务故障

在调试java程序任务的时候,注意环境变量,把环境变量的定义加到脚本里

格式:export JAVA_HOME=/application/jdk1.6

export PATH=$JAVA_HOME/bin:$PATH

[root@oldboyedu ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

4.6.6 经过crond 定时任务服务日志调试定时任务

[root@oldboyedu ~]# ifconfig eth0|awk -F "[ :]+" 'NR==2{print $4}' 10.0.0.8

[root@oldboyedu ~]# echo $PATH

/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

[root@oldboyedu ~]# cd /server/scripts/

[root@oldboyedu scripts]# vim iptest.sh

#!/bin/bash

IP=$(ifconfig eth0|awk -F "[ :]+" 'NR==2{print $4}')

echo ${IP}_a123 >> /tmp/iptest.txt

[root@oldboyedu scripts]# sh iptest.sh

[root@oldboyedu scripts]# vim iptest.sh

#!/bin/bash

source /etc/profile

IP=$(ifconfig eth0|awk -F "[ :]+" 'NR==2{print $4}')

echo ${PATH}_${IP}_a123 >> /tmp/iptest.txt

另开窗口1:

[root@oldboyedu ~]# cd /tmp;ls

[root@oldboyedu tmp]# touch iptest.txt

[root@oldboyedu tmp]# tailf /tmp/iptest.txt

[root@oldboyedu tmp]# cat iptest.txt

10.0.0.8_a123

小结:

缘由:用户的定时任务的环境变量为/usr/bin:/bin

1. 脚本中全部的命令都使用全路径

2. 自定义path路径

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin

3.source /etc/profile

4.7 crontab 定时任务生产应用问题注意

1.系统环境变量问题

2.定时任务要用绝对路径

※若是用相对路径,则会建立在 该用户的家目录下面

# test

* * * * * echo + > oldboy.log

3.定时任务的脚本权限问题 最好带上/bin/sh

4. 时间变量问题用反斜线 \ 转义

crontab 任务命令中,若是有“date +%Y%m%d”,必须替换为“date +\%Y\%m\%d”但写在脚本文件里,就不需转义。

5. >/dev/null 2>&1

或者 (1>/dev/null 2>/dev/null) 或者(&>/dev/null)

当定时任务在你指定的时间执行后,系统会寄一封信给你,显示该程式执行的内容,若系统未开启邮件服务就会致使邮件临时目录/var/spool/clientmqueue 小文件逐渐增多,以致于大量消耗inode数量,其实在每一行任务结尾空一格以后加上 >/dev/null 2>&1 将输出定向为空 来规避这个问题

若是须要打印日志输出,也能够追加到制定的日志文件里。尽可能不要留空。

6. 定时任务以前加注释

7. 使用脚本程序代替命令行定时任务

8.避免没必要要的程序以及命令输出 如tar zcvf 的v

9.切到目标目录的上一级打包目标

4.8 crontab 生产问题及解决过程

面试题 : 在系统维护的时候,建立文件提示:No space left on device磁盘没有空间可写,请问这是什么故障?

解答: 磁盘空间block 满了或者inode满了 ,用df –h 或者df –i

查看

4.8.1 No space left on device 故障1

措施: 菜单/var/spool/clientmqueue && ls |xargs rm -f

亡羊补牢解决办法:

尽可能将crontab 里面的命令或脚本中的命令结尾加上

/dev/null 2>&1

启邮件服务,不过最好不作,由于邮件服务会带来安全问题。

优化系统,加定时清理任务,如 find /var/spool/clientmqueue/ -type f -mtime +30|xargs rm -f 放入到定时任务,每周处理一次

4.8.2 No space left on device 故障2

令passwd报错因inode节点处理记录
故障现象:
      一、修改密码时报错 passwd: Authentication token manipulation error
      二、添加用户报错:unable to lock password file
分析问题:
  一、检查相关配置文件权限正常:
  /etc/passwd
  /etc/shadow
google的解决方法尝试没能解决问题,df查看硬盘空间正常:

image

  二、使用命令strace -f passwd 追踪分析缘由,看到关键报错信息:“No space left on device”,但是df查看硬盘空间没问题呀,google得知有多是inode满了查看的确是根分区inode满了

image

查找缘由:
    /var/spool/clientmqueue 生成的文件占用完inode,此目录下文件的产生缘由主要是crontab里面的命令没有添加“>/dev/null 2>&1”标准输出、错误输出信息都输入到/dev/null,

解决方法:
   一、删除文件后正常,目录下文件太多建议使用管道加xargs:
   ls /var/spool/clientmqueue |xargs rm -rf   #由于目录下都是文件最好使用

image

二、将crontab命令后面添加“>/dev/null 2>&1”

小结:

1) inode知识

2) crontab任务写法

3/var/spool/clientmqueue 功能和潜在问题。

4strace跟踪命令

4.9 crond 章节重点

4.10常见报错注释

1. command not found 命令没有找到

2. No such file or directory 没有这个文件或目录

3. Permission denied 权限不足

4. No space left on device 磁盘没有剩余空间

5. File exists 文件已经存在

6. Is a directory 这是1个目录

7. Not a directory 不是1个目录

8. Warning: Changing a readonly file 警告:改变一个只读文件

9. Found a swap file by the name ".1.swp" vim编辑器碰到的意外关闭文件有缓存文件

10. unrecognized option '--aaa' 未知参数,去查看帮助

11. Connection timed out

12. Connection refused

4.11 crontab 书写规范逻辑图

定时任务书写规范逻辑结构图1

4.12 Linux邮件发送情书 逻辑结构图

image

【补充】定时任务的平常rc.d不能直接打包 (它是一个软链接),须要使用原路径

相关文章
相关标签/搜索
本站公众号
   欢迎关注本站公众号,获取更多信息