歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> linux使用cronlab定時任務

linux使用cronlab定時任務

日期:2017/3/3 13:39:47   编辑:Linux技術

第一次使用crontab 時,會出現

no crontab for root - using an empty one

“Select a editor ......”下面有幾個選項,就是叫你選擇編輯器。(選vim)就可以了,我選的第三個。

但是如果你選錯了,就可能會遇到點麻煩,沒有辦法正常編輯,crontab -e。 怎麼辦?

執行這個命令:select-editor (針對crontab的一個命令), 可以讓你重新選一次。

然後執行 crontab -e

敲擊鍵盤符 a 開始編輯如下

# 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)

#

*/1 * * * * echo “Hello World.” >> /alidata/test

~

~

~

~

~

"/tmp/crontab.D4qssN/crontab" 23L, 961C

ctrl+c 退出編輯 輸入:wq 保存退出

在test 文件裡面 查看執行結果如下

echo "please enter two number"“Hello World.”

“Hello World.”

“Hello World.”

“Hello World.”

“Hello World.”

“Hello World.”

繼續執行一個sh腳本

echo "please enter two number" >> /alidata/result

發現執行沒有效果 單獨執行這個文件

提示權限不夠

sudo chmod +x xxxx 把xxxx換成那個文件名

再次執行就可以了

ps:

linux下crontab每隔5分鐘執行一次任務的寫法

有兩種寫法

第一種寫法是*/5,這種寫法有的系統會不支持

*/5 * * * * /xxx/task.sh

第二種寫法比較繁瑣,但所有系統都支持:

0,5,10,15,20,25,30,35,40,45,50,55 * * * * /xx/task.sh

Copyright © Linux教程網 All Rights Reserved