1、安装cron
# 安装
apt-get install cron
2、常用命令
# 查看状态
sudo systemctl status cron
# 开启服务
sudo systemctl start cron
# 停止服务
sudo systemctl stop cron
# 重启服务
sudo systemctl restart cron
3、编写要定时执行 hello.sh 脚本
#!/bin/bash
echo "Hello"
4、确保脚本有可执行权限
chmod +x hello.sh
5、打开当前用户的crontab定时任务列表
注:没有则会新建文件,位置在:/var/spool/cron/crontabs/下。Ctrl+X保存
crontab -e
6、在crontab文件中添加定时任务
例:每5分钟运行一次hello.sh脚本
*/5 * * * * /mnt/project/sinotmemc/check/hello.sh
7、保存并关闭crontab文件
8、重启cron服务以加载新任务
sudo systemctl restart cron
- 完成Ubuntu系统上的cron服务以每5分钟自动运行一次 hello.sh 脚本。
9、查看当前用户的crontab定时任务列表
crontab -l
10、删除当前用户的crontab定时任务列表
crontab -r
11、 Crontab格式说明
| 字段 | 是否必填 | 允许值 | 允许特殊字符 | 备注 |
|---|---|---|---|---|
| Seconds | 是 | 0–59 | *,- | 标准实现不支持此字段。 |
| Minutes | 是 | 0–59 | *,- | |
| Hours | 是 | 0–23 | *,- | |
| Day of month | 是 | 1–31 | *,-?LW | ?LW只有部分软件实现了 |
| Month | 是 | 1–12 or JAN–DEC | *,- | |
| Day of week | 是 | 0–7 or SUN–SAT | *,-?L# | ?L#只有部分软件实现了Linux和Spring的允许值为0-7,0和7为周日 Quartz的允许值为1-7,1为周日 |
| Year | 否 | 1970–2099 | *,- | 标准实现不支持此字段。 |
说明:Linux* * * * *- - - - -| | | | || | | | +----- day of week (0 - 7) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat| | | +---------- month (1 - 12) OR jan,feb,mar,apr ...| | +--------------- day of month (1 - 31)| +-------------------- hour (0 - 23)+------------------------- minute (0 - 59)
参考:crontab执行时间计算 - 在线工具
转载请注明出处:BestEternity亲笔。