crontab工具來做計划任務,定時任務,執行某個腳本等等
1.檢查是否已安裝crontab
# crontab -bash: crontab: command not found
執行 crontab 命令如果報 command not found,表明沒有安裝.
2.yum安裝
1 # yum -y install vixie-cron 2 Loaded plugins: security 3 base | 3.7 kB 00:00 4 epel | 4.7 kB 00:00 5 epel/primary_db | 6.0 MB 00:00 6 extras | 3.4 kB 00:00 7 extras/primary_db | 29 kB 00:00 8 updates | 3.4 kB 00:00 9 updates/primary_db | 5.3 MB 00:00 10 Setting up Install Process 11 Resolving Dependencies 12 --> Running transaction check 13 ---> Package cronie.x86_64 0:1.4.4-16.el6_8.2 will be installed 14 --> Processing Dependency: dailyjobs for package: cronie-1.4.4-16.el6_8.2.x86_64 15 --> Processing Dependency: /usr/sbin/sendmail for package: cronie-1.4.4-16.el6_8.2.x86_64 16 --> Running transaction check 17 ---> Package cronie-anacron.x86_64 0:1.4.4-16.el6_8.2 will be installed 18 --> Processing Dependency: crontabs for package: cronie-anacron-1.4.4-16.el6_8.2.x86_64 19 ---> Package exim.x86_64 0:4.89-2.el6 will be installed 20 --> Running transaction check 21 ---> Package crontabs.noarch 0:1.10-33.el6 will be installed 22 --> Finished Dependency Resolution 23 24 Dependencies Resolved 25 26 =============================================================================================================================================================================================== 27 Package Arch Version Repository Size 28 =============================================================================================================================================================================================== 29 Installing: 30 cronie x86_64 1.4.4-16.el6_8.2 base 75 k 31 Installing for dependencies: 32 cronie-anacron x86_64 1.4.4-16.el6_8.2 base 31 k 33 crontabs noarch 1.10-33.el6 base 10 k 34 exim x86_64 4.89-2.el6 epel 1.4 M 35 36 Transaction Summary 37 =============================================================================================================================================================================================== 38 Install 4 Package(s) 39 40 Total download size: 1.5 M 41 Installed size: 4.3 M 42 Downloading Packages: 43 (1/4): cronie-1.4.4-16.el6_8.2.x86_64.rpm | 75 kB 00:00 44 (2/4): cronie-anacron-1.4.4-16.el6_8.2.x86_64.rpm | 31 kB 00:00 45 (3/4): crontabs-1.10-33.el6.noarch.rpm | 10 kB 00:00 46 (4/4): exim-4.89-2.el6.x86_64.rpm | 1.4 MB 00:00 47 ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 48 Total 10 MB/s | 1.5 MB 00:00 49 Running rpm_check_debug 50 Running Transaction Test 51 Transaction Test Succeeded 52 Running Transaction 53 Installing : exim-4.89-2.el6.x86_64 1/4 54 Installing : cronie-1.4.4-16.el6_8.2.x86_64 2/4 55 Installing : crontabs-1.10-33.el6.noarch 3/4 56 Installing : cronie-anacron-1.4.4-16.el6_8.2.x86_64 4/4 57 Verifying : crontabs-1.10-33.el6.noarch 1/4 58 Verifying : cronie-anacron-1.4.4-16.el6_8.2.x86_64 2/4 59 Verifying : exim-4.89-2.el6.x86_64 3/4 60 Verifying : cronie-1.4.4-16.el6_8.2.x86_64 4/4 61 62 Installed: 63 cronie.x86_64 0:1.4.4-16.el6_8.2 64 65 Dependency Installed: 66 cronie-anacron.x86_64 0:1.4.4-16.el6_8.2 crontabs.noarch 0:1.10-33.el6 exim.x86_64 0:4.89-2.el6 67 68 Complete!
安裝完成后,可使用 info crontab 命令查看詳細的幫助信息
cron服務提供crontab命令來設定cron服務的,以下是這個命令的一些參數與說明:
crontab -u //設定某個用戶的cron服務,一般root用戶在執行這個命令的時候需要此參數
crontab -l //列出某個用戶cron服務的詳細內容
crontab -r //刪除沒個用戶的cron服務
crontab -e //編輯某個用戶的cron服務
比如說root查看自己的cron設置:crontab -u root -l
再例如,root想刪除fred的cron設置:crontab -u fred -r
在編輯cron服務時,編輯的內容有一些格式和約定,輸入:crontab -u root -e
crontab服務狀態操作:
/sbin/service crond start //啟動服務
/sbin/service crond stop //關閉服務
/sbin/service crond restart //重啟服務
/sbin/service crond reload //重新載入配置
/sbin/service crond status //查看狀態
或者使用
# service crond start
# service crond stop
# service crond restart
# service crond reload
# service crond status
添加到開機服務
在/etc/rc.d/rc.local這個腳本的末尾加上:
/sbin/service crond start