CentOS Linux 系统计划任务简单介绍

CentOS  Linux 系统计划任务简单介绍

§·linux 系统计划任务

※·linux中Centos计划任务的程序为:cron

周期性任务计划:cron

相关的程序包:

cronie: 主程序包,提供crond守护进程及相关辅助工具

cronie-anacron:cronie的补充程序;用于监控cronie任务执行状况;如cronie中的任务在过去该运行的时间点未能正常运行,则anacron会随后启动一次此任务

crontabs:包含CentOS提供系统维护任务

 

查看系统计划任务程序包

[root@centos68 grub]# rpm -qa |  grep cron
cronie-1.4.4-15.el6_7.1.x86_64
crontabs-1.10-33.el6.noarch
cronie-anacron-1.4.4-15.el6_7.1.x86_64
 
[root@centos68 grub]# rpm -ql cronie-1.4.4-15.el6_7.1.x86_64  #cron主程序包,提供crond的守护进程
/etc/cron.d
/etc/cron.d/0hourly
/etc/cron.deny
/etc/pam.d/crond
/etc/rc.d/init.d/crond
/etc/sysconfig/crond
/usr/bin/crontab
/usr/sbin/crond
/usr/share/doc/cronie-1.4.4
/usr/share/doc/cronie-1.4.4/AUTHORS
/usr/share/doc/cronie-1.4.4/COPYING
/usr/share/doc/cronie-1.4.4/ChangeLog
/usr/share/doc/cronie-1.4.4/INSTALL
/usr/share/doc/cronie-1.4.4/README
/usr/share/man/man1/crontab.1.gz
/usr/share/man/man5/crontab.5.gz
/usr/share/man/man8/cron.8.gz
/usr/share/man/man8/crond.8.gz
/var/spool/cron
 
[root@centos68 grub]# rpm  -ql  crontabs-1.10-33.el6.noarch  #系统计划任务使用程序包
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
/etc/crontab
/usr/bin/run-parts
/usr/share/man/man4/crontabs.4.gz
 
[root@centos68 grub]# rpm -ql cronie-anacron-1.4.4-15.el6_7.1.x86_64   #anacron程序包,服务器很少使用
/etc/anacrontab
/etc/cron.hourly/0anacron
/usr/sbin/anacron
/usr/share/man/man5/anacrontab.5.gz
/usr/share/man/man8/anacron.8.gz
/var/spool/anacron
/var/spool/anacron/cron.daily
/var/spool/anacron/cron.monthly
/var/spool/anacron/cron.weekly
[root@centos68 grub]#

 ※·分析 linux中Centos系统周期性任务分类

[root@centos68 grub]# rpm  -ql  crontabs-1.10-33.el6.noarch  #系统计划任务使用程序包
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
/etc/crontab   #系统计划任务的配置文件
/usr/bin/run-parts
/usr/share/man/man4/crontabs.4.gz

 ※·分析几个定时计划目录文件: 天计划,小时计划,月计划,周计划

[root@centos68 etc]# ls /etc/cron.daily/    #天计划任务目录下的文件
cups  logrotate  makewhatis.cron  mlocate.cron  prelink  readahead.cron  tmpwatch
[root@centos68 etc]# ls /etc/cron.hourly/   #小时计划任务目录下文件
0anacron
[root@centos68 etc]# ls /etc/cron.monthly/  #月计划任务目录下的文件
readahead-monthly.cron
[root@centos68 etc]# ls /etc/cron.weekly/

 ※·Linux 系统常见的计划任务有以下几种:

有兴趣可以看看研究下 /etc/cron.daily/ 各个执行脚本的文件内容

◎·进行登录档的轮替 (log rotate):

Linux 会主动的将系统所发生的各种信息都记录下来,这就是登录档。 由于系统会一直记录登录信息,所以登录文件将会越来越大!我们知道大型文件不但占容量还会造成读写效能的困扰, 因此适时的将登录文件数据挪一挪,让旧的数据与新的数据分别存放,则比较可以有效的记录登录信息。这就是 logrotate 的任务!这也是系统必要的例行任务;

