Linux常用命令02-定時任務

一、cron

1、簡介

Cron is a system daemon used to execute desired tasks (in the background) at designated times.
Cron是一個系統守護進程,用於在指定的時間(在後臺)執行所需的任務。

A crontab file is a simple text file containing a list of commands meant to be run at specified times.
crontab文件是一個簡單的文本文件,其中包含一系列要在指定時間運行的命令。

2、格式

用戶所建立的crontab文件中,每一行都代表一項任務,每行的每個字段代表一項設置,它的格式共分爲六個字段,前五段是時間設定段,第六段是要執行的命令段。格式如下:

field allowed values
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)

command:要執行的命令,可以是系統命令,也可以是自己編寫的shell腳本文件。 在以上各個字段中,還可以使用以下特殊字符:

星號(*):代表所有可能的值,全部。
逗號(,):可以用逗號隔開的值指定一個列表範圍。
中槓(-):可以用整數之間的中槓表示一個整數範圍。
正斜線(/):一般配合 * 使用,代表每隔多長時間。(比如分鐘部分填寫 */3 代表每隔3分鐘)

3、編輯

The -e option is used to edit the current crontab using the editor specified by the EDITOR environment variable.

crontab -e 

使用以上命令,可以打開你的用戶所屬的crontab文件。

注意:第一次用這個命令,會讓你選擇文本編輯器,默認的是nano。選定的編輯器也可以使用命令select-editor進行修改。
或者,export EDITOR=vim

打開後的crontab文件類似這樣子:

# m h  dom mon dow   command
20 14 22 2 5 touch /var/www/c_test03.txt    #創建一個文件的例子

注意:After you exit from the editor, the modified crontab is checked for errors and, if there are no errors, it is installed automatically. The file is stored in /var/spool/cron/crontabs but should only be edited using the crontab command.

crontab文件對每個用戶都是不同的。cron中的任務是以你當前登錄用戶的權限執行的,如果你需要以root用戶執行某個任務,可以在crontab前加上sudo。

sudo crontab -e 

4、其它命令

crontab [ -u user ] file
crontab [ -u user ] [ -i ] { -e | -l | -r }

1)-u user
用來設定某個用戶的crontab服務,例如,“-u nosee”表示設定nosee用戶的crontab服務,此參數一般有root用戶來運行。

2)file
file是命令文件的名字,表示將file做爲crontab的任務列表文件並載入crontab。如果在命令行中沒有指定這個文件,crontab命令將接受標準輸入(鍵盤)上鍵入的命令,並將它們載入crontab。

3)-l
The -l option causes the current crontab to be displayed on standard output.
顯示某個用戶的crontab文件內容,如果不指定用戶,則表示顯示當前用戶的crontab文件內容。

crontab -l

4)-r
The -r option causes the current crontab to be removed.
從/var/spool/cron目錄中刪除某個用戶的crontab文件,如果不指定用戶,則默認刪除當前用戶的crontab文件。

crontab -r

5)-i
The -i option modifies the -r option to prompt the user for a 'y/Y' response before actually removing the crontab.
在刪除用戶的crontab文件時給確認提示。

5、cron服務

/etc/init.d/cron {start|stop|status|restart|reload|force-reload}

或:

service cron {start|stop|status|restart|reload|force-reload}

6、cron相關文件

1)cron.allow與cron.deny
If the /etc/cron.allow file exists, then users must be listed in it in order to be allowed to run the crontab command. If the /etc/cron.allow file does not exist but the /etc/cron.deny file does, then users must not be listed in the /etc/cron.deny file in order to run crontab.
cron.allow文件中所列用戶允許使用crontab命令,cron.deny文件中所列用戶不允許使用crontab命令。

In the case where neither file exists, the default on current Ubuntu (and Debian, but not some other Linux and UNIX systems) is to allow all users to run jobs with crontab.

No cron.allow or cron.deny files exist in a standard Ubuntu install, so all users should have cron available by default, until one of those files is created. If a blank cron.deny file has been created, that will change to the standard behavior users of other operating systems might expect: cron only available to root or users in cron.allow.

在標準Ubuntu中默認不存在cron.allow或cron.deny文件,因此默認情況下所有用戶都應該有cron可用。

2)/var/spool/cron/
所有用戶crontab文件存放的目錄,以用戶名命名。

3)/etc/crontab
這個文件負責安排由系統管理員制定的維護系統以及其他任務的crontab。

7、注意事項

  • 環境變量

有時我們創建了一個crontab,但是這個任務卻無法自動執行,而手動執行這個任務卻沒有問題,這種情況一般是由於在crontab文件中沒有配置環境變量引起的。

