Ubuntu系统cron服务运行 .sh 脚本

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格式说明

字段是否必填允许值允许特殊字符备注
Seconds0–59*,-标准实现不支持此字段。
Minutes0–59*,-
Hours0–23*,-
Day of month1–31*,-?LW?LW只有部分软件实现了
Month1–12 or JAN–DEC*,-
Day of week0–7 or SUN–SAT*,-?L#?L#只有部分软件实现了
Linux和Spring的允许值为0-7,0和7为周日
Quartz的允许值为1-7,1为周日
Year1970–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亲笔。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值