linux定时任务 crontab

一、命令形式:

crontab命令的一般形式为: 

crontab [-u user] file 

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

-e (编辑) -l (查看列表) -r (删除) -i (删除之前提示) 如果使用自己的名字登录,就不用使用-u选项,因为在执行crontab命令时,该命令能够 知道当前的用户

二、操作步骤

1、crontab -l 查看任务

2、如果有任务,crontab -e 按下边的格式添加任务 (nano编辑的control+X为退出,Y保存)

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

3、如果没有任务 编写 crontab文件 fyhcron

EDITOR=vi; export EDITOR

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

4、 crontab fyhcron 添加任务

5、 crontab -l 查看任务

6、 定时任务格式

第1列分钟1~5 9

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

第3列日1~3 1

第4列月1~1 2

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

第6列要运行的命令

三、cron的主配置

cron 是一个可以用来根据时间、日期、月份、星期的组合来调度对重复任务的执行的守护进程。cron的主配置文件/etc/crontab如下,定时执行相应任务

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

#

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容