◎·登录文件分析 logwatch 的任务:

如果系统发生了软件问题、硬件错误、资安问题等,绝大部分的错误信息都会被记录到登录文件中, 因此

系统管理员的重要任务之一就是分析登录档。但你不可能手动透过 vim 等软件去检视登录文件,因为数据

太复杂了! 我们的 CentOS 提供了一只程序『 logwatch 』来主动分析登录信息,所以你会发现,你的 root

老是会收到标题为 logwatch 的信件,那是正常的!你最好也能够看看该信件的内容喔!

◎·建立 locate 的数据库:

我们谈到的 locate 指令时, 我们知道该指令是透过已经存在的文件名数据库来进行系统上文件名的查询。我 们的文件名数据库是放置到 /var/lib/mlocate/ 中。 问题是,这个数据库怎么会自动更新啊?嘿嘿!这就是系 统的例行性工作所产生的效果啦!系统会主动的进行 updatedb 喔!

◎·man page 查询数据库的建立:

与 locate 数据库类似的,可提供快速查询的 man page db 也是个数据库,但如果要使用 man page 数据库

时,就得要执行 mandb 才能够建立好啊! 而这个 man page 数据库也是透过系统的例行性工作排程来自

动执行的哩!

◎·RPM 软件登录文件的建立:

RPM (第二十二章) 是一种软件管理的机制。由于系统可能会常常变更软件, 包括软件的新安装、非经常

性更新等,都会造成软件文件名的差异。为了方便未来追踪,系统也帮我们将文件名作个排序的记录呢! 有

时候系统也会透过排程来帮忙 RPM 数据库的重新建置喔!

◎·清楚临时文件:

某些软件在运作中会产生一些暂存档,但是当这个软件关闭时,这些暂存盘可能并不会主动的被移除。 有

些暂存盘则有时间性,如果超过一段时间后,这个暂存盘就没有效用了,此时移除这些暂存盘就是一件重

要的工作! 否则磁盘容量会被耗光。系统透过例行性工作排程执行名为 tmpwatch 的指令来删除这些暂存

档呢!

◎·与网络服务有关的分析行为:

如果你有安装类似 WWW 服务器软件 (一个名为 apache 的软件),那么你的 Linux 系统通常就会主动的

分析该软件的登录文件。 同时某些凭证与认证的网络信息是否过期的问题,我们的 Linux 系统也会很亲和

的帮你进行自动检查!

※· Linux 系统计划任务的大概执行流程

◎·crond 服务读取配置文件的位置:

◎·一般来说,crond 预设有三个地方会有执行脚本配置文件,他们分别是:

· /etc/crontab     #crontab 系统计划任务的配置文件

· /etc/cron.d/*    #可以把每一个系统计划任务放在该目录下,系统会自动定时检查该目录下的脚本

· /var/spool/cron/* #linux用户的计划任务配置文件存放地

◎·还有几个重要目录:

/etc/cron.daily      #系统定时每天执行一次,不需要在文件中定义时间,只需要写上脚本内容即可。

/etc/cron.hourly #系统定时小时执行一次,不需要在文件中定义时间,只需要写上脚本内容即可。

/etc/cron.monthly #系统定时每月执行一次,不需要在文件中定义时间,只需要写上脚本内容即可。

/etc/cron.weekly #系统定时每周执行一次,不需要在文件中定义时间,只需要写上脚本内容即可。

◎·crontab系统计划任务文件格式:

[root@centos68 cron.daily]# cat /etc/crontab 
#定义各种系统环境,注意,在文件写的命令建议使用绝对路径,由于脚本申明的PATH变量不一定是系统的PATH所有的PATH变量。
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  *  *  *  *   root   /root/crontabhelloworld.sh      #系统计划任务需要填写用户名的。

◎·cron.d/目录下系统计划任务文件格式:

在cron.d/的目录下都是独立的文件,所以我们的定义的计划任务直接写在一个文件中即可,内容如下:

[root@centos68 cron.d]# cat  crondhelloworld.sh    #存放在/etc/cron.d/目录下的文件 内容为如下
*/2   *   *   *  *  root  /root/crondhelloworld.sh  #真正脚本文件放置与 /root/文件目录下
[root@centos68 cron.d]# pwd
/etc/cron.d
[root@centos68 cron.d]# 
 
