linux定時任務(wù) crontab

一、命令形式:

crontab命令的一般形式為:?

crontab [-u user] file?

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

-e (編輯) -l (查看列表) -r (刪除) -i (刪除之前提示) 如果使用自己的名字登錄,就不用使用-u選項,因為在執(zhí)行crontab命令時,該命令能夠 知道當(dāng)前的用戶

二、操作步驟

1、crontab -l 查看任務(wù)

2、如果有任務(wù),crontab -e 按下邊的格式添加任務(wù) (nano編輯的control+X為退出,Y保存)

15 * * * * bash /opt/fyh/a.sh > ~/fyh/a.log

3、如果沒有任務(wù) 編寫 crontab文件 fyhcron

EDITOR=vi; export EDITOR

15 * * * * bash /opt/fyh/a.sh > ~/fyh/a.log

4、 crontab fyhcron 添加任務(wù)

5、 crontab -l 查看任務(wù)

6、 定時任務(wù)格式

第1列分鐘1~5 9

第2列小時1~2 3(0表示子夜)

第3列日1~3 1

第4列月1~1 2

第5列星期0~6(0表示星期天)

第6列要運行的命令

三、cron的主配置

cron 是一個可以用來根據(jù)時間、日期、月份、星期的組合來調(diào)度對重復(fù)任務(wù)的執(zhí)行的守護進程。cron的主配置文件/etc/crontab如下,定時執(zhí)行相應(yīng)任務(wù)

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

0? 0? ? * * *? 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 )

0? 0? ? 1 * *? root? ? /opt/ssl/renew_cert.sh >/dev/null 2>&1

#

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容