<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                [TOC] 1. Linux有個三種方式 **/etc/cron.d** , **/etc/crontab文件**和**crontab -e**,遇到的很多黑客攻擊都會用定時來不斷的摧殘我們的系統。 2. cron服務的最低檢測時間單位是分鐘,所以cron會每分鐘讀取一次/etc/crontab與/var/spool/cron中的數據內容,因此,編輯完/etc/crontab文件并且保存之后,crontab時設定就會自動執行。 **3. cron執行時,也就是要讀取三個地方的配置文件:一是/etc/crontab,二是/etc/cron.d目錄下的所有文件,三是每個用戶的配置文件. ** ## **1. /etc/crontab文件** ### **1.1這種設置定時任務的方法,只限于root用戶(用于root給自己與其他用戶分配定時任務)** ``` root@ubuntu01:~# ll /etc/crontab -rw-r--r-- 1 root root 420 Nov 8 18:25 /etc/crontab ``` 如上,只有root有權修改這個文件。 ### **1.2創建定時任務** ``` * * * * * <用戶> <commond> :給某一用戶創建定時任務 ``` ``` root@ubuntu01:~# vim /etc/crontab # /etc/crontab: system-wide crontab # Unlike any other crontab you don't have to run the `crontab' # command to install the new version when you edit this file # and files in /etc/cron.d. These files also have username fields, # that none of the other crontabs do. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # m h dom mon dow user command */2 * * * * root echo "dd" >> /root/test ``` ### **1.3.這種方式,通過`crontab -l`是*不能顯示用戶的定時任務的*** ``` root@ubuntu01:~# crontab -l no crontab for root ``` ### **1.4直接刪除定時任務的那行就可以停止定時任務** ### **1.5 centos與Ubuntu的區別** #### 1.5.1 Ubuntu的/etc/crontab ``` root@ubuntu02:~# vim /etc/crontab # /etc/crontab: system-wide crontab # Unlike any other crontab you don't have to run the `crontab' # command to install the new version when you edit this file # and files in /etc/cron.d. These files also have username fields, # that none of the other crontabs do. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin # m h dom mon dow user command 17 * * * * root cd / && run-parts --report /etc/cron.hourly 25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily ) 47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly ) 52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly ) ``` #### 1.5.2 centos的 /etc/crontab ``` [root@localhost ~]# vi /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # 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 ``` ### 1.5.3說明 1.Ubuntu的文件多出了幾行`25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )`這種東西 2.run-parts --report /etc/cron.daily ://每天執行/etc/cron.hourly內的腳本,run-parts代表執行目錄中的腳本,不加的話要指定執行的腳本了,而不是目錄。 3.難道centos就沒有類似的這種文件嗎?有: ``` [root@localhost etc]# ls | grep cron anacrontab cron.d cron.daily cron.deny cron.hourly cron.monthly crontab cron.weekly ``` ## **2.crontab -e** **1.這種所有用戶都可以使用,普通用戶也只能為自己設置計劃任務 2.然后自動將定時任務寫入/var/spool/cron/crontabs/username** ### **2.1創建定時任務** ``` tuna@ubuntu01:~$ crontab -e # Edit this file to introduce tasks to be run by cron. ... # # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command */1 * * * * echo "I am tuna,hello!" > ~/tuna.txt ``` ### **2.2查看定時任務** ``` tuna@ubuntu01:~$ crontab -l # Edit this file to introduce tasks to be run by cron. # ... # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command */1 * * * * echo "I am tuna,hello!" > ~/tuna.txt ``` ### **2.3刪除定時任務** 執行下邊的命令后,自動刪除屬于當前用戶的定時任務 ``` crontab -r ``` ## 3. /etc/cron.d ``` SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # 發生錯誤會給/var/spool/mail/root發送email */1 * * * * root /root/test.sh ``` ## 4. crontab的限制 ``` /etc/cron.allow:將可以使用 crontab 的帳號寫入其中,若不在這個文件內的使用者則不可使用 crontab; /etc/cron.deny:將不可以使用 crontab 的帳號寫入其中,若未記錄到這個文件當中的使用者,就可以使用 crontab 。 ``` 如果有需要,使用其中一種方式即可 ### 3.1 實例 ``` root@ubuntu01:~# cat /etc/cron.deny tuna test ``` ## **5.anacron** 1.anacron并不能取代cron去運行某項任務,而是以天為單位或者是在啟動后立刻進行anacron的動作,**它會去偵測停機期間應該進行但是并沒有進行的crontab任務,并將該任務運行一遍**后,anacron就會自動停止了。 **2.anacron運行的時間通常由兩個,一個是系統啟動期間運行,一個是寫入crontab的排程中,這樣才能夠在特定時間分析系統未進行的crontab工作。** **3.anacron的配置文件是/etc/anacrontab** ``` [root@localhost cron.hourly]# vi /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 ``` **4.anacron命令的語法如下:** (1)-s開始連續的運行各項工作,會一句時間記錄當的數據判斷是否進行。 (2)-f強制進行,而不去判斷時間登錄檔的時間戳。 (3)-n立即進行未進行的任務,而不延遲等待時間。 (4)-u僅升級時間記錄當的時間戳,不進行任何工作。 而anacron的配置文件是/etc/anacrontab,而它的很多內容則是在/var/spool/anacron里面保存。 **5.當anacron下達anacron -s cron.daily時,它會有如下的步驟:** (1)由/etc/anacrontab分析到cron.daily這項工作名稱的天數為一天。 (2)由/var/spool/anacron/cron.daily取出最近一次運行anacron的時間戳。 (3)把取出的時間戳與當前的時間戳相比較,如果差異超過了一天,那么就準備進行命令。 (4)若準備進行命令,根據/etc/anacrontab的配置,將延遲65分鐘。 (5)延遲時間后,開始運行后續命令,也就是run-parts /etc/cron.daily這串命令。 (6)運行完畢后,anacron程序結束。 **6. crontab調用anacron** 1)/etc/cron.d/0hourly /etc/cron.d目錄 cron進程執行時,就會自動掃描該目錄下的所有文件,按照文件中的時間設定執行后面的命令。 ``` [root@localhost cron.d]# vi 0hourly #nacron Run the hourly jobs SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root 01 * * * * root run-parts /etc/cron.hourly ``` 2)/etc/cron.hourly ``` [root@localhost cron.hourly]# ls 0anacron [root@localhost cron.hourly]# vi 0anacron #!/bin/sh # Check whether 0anacron was run today already if test -r /var/spool/anacron/cron.daily; then day=`cat /var/spool/anacron/cron.daily` fi if [ `date +%Y%m%d` = "$day" ]; then exit 0; fi # Do not run jobs when on battery power if test -x /usr/bin/on_ac_power; then /usr/bin/on_ac_power >/dev/null 2>&1 if test $? -eq 1; then exit 0 fi fi /usr/sbin/anacron -s //調用 ``` 如上腳本所示,會按照天去檢查是否有未執行的定時任務
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看