[root@centos68 cron.d]# ll   #存放在/etc/cron.d/的文件属性记得一定要是普通文件
total 16
-rw-r--r--. 1 root root 113 Nov 10  2015 0hourly
-rw-r--r--. 1 root root  51 Sep  8 09:40 crondhelloworld.sh
-rw-------. 1 root root 108 Dec 11  2015 raid-check
-rw-------. 1 root root 235 May 11 10:02 sysstat
[root@centos68 cron.d]#

◎·cron系统定义好的计划任务文件格式:(最小貌似为小时任务)

[root@centos68 cron.hourly]# ll  #在/etc/cron.hourly/定义的文件都是可执行权限的文件,内容直接为脚本内容即可
total 8
-rwxr-xr-x. 1 root root 409 Nov 10  2015 0anacron
-rwxr-xr-x. 1 root root  51 Sep  8 09:58 cronhour.sh
[root@centos68 cron.hourly]# cat cronhour.sh   #查看其文件内容
#!/bin/bash
 
echo "$0  hello  word..............."
[root@centos68 cron.hourly]# pwd
/etc/cron.hourly
[root@centos68 cron.hourly]#

◎·验证以上三种情况定义的系统计划任务:

由于我使用了 echo 命令,所以任务每次执行,我都会收到邮件:

[root@centos68 cron.hourly]# mail
U 37 Cron Daemon           Thu Sep  8 10:06  23/766   "Cron <root@centos68>   run-parts /etc/cron.hourly
 U 38 Cron Daemon           Thu Sep  8 10:06  24/788   "Cron <root@centos68>  /root/crontabhelloworld.sh"
 U 39 Cron Daemon           Thu Sep  8 10:08  23/766   "Cron <root@centos68>  /root/crondhelloworld.sh"
 U 40 Cron Daemon           Thu Sep  8 10:08  24/788   "Cron <root@centos68>  /root/crontabhelloworld.sh"
 U 41 Cron Daemon           Thu Sep  8 10:10  24/788   "Cron <root@centos68>  /root/crontabhelloworld.sh"
 U 42 Cron Daemon           Thu Sep  8 10:10  23/766   "Cron <root@centos68>  /root/crondhelloworld.sh"

·Centos计划任务日志查询(/var/log/cron)

root@centos68 cron.hourly]# less  /var/log/cron

