Ubuntu Linux 下面的計划任務部署詳解


Linux下面的任務部署大家都知道是利用crontab來實現的,至於crontab的一些類型我這里就不詳細的來闡述,例如:

-e (edit user's crontab)--編輯當前的計划任務
-l (list user's crontab)--查看當前的計划任務
-r (delete user's crontab)--刪除當前的計划任務
-ri (prompt before deleting user's crontab)--刪除前給出確認,-r的話就是直接刪除,通常使用下面的,-ri刪除操作危險

king@ubuntu:/$ crontab -ri
crontab: really delete king's crontab? (y/n)

--提示你是否刪除

格式:

 大概的語法知道了,下面我們就從一個0起步的人做起,此時我們的系統里面的計划任務就是一個空白,沒有設置任何計划任務,也可以創建一個新用戶登錄查看

step1:以study 用戶登錄執行crontab -l命令

study@ubuntu:/$ crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# For example, you can run a backup of all your user accounts
# 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

study@ubuntu:/$

可以看出最后面的不帶#注釋后面什么也沒有,切換一個用計划任務的用戶king,再次執行crontab -l

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# 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
*/2 * * * * date >>/home/king/b1/time.txt
king@ubuntu:/$

大家可以清楚的看到這一行:*/2 * * * * date >>/home/king/b1/time.txt

這個就是一個每隔2分鍾讀取當前時間輸出到/home/king/b1/time.txt這個文件中,下面我們切換回study用戶

crontab -e

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# For example, you can run a backup of all your user accounts
# 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

~
"/tmp/crontab.Aulssl/crontab" 22L, 886C

我們發現不能編輯,此時就要切換一下到編輯模式:i 進入編輯模式 輸入

*/1 * * * * date >>/home/study/test/testdate.txt

然后按Esc退出編輯模式

然后按Shift+: 保存 

然后按wq 退出

編輯成功 crontab -l 查看我們編輯的任務

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# For example, you can run a backup of all your user accounts
# 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 * * * * date >>/home/study/test/testdate.txt
study@ubuntu:/$

OK 部署上去了,接着看部署執行效果 cd /home/study/test/

study@ubuntu:/$ cd /home/study/test

study@ubuntu:~/test$ cat testdate--這樣寫
cat: testdate: No such file or directory--提示不存在
study@ubuntu:~/test$ cat testdate.txt--cat 查看文件的全稱 ok 
Wed Mar 6 11:57:01 CST 2013
Wed Mar 6 11:58:02 CST 2013
Wed Mar 6 11:59:01 CST 2013
Wed Mar 6 12:00:01 CST 2013
Wed Mar 6 12:01:01 CST 2013
study@ubuntu:~/test$

最后要強調的是每一個crontab是針對當前用戶的,用戶king 有自己的計划任務 用戶study也有自己的計划任務 

 

可以看到 在文件夾里面已經按照每分鍾輸入格式顯示出來了。以上文章獻給初學者,共勉!

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM