【问题标题】:Setup cron job to execute python script inside the bash sh file设置 cron 作业以在 bash sh 文件中执行 python 脚本
【发布时间】:2017-11-16 01:02:42
【问题描述】:

我确实创建了一个 Cron 作业来运行 bash 文件,它从命令行运行正常,但是如果从 Cron 作业运行,它给了我一个错误提示“Python 不是命令”

我的 bash 文件名为:Myscript.sh

#!/bin/bash
cd /var/www/html/public_html/test_scripts
python script1.py serverlog.log 2&>1 &
python script2.py serverlog.log 2&>1 &

我的 cron 作业设置

30 5 * * 1-5 root bash Myscript.sh

有人知道我做错了什么并解决这个问题吗?

谢谢,

【问题讨论】:

  • 是否在 crontab 文件中正确设置了 PATH?
  • 是的,路径设置正确,放到Myscript.sh的位置

标签: bash cron sh


【解决方案1】:

您需要将完整路径放入 python。如果不知道路径,可以使用which 命令查找:

➜  ~ which python
/usr/bin/python

然后修改你的脚本

#!/bin/bash
cd /var/www/html/public_html/test_scripts
/usr/bin/python script1.py serverlog.log 2&>1 &
/usr/bin/python script2.py serverlog.log 2&>1 &

【讨论】:

  • 您好,将完整路径放入Python解释器后,错误消息消失了,但现在它抱怨从Cron运行时找不到某些模块,但是如果从命令行运行,那么所有好的。有人知道为什么吗?
  • 有类似问题,请查看:stackoverflow.com/questions/7970905/…
猜你喜欢
  • 1970-01-01
  • 2018-01-02
  • 2018-05-20
  • 2012-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多