【问题标题】:crontab won't run os.system python commandcrontab 不会运行 os.system python 命令
【发布时间】:2018-07-26 20:33:38
【问题描述】:

使用 ubuntu 的 16.04 crontab 和 @reboot 来运行 python3 脚本。当我看到记录的输出时,脚本在重新启动时正常运行。但是,我的脚本的 os.system 命令没有运行。如果在 crontab 之外运行,它运行良好。我的脚本都是可执行的。

crontab -l 输出:

SHELL=/bin/bash
@reboot nohup /usr/bin/python3 -u /home/path/scheduler.py >> /path/log.out &

scheduler.py 代码:

#...(check if web server is running...if not restart)
os.system('nohup /usr/bin/python3 -u /path/webserver/main.py &')
print('this function ran')

当我记录 os.system 命令的输出时,没有输出。

附带说明一下,我正在运行 python 调度命令来检查网络服务器的总体健康状况。 crontab 似乎不是正确的工具,所以我只使用 crontab 在重启时启动我的 python 调度程序。

我使用烧瓶作为网络服务器,如果我可以让它工作,我会使用 gunicorn 和 systemctrl...但它没有,所以这是我的解决方法。

【问题讨论】:

  • 即使你找到了答案,这似乎也是个麻烦事。我建议您使用更标准的解决方案(supervisord 或 systemctl)解决您遇到的问题,而不是对自制解决方案进行故障排除。你遇到了什么样的问题?
  • @MrName,你可能是对的。在我回到 supervisord/systemctl 之前,这似乎是一个简单的解决方法。有了这两个和gunicorn,问题接踵而至。足以让我尝试找到一个更简单的解决方案(如 python schedule)。
  • 我肯定知道,去过那里,但可能非常令人沮丧。如果您需要帮助解决这些问题,请告诉我,我会尽力而为。

标签: python cron


【解决方案1】:

当我们尝试通过 os.system() 命令直接在 crontab 中运行 python 脚本时,我遇到了同样的问题。

制作launcher.sh

#!bin/bash
cd /home/pi/
sudo python example.py

然后,使您的脚本可执行:

chmod 755 launcher.sh

最后,将您的脚本添加到 crontab:

crontab -e

并在最后添加这一行:

@reboot sh /home/pi/launcher.sh
(I set the program to run at each reboot)

【讨论】:

    【解决方案2】:

    关键是,os.system调用的命令不在默认路径中。

    例如,tcpdump 不在 /usr/bin/ 中。

    所以,你可以通过添加命令的完整路径来解决问题。

    【讨论】:

      猜你喜欢
      • 2015-07-02
      • 1970-01-01
      • 2017-08-05
      • 1970-01-01
      • 2015-08-10
      • 2020-11-04
      • 2018-07-17
      • 1970-01-01
      • 2018-10-24
      相关资源
      最近更新 更多