Linux定时任务

一、安装

yum install -y crontabs

二、常用命令

# 查看定时任务
crontab -l
# 启动和查看定时任务
systemctl start crond
systemctl stop crond
systemctl restart crond
systemctl reload crond
systemctl status crond
service crond restart
service crond status
# 编辑定时任务
crontab -e
# 定时任务的每段为:分,时,日,月,周,用户,命令
* * * * * user-name command to be executed
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令

三、常用定时任务

# 每五分钟执行一次
*/5 * * * * command
# 每一分钟执行一次
*/1 * * * *
# 每十分钟执行一次
*/10 * * * *
# 每一小时的第0分钟执行一次
0 */1 * * *
# 每小时的第1分钟执行一次
* */1 * * * 
# 每天晚上0点定时执行一次
0 0 * * *
# 每小时的第3和第15分钟执行
3,15 * * * *
# 在上午8点到11点的第3和第15分钟执行
3,15 8-11 * * *
# 每个小时的第5分钟执行
5 */1 * * *
# 每天的凌晨2点05分执行
5 02 * * *
# 每1分钟执行一次
* * * * *
# 每隔两天的上午8点到11点的第3和第15分钟执行
3,15 8-11 */2  *  *
# 每周一上午8点到11点的第3和第15分钟执行
3,15 8-11 * * 1
# 每晚的21:30
30 21 * * *
# 每月1、10、22日的4 : 45
45 4 1,10,22 * *
# 每周六、周日的1 : 10
10 1 * * 6,0
# 每天18 : 00至23 : 00之间每隔30分钟
0,30 18-23 * * *
# 每星期六的晚上11 : 00 pm
0 23 * * 6
# 每一小时
* */1 * * *
# 晚上11点到早上7点之间,每隔一小时
* 23-7/1 * * *
# 实现秒级任务20秒
* * * * * /bin/echo  "Hello"  >> /var/log/cron.log
* * * * * sleep  20 ;/bin/echo  "Hello"  >> /server/scripts/hello.log
* * * * * sleep  40 ;/bin/echo  "Hello"  >> /server/scripts/hello.log

四、常见错误

问题:

Dec  6 07:00:01 iz2zeau84juzpalekyrpfmz crond: sendmail: fatal: parameter inet_interfaces: no local interface found for ::1

这段内容表达的是 参数inet_interfaces::1没有找到本地接口,不只是设置定时任务的时候会遇到这个问题,其他关于本地接口使用时都有可能遇到这个问题,下面就是解决方案。

解决:

# 修改配置文件
vim /etc/postfix/main.cf
将配置文件内容
--------------------------
inet_interfaces = localhost
inet_protocols = all
--------------------------
修改为以下内容
inet_interfaces = all
inet_protocols = all
--------------------------

作者(Author):小强崽
来源(Source):https://www.wuduoqiang.com/archives/Linux定时任务
协议(License):署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)
版权(Copyright):商业转载请联系作者获得授权,非商业转载请注明出处。 For commercial use, please contact the author for authorization. For non-commercial use, please indicate the source.

posted @ 2021-03-13 20:12  小强崽  阅读(146)  评论(0编辑  收藏  举报