在 crontab文件中定義多個調度任務時,需要特別注意的一個問題就是環境變量的設置,因爲我們手動執行某個任務時,是在當前shell環境下進行的,程 序當然能找到環境變量,而系統自動執行任務調度時,是不會加載任何環境變量的,因此,就需要在crontab文件中指定任務運行所需的所有環境變量,這 樣,系統執行任務調度時就沒有問題了。

不要假定cron知道所需要的特殊環境,它其實並不知道。所以你要保證在shelll腳本中提供所有必要的路徑和環境變量,除了一些自動設置的全局變量。所以注意如下3點:

1)腳本中涉及文件路徑時寫全局路徑;

2)腳本執行要用到java或其他環境變量時,通過source命令引入環境變量,如:

cat start_cbp.sh
#!/bin/sh
source /etc/profile
export RUN_CONF=/home/d139/conf/platform/cbp/cbp_jboss.conf
/usr/local/jboss-4.0.5/bin/run.sh -c mev &

3)當手動執行腳本OK,但是crontab死活不執行時。這時必須大膽懷疑是環境變量惹的禍,並可以嘗試在crontab中直接引入環境變量解決問題。如:

0 * * * * . /etc/profile;/bin/sh /var/www/java/audit_no_count/bin/restart_audit.sh
  • 其它

新創建的cron job,不會馬上執行,至少要過2分鐘才執行。如果重啓cron則馬上執行。

8、創建cron腳本

第一步:寫cron腳本文件,命名爲crontest.cron。
15,30,45,59 * * * * echo "xgmtest....." >> xgmtest.txt 表示,每隔15分鐘,執行打印一次命令
第二步:添加定時任務。執行命令 “crontab crontest.cron”。搞定
第三步:"crontab -l" 查看定時任務是否成功或者檢測/var/spool/cron下是否生成對應cron腳本

注意:這操作是直接替換該用戶下的crontab,而不是新增。

二、at

at 只能執行一次,在一個指定的時間執行一個指定任務,只能執行一次,且需要開啓atd進程。

at是英語 "在...時刻" 的意思。

1、語法

at [-V] [-q queue] [-f file] [-mMlv] timespec...
       at [-V] [-q queue] [-f file] [-mMkv] [-t time]
       at -c job [job...]
       atq [-V] [-q queue]
       at [-rd] job [job...]
       atrm [-V] job [job...]
       batch
       at -b

2、at服務

/etc/init.d/atd {start|stop|restart|force-reload|status}

或:

service atd {start|stop|status|restart|reload|force-reload}

查看at進程:

root@Chan:~# ps -ef|grep atd
daemon     622     1  0  2018 ?        00:00:00 /usr/sbin/atd -f
root      2986  2693  0 18:22 pts/5    00:00:00 grep --color=auto atd

3、用法
1)在指定時刻執行程序
先用at命令後接想要程序執行的確定時刻;
再輸入你想要在以上指定時刻執行的命令。
如:

root@Chan:/var/www# at 0:55
warning: commands will be executed using /bin/sh
at> touch a_test.txt
at> <EOT>
job 2 at Tue Mar  5 00:55:00 2019

說明:

  • 輸入命令結束時可以按Ctrl + D組合鍵,這時at會顯示<EOT>。
  • 如果是想在明天執行指定任務則可以在時間後面加上tomorrow,如:at 0:55 tomorrow
  • 如果想指定具體日期,則如:at 17:12 4/5/19,依次是月/日/年

2)在指定間隔之後執行程序
例如,在10分鐘之後執行指定程序:

root@Chan:/var/www# at now +2 minutes
warning: commands will be executed using /bin/sh
at> touch a_test2.txt
at> <EOT>
job 3 at Tue Mar  5 01:03:00 2019

還可以使用其它的關鍵字,如:
minutes、hours、days、weeks、months、years
例如:at now +7 weeks 表示在距今7個星期之後執行。

4、atq和atrm
每次我們用at命令指定延時執行的命令,at都會爲其分配一個job編號,比如我們上面的兩個例子,分別被分配了2和3的編號。2號是touch a_test.txt,3號是touch a_test2.txt

atq命令可以列出正等待執行的at任務。q是英語queue的首字母,queue表示“隊列”,會列出at命令的任務隊列。

atrm命令可以刪除正在等待執行的at任務。rm是英語remove的首字母,remove表示“刪除”。後接at任務的編號。例如1和2這樣的編號。

三、經典案例

1、如何實現每天0點鐘重新啓動服務器。

0 0 * * * reboot

參考

1、查看命令:man 5 crontabman at
2、Ubuntu官方文檔:https://help.ubuntu.com/community/CronHowto
3、https://www.jianshu.com/p/e62de8adb6e0

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章