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

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

§·linux 系统计划任务

※·linuxCentos计划任务的程序为:cron

周期性任务计划:cron

相关的程序包:

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

cronie-anacroncronie的补充程序;用于监控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)
上一篇 2016-09-08 18:55
下一篇 2016-09-08 18:55

相关推荐

  • LVS的简介和配置

    lvs分为3种模式 :1:lvs-nat:本质是多目标IP的DNAT,通过将请求报文中的目标地址和目标端口修改为某挑出的RS的RIP和PORT实现转发(1)RIP和DIP必须在同一个IP网络,且应该使用私网地址;RS的网关要指向DIP(2)请求报文和响应报文都必须经由Director转发,Director易于成为系统瓶颈(3)支持端口映射,可修改请求报文的目…

    2017-10-23
  • 一个简单小例子来说一下Rescue营救模式

    昨天不小心将/lib64下的一个动态库文件libc.so.6(很多命令都依赖的)给移动到了/root下,然后除了一些内部命令可用外,其余命令都不能用了,然后就想着看看重启可不可以修复,结果重启后就再也开不了机了,心想着这可咋整,要不用最快的方式进行快照恢复吧,结果老师就说了,不能使用快照,让我们使用Rescue营救模式进行恢复,然后在恢复的过程中就发现一个问…

    Linux干货 2017-08-21
  • N24_jerry 第十二周作业

    1、请描述一次完整的http请求处理过程; 简介 一次完整的HTTP请求过程从TCP三次握手建立连接成功后开始,客户端按照指定的格式开始向服务端发送HTTP请求,服务端接收请求后,解析HTTP请求,处理完业务逻辑,最后返回一个HTTP的响应给客户端,HTTP的响应内容同样有标准的格式。无论是什么客户端或者是什么服务端,大家只要按照HTTP的协议标准…

    Linux干货 2017-03-22
  • N22-第六周作业

    1、复制/etc/rc.d/init.d/functions文件至/tmp目录,将/tmp/functions文件中 的以至少一个空白字符开头的行的行首加# ~]# cp /etc/rc.d/init.d/functions /tmp~]# vim /tmp/functions %s@^[[:space:]]\+@#@g 2、复制/boot/grub/gru…

    Linux干货 2016-10-17
  • 实现CA和证书申请,

    接下来讲的是在centos7.3和centos6.8中实现CA和证书申请,centos7.3作为主机,centos6.8作为客户端 首先你授权客户端CA证书,必须本身主机也具有CA,自己证明自己,先CA自签证书,然后在7.3创建私钥 为了方便以后的操作CD进入 cd /etc/pki/CA   生成自签名证书 -new:  生成…

    2017-04-11
  • N26 – 第一周作业

    一、计算机组成及其功能     如图,计算机由硬件和软件组成。其中硬件可划分为控制器、运算器、存储器,以及输入和输出设备;软件则分为直接与硬件通信的系统软件,以及运行在其上的各类应用软件。      二、Linux发行版的不同系列     …

    Linux干货 2016-12-28