ubuntu 定时任务 Crontab

常用命令:

# 列出某个用户cron服务的详细内容
crontab -l
# 删除没个用户的cron服务
crontab -r
# 编辑某个用户的cron服务 
crontab -e 
# 重启服务
sudo /etc/init.d/cron restart
# 暂停服务
sudo /etc/init.d/cron stop
# 开启服务
sudo /etc/init.d/cron start

编辑一项任务:
命令说明
例如:
每分钟执行一次

* * * * * python hello.py

每小时的第12分钟执行一次

12 * * * * python hello.py

每小时的第12分钟和第30分钟执行一次

12,30 * * * * python hello.py

每天的5点12分钟执行一次

12 5 * * * python hello.py

每月的1号的5点12分钟执行一次

12 5 1 * * python hello.py

每年的6月1号的5点12分钟执行一次

12 5 1 6 * python hello.py

每周1的5点12分钟执行一次(周日是0)

12 5 * * 1 python hello.py

每3个小时的第12分钟执行一次

12 */3 * * * python hello.py

每小时的0-5分钟, 每分钟执行一次

0-5 * * * * python hello.py

简单的测试:

在用户目录下面生成log.txt文件, 并每分钟写入一次时间。
* * * * * date >> ~/log.txt

使用过程的一些坑

例如命令: * * * * * cd /home/hjl/test/ && python test.py

test项目的环境是anaconda3, 也已经添加好了环境变量, 默认执行的anaconda3的python。
我发现, 直接在终端输入: cd /home/hjl/test/ && python test.py 可以完美运行, 但是上面的定时任务并没有执行。后来打印错误发现, test.py文件里面的opencv没有导入成功, 查找原因发现, crontab运行时, 所使用的python环境并不是anaconda里面的python环境, 而是系统自带的python3.5。
于是, 在crontab里面强制说明python解释器, 解决了问题。
cd /home/hjl/test/ && /home/hjl/anaconda3/bin/python test.py

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值