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.www58058.com/44577

(0)
linux_rootlinux_root
上一篇 2016-09-08
下一篇 2016-09-08

相關推薦

  • 14 用戶組和權限管理4

    14 用戶組和權限管理4 一、雜項知識整理 1、groups 查看用戶所屬組列表 [user1@localhost ~]$ groups user3 user1 2、文件屬性:-rw-r–r–. 1 root root 0 8月   3 07:56 cgroup.clone_children &…

    Linux干貨 2016-08-04
  • N25-Bazinga-第二周作業

    1.Linux文件管理類命令 命令 功能 命令 功能 pwd 顯示當前目錄 ls 顯示目錄下的內容 cd 改變所在目錄 cat 顯示文件的內容 grep 在文件中查找字符串 cp 復制文件 touch 創建文件 mv 移動文件 rm 刪除文件 rmdir 刪除目錄 1.1 pwd命令 該命令的英文解釋為print working&nbsp…

    Linux干貨 2016-12-13
  • N25 – 第二周博客作業

      Linux上的文件管理類命令有哪些,其常用的使用方法及相關示例演示 more: more命令非常簡單,只用more file即可,但是more命令有一個特點,就是翻屏至文件尾部后自動退出 less: less命令就是在使用man時是一樣的,因為man手冊就是調用less來查看的  head: head命令用來查看文件的前#行,#默認為…

    Linux干貨 2016-12-07
  • find和grep命令練習

                                    find和grep命令練習 1、顯示當前系統上root,fedora,或user1用戶的默認shell; ~]# cat /etc/pa…

    Linux干貨 2016-10-23
  • Linux主要發行版

    Redhat:三大發行版之一,由紅帽公司維護,分支有fedora,centosDebian:社區維護,非商業維護,三大發行版之一,分支有Ubuntu,Mintslackware:三大發行版之一,分支有Suse,opensusearch Linux:輕量級行業新貴

    Linux干貨 2018-03-03
  • 源碼編譯nginx和Nginx的特性,配置文件指令說明

    nginx的官方站點 www.nginx.org Nginx:engine X Tenginx:Taobao enginx libevent:高性能的網絡庫 epoll(); Nginx特性  模塊化設計,較好擴展性:但不支持模塊的動態裝卸載;Tenginx支持;  高可靠性   master –> …

    Linux干貨 2017-04-30
欧美性久久久久