实现crontab定时调用python脚本,以及command not found的问题

操作

1.修改 /etc/crontab文件
调用python脚本和其他sh的不同是:需要写清楚调用哪个python解释器
例如:
* 12 * * * root /usr/bin/python /home/admin/test.py
需要用/usr/bin/python 全路径指定.
另外需要在此前写root 表示调用账户.
2.增加日志
使用/home/admin/test.py.log 2>&1 把错误流重定向到标准输出流
全部配置如下:
* 12 * * * root /usr/bin/python /home/admin/test.py >> /home/admin/test.py.log 2>&1
***

问题

python脚本里调用了别的命令,如git命令,执行时可以执行,但crontab执行时显示command not found
比如我在python脚本里,subprocess.Popen来执行一个'git pull'命令.

def get_err_process_cmd(cmd):
    stdout, stderr = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
                                      stderr=subprocess.PIPE).communicate()
    print stdout
    return str(stderr)

# 直接 ./test.py可以顺利运行
err = get_err_process_cmd('git pull')

crontab配置后,则会是 bin\sh: git command not found
解决办法:
whereis git去找到git的安装路径,比如我的是 /usr/local/bin/git
然后在python脚本里替换:

git_home = '/usr/local/bin/git'
err = get_err_process_cmd(git_home +' pull')

这样crontab就能顺利执行

转载于:https://www.cnblogs.com/zhhiyp/p/10160754.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值