Sep  8 09:40:01 centos68 CROND[4813]: (root) CMD ( /root/crontabhelloworld.sh)
Sep  8 09:41:01 centos68 crond[2572]: (root) BAD FILE MODE (/etc/cron.d/crondhelloworld.sh) 
#定义的系统任务有语法错误
Sep  8 09:42:01 centos68 CROND[4830]: (root) CMD ( /root/crontabhelloworld.sh)
Sep  8 09:44:01 centos68 CROND[4842]: (root) CMD ( /root/crontabhelloworld.sh)
Sep  8 09:46:01 centos68 CROND[4857]: (root) CMD ( /root/crontabhelloworld.sh)
Sep  8 09:48:02 centos68 CROND[4885]: (root) CMD ( /root/crondhelloworld.sh)
Sep  8 09:48:02 centos68 CROND[4884]: (root) CMD ( /root/crontabhelloworld.sh)
Sep  8 09:49:02 centos68 anacron[4606]: Job `cron.daily' started     
Sep  8 09:49:02 centos68 run-parts(/etc/cron.daily)[4898]: starting cups
Sep  8 09:49:02 centos68 run-parts(/etc/cron.daily)[4906]: finished cups
Sep  8 09:49:02 centos68 run-parts(/etc/cron.daily)[4898]: starting logrotate
Sep  8 09:49:02 centos68 run-parts(/etc/cron.daily)[4913]: finished logrotate
Sep  8 09:49:02 centos68 run-parts(/etc/cron.daily)[4898]: starting makewhatis.cron
Sep  8 09:49:02 centos68 run-parts(/etc/cron.daily)[5079]: finished makewhatis.cron
Sep  8 09:49:02 centos68 run-parts(/etc/cron.daily)[4898]: starting mlocate.cron
Sep  8 09:49:05 centos68 run-parts(/etc/cron.daily)[5090]: finished mlocate.cron
Sep  8 09:49:05 centos68 run-parts(/etc/cron.daily)[4898]: starting prelink
Sep  8 09:49:05 centos68 run-parts(/etc/cron.daily)[5102]: finished prelink
Sep  8 09:49:05 centos68 run-parts(/etc/cron.daily)[4898]: starting readahead.cron
Sep  8 09:49:05 centos68 run-parts(/etc/cron.daily)[5114]: finished readahead.cron
Sep  8 09:49:05 centos68 run-parts(/etc/cron.daily)[4898]: starting tmpwatch
Sep  8 09:49:06 centos68 run-parts(/etc/cron.daily)[5152]: finished tmpwatch
Sep  8 09:49:06 centos68 anacron[4606]: Job `cron.daily' terminated
Sep  8 09:49:06 centos68 anacron[4606]: Normal exit (1 job run)
Sep  8 09:50:01 centos68 CROND[5161]: (root) CMD ( /root/crontabhelloworld.sh)
Sep  8 09:50:01 centos68 CROND[5163]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Sep  8 09:50:01 centos68 CROND[5162]: (root) CMD ( /root/crondhelloworld.sh)
Sep  8 09:52:01 centos68 CROND[5181]: (root) CMD ( /root/crontabhelloworld.sh)
Sep  8 09:52:01 centos68 CROND[5182]: (root) CMD ( /root/crondhelloworld.sh)
Sep  8 09:54:01 centos68 CROND[5199]: (root) CMD ( /root/crontabhelloworld.sh)
Sep  8 09:54:01 centos68 CROND[5200]: (root) CMD ( /root/crondhelloworld.sh)
Sep  8 09:56:01 centos68 CROND[5217]: (root) CMD ( /root/crondhelloworld.sh)
Sep  8 09:56:01 centos68 CROND[5218]: (root) CMD ( /root/crontabhelloworld.sh)
Sep  8 09:58:01 centos68 CROND[5236]: (root) CMD ( /root/crondhelloworld.sh)
Sep  8 09:58:01 centos68 CROND[5237]: (root) CMD ( /root/crontabhelloworld.sh)
Sep  8 10:00:01 centos68 CROND[5263]: (root) CMD ( /root/crondhelloworld.sh)
Sep  8 10:00:01 centos68 CROND[5264]: (root) CMD ( /root/crontabhelloworld.sh)
Sep  8 10:00:01 centos68 CROND[5265]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Sep  8 10:01:01 centos68 CROND[5287]: (root) CMD (run-parts /etc/cron.hourly)
Sep  8 10:01:01 centos68 run-parts(/etc/cron.hourly)[5287]: starting 0anacron   #系统执行的每小时任务
Sep  8 10:01:01 centos68 run-parts(/etc/cron.hourly)[5296]: finished 0anacron
Sep  8 10:01:01 centos68 run-parts(/etc/cron.hourly)[5287]: starting cronhour.sh
Sep  8 10:01:01 centos68 run-parts(/etc/cron.hourly)[5303]: finished cronhour.sh
Sep  8 10:02:01 centos68 CROND[5310]: (root) CMD ( /root/crontabhelloworld.sh)
Sep  8 10:02:01 centos68 CROND[5311]: (root) CMD ( /root/crondhelloworld.sh)
Sep  8 10:04:01 centos68 CROND[5331]: (root) CMD ( /root/crondhelloworld.sh)
Sep  8 10:04:01 centos68 CROND[5332]: (root) CMD ( /root/crontabhelloworld.sh)
Sep  8 10:06:01 centos68 CROND[5353]: (root) CMD ( /root/crontabhelloworld.sh)
Sep  8 10:06:01 centos68 CROND[5352]: (root) CMD ( /root/crondhelloworld.sh)
Sep  8 10:08:02 centos68 CROND[6055]: (root) CMD ( /root/crondhelloworld.sh)
Sep  8 10:08:02 centos68 CROND[6056]: (root) CMD ( /root/crontabhelloworld.sh)
Sep  8 10:10:01 centos68 CROND[6106]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Sep  8 10:10:01 centos68 CROND[6107]: (root) CMD ( /root/crontabhelloworld.sh)  #定义在crontab中的计划任务
Sep  8 10:10:01 centos68 CROND[6105]: (root) CMD ( /root/crondhelloworld.sh)
Sep  8 10:12:01 centos68 CROND[6142]: (root) CMD ( /root/crondhelloworld.sh)
Sep  8 10:12:01 centos68 CROND[6141]: (root) CMD ( /root/crontabhelloworld.sh)
Sep  8 10:14:01 centos68 CROND[6171]: (root) CMD ( /root/crondhelloworld.sh)
Sep  8 10:14:01 centos68 CROND[6170]: (root) CMD ( /root/crontabhelloworld.sh)

