目录

Linux 定时任务之 crontab

Linux 定时任务之 crontab

1. crontab一般用法,crontab –e

  • crontab -e 第一次用会选择一下编辑器,vim还是nano自己看着来
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 编辑定时任务(重启cron服务才会生效)
crontab –e

# 重启cron服务,这可能重启一个就行,不放心的话都执行
service cron restart    # 这个比较新的系统可能不用
systemctl restart cron

# 列出定时任务
crontab –l

# 配置文件解读及示例
# minute hour day month week command
#  分    时   日   月    周   命令

# 举例如下
# 实在不会,记得是看Crontab表达式:https://www.toolnb.com/tools/croncreate.html
* * * * * sh /root/hello.sh  # 每1分钟执行 sh /root/hello.s
*/10 * * * * sh /root/hello.sh  # 每10分钟
* */2 * * * sh /root/hello.sh  # 每2小时
0 8 * * * sh /root/hello.sh  # 每天8点0分
0 8,9,10 * * * sh /root/hello.sh  # 每天8、9、10点
0 8-10 * * * sh /root/hello.sh  # 每天8、9、10点
0 8-20/3 * * * sh /root/hello.sh  # 每天8-20点中,每3小时执行一次
# 特殊的:开机触发
@reboot sh /root/hello.sh  # 开机执行

2. crontab高级用法,crontab 配置文件

  • crontab -e 其实是生成了个配置文件,我们也可以创建一个 crontab配置文件
  • 这个配置文件,简单来说是个脚本,可以设置 shebang、环境变量、指定用户,不过这几个都可不写
  • 允许一些简单的 shell语法
  • 举例一下配置文件,前两行可以不要
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#!/bin/env bash
export PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

# 开机触发
@reboot wx "PVE 开机了!"

# PVE
#5 8 * * * qm start 101
58 23 * * * /usr/sbin/shutdown -h now
56 23 * * * /usr/sbin/qm list | grep -q "101.*running" && /usr/sbin/qm shutdown 101
54 23 * * * /usr/sbin/qm list | grep -q "102.*running" && /usr/sbin/qm suspend 102 --todisk

# 更新ydns
10 * * * * bash /root/sh/ydns.sh > /dev/null

# 更新crontab
30 8 * * * /usr/bin/crontab ~/sh/cron.list && /usr/sbin/service cron restart && /usr/bin/systemctl restart cron
  • 使用方法:

    • 1
      2
      3
      4
      5
      6
      
       # 设置配置文件
      crontab ~/sh/cron.list
      
      # 重启cron服务,这可能重启一个就行,不放心的话都执行
      service cron restart
      systemctl restart cron
      

https://cdn.jsdelivr.net/gh/rentianyu/media@main/img/PicList/crontab_cheatsheet.png


3. 一些坑

  1. 一般来讲,crontab的前边时间的表达式就只有5位,除非你懂,否则别整花里胡哨的
  2. 如果没有导入环境变量(export PATH),一些命令就要用绝对路径,比如:/usr/sbin/service cron restart
  3. 修改完配置文件后一定要重启一下服务
  4. 有一些系统抽风,按常规方法改了也不生效,就得去改动 cron相关的底层文件
「感谢支持」