Linux/ubuntu定时运行python程序/运行程序失败原因

我用的是ubuntu 20.04.

1.vi /etc/crontab命令

这个命令只有root用户才可以使用,所以应该是sudo vi /etc/crontab才够完整。输入命令,系统输出如下结果:

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name command to be executed
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

由于我并没有使用此命令,并且此命令使用方法和crontab -e命令相似,所以不展开来讲解。

2.crontab -e命令

这一命令是在当前用户下创建自动执行程序任务。
输入crontab -e,在文件末尾追加你想要运行的命令(程序),就可以自动执行任务。命令的基本格式为:

m  h  dom mon dow   command
分 时  天  月   周    命令

2.1频率设置

  • m表示每小时的第几分钟执行command
  • h表示每天的第几小时执行command
  • dom表示每月小时的第几天执行command
  • mon表示每年的第几个月执行command
  • dow表示每周的第几天执行command

例如:

*/3 * * * *表示每三分钟执行一次。
1,3,5 * * * *表示每小时的第1,3,5分钟各执行一次。
1-5 * * * *表示每小时的第1,2,3,4,5分钟各执行一次。
* 1,2 * * *表示每天的凌晨一点和两点各执行一次。

2.2命令设置

例如:

*/1 * * * * /usr/bin/python /home/ubuntu/auto/auto_test.py >> /home/ubuntu/auto/auto_test.log 2>&1

这个命令的意思是每一分钟通过/usr/bin/python运行一次/home/ubuntu/auto/auto_test.py程序,并把日志输出到/home/ubuntu/auto/auto_test.log
这样,程序里的print就会输出到auto_test.log里。这里的2>&1表示把错误的输出也输出到标准输出(print),即平时运行出错时,出现在控制台里的错误提示也会被写到auto_test.log里。
当然,>> /home/ubuntu/auto/auto_test.log 2>&1这一段可以不写。

3.错误分析

像上面那样写的命令,当程序auto_test.py例如为以下内容时,crontab可能会如愿地执行程序:

print("Hello,world!")

程序运行后,/home/ubuntu/auto/auto_test.log里会有一行一行的Hello,world!

然而,你的程序却不一定能正常运行。
这里总结了如下原因:

3.1.python路径原因

你可能注意到,上面的例子中,我用了python的绝对路径/usr/bin/python,这是因为,你的系统中可能会有多个版本的python,如果没有使用绝对路径,可能会使系统找不到你的python。这位博主就是一个例子。
另外,你可以使用which python查看你的python路径。

3.2. 文件权限原因

假如你是在用户ubuntu下设置的定时任务,如果ubuntu对程序auto_test.py没有执行权限,当然就不能定时执行程序了。可以用命令sudo chmod 777 /home/ubuntu/auto/auto_test.py来增加权限。

3.3. 文件路径原因

比如auto_test.py为以下内容:

with open('auto_test.txt', 'a+') as f:
   f.write("Hello,world!\n")
   f.close()

auto_test.txt是和auto_test.py同一目录下的文件即:/home/ubuntu/auto/auto_test.txt,当程序执行时,系统并找不到这个文件。所以,你应该把'auto_test.txt'替换为/home/ubuntu/auto/auto_test.txt
可是,可能你的项目里不止一个文件,里面有好多文件路径,为了移植方便,不需要频繁改变路径,还是用相对路径比较方便。所以,这里推荐使用.sh脚本的方法。
例如,在目录/home/ubuntu/auto下新建auto_test.sh

#! /bin/bash
cd /home/ubuntu/auto/
python auto_test.py

crontab的定时执行任务设置为:

*/1 * * * * /home/ubuntu/auto/auto_test.sh >> /home/ubuntu/auto/auto_test.log 2>&1

由于auto_test.shcd到了项目目录里,就不用写绝对路径了。所以推荐这种使用脚本的方法。

4.其他命令

# crontab 启动
sudo service cron start
# crontab 停止
sudo service cron stop
# crontab 重启
sudo service cron restart
# 列出crontab 任务列表
crontab -l
  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值