原创文章,作者:linux_root,如若转载,请注明出处:http://www.178linux.com/44577

赞 (0)
linux_rootlinux_root
上一篇 2016-09-08 18:55
下一篇 2016-09-08 18:55

相关推荐

  • N25第八周学习总结_sed命令详解

    sed使用详解 大纲: 1、    sed是什么 2、    为什么要使用sed 3、    sed使用方法 4、    sed详解   正题: 1、    sed是什么 sed全名Stream Ed…

    2017-02-26
  • 上海2016全球运维大会 美女辣妹吸引眼球!

    国内第一个运维行业大会 GOPS 2016全球运维大会(上海站)在2016年9月23-24日在上海雅悦新天地大酒店举办,面向IT及传统行业、广大运维技术人员,传播先进技术思想和理念,分享业内最佳实践。 参会两日,虽然大会主角是运维技术交流学习,不过却意料之外的邂逅了另一道靓丽的风景,瞬间燃了… 在会场,站立着一群高挑美女,我的…

    2016-12-05
  • N26-第十二周

    1、请描述一次完整的http请求处理过程;        1、建立或处理链接:接收请求或拒绝请求        2、接收请求:接收来自于网络的请求报文中对某资源的一次请求的过程;接收请求的方式通常是并发访问响应模型        3、处理请…

    2017-05-14
  • class16高级磁盘管理(二)网络管理(一)

    一、高级磁盘管理(二) 逻辑卷管理器快照 快照是特殊的逻辑卷 快照只有在它们和原来的逻辑卷不同时才会消耗空间。 当原来的逻辑卷中有所改变时,会将旧的数据复制到快照中。 建立快照的卷大小只需要原始逻辑卷的15%~20%就够了。也可以使用lvextend 放大快照。 使用LVM 快照 为现有逻辑卷创建快照 #lvcreate -L …

    Linux干货 2016-09-05
  • Tomcat基于MSM+Memcached实现Session共享

    前言 在Tomcat集群中,当一个节点出现故障,其他节点该如何接管故障节点的Session信息呢?本文带来的解决方案是基于MSM+Memcached实现Session共享。 相关介绍 MSM MSM–Memcached Session Manager是一个高可用的Tomcat Session共享解决方案,除了可以从本机内存快速读取Session信…

    Linux干货 2015-07-03
  • Trie树:应用于统计和排序

    1. 什么是trie树 1.Trie树 (特例结构树)         Trie树,又称单词查找树、字典树,是一种树形结构,是一种哈希树的变种,是一种用于快速检索的多叉树结构。典型应用是用于统计和排序大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计。它的优点是:最大限度地减少…

    Linux干货 2015-04-08