python - crontab:python 脚本正在运行但不执行操作系统命令

标签 python linux crontab

我有这个 crontab 配置设置和以下脚本。

MAILTO="abc@avc.com"
41 15 * * * /usr/bin/python /home/atweb/Documents/opengrok/setup_and_restart.py >       /home/atweb/Documents/opengrok/restart_log.txt 2&>1

python脚本是这样的

import subprocess
import os
from time import gmtime, strftime


def main():
    print(strftime("%a, %d %b %Y %X +0000", gmtime()))
    print('Running opengrok index..')
    subprocess.call(["cd", "/home/atweb/Documents/opengrok"])
    subprocess.call(["./stop_website"])
    print('Stopped website...')
    subprocess.call(["./index_opengrok"])
    print('finished indexing...')
    subprocess.call(["./setup_opengrok"])
    print('setup finished...')
    subprocess.call(["./start_website"])
    print('Finished opengrok index..')

if  __name__ =='__main__':main()

这是输出日志

Tue, 27 Aug 2013 22:41:01 +0000
Running opengrok index..

由于某种原因,脚本已经开始运行,但脚本的其他部分尚未完成。 我不确定它是操作系统故障还是 cron 故障还是 python。当我从命令行调用它时,脚本本身运行良好。

有人知道为什么会这样吗?

最佳答案

你需要 shell 来运行 cd 命令。在您的 crontab 中将 shbash 定义为 SHELL。

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
MAILTO="abc@avc.com" 
# m h dom mon dow   command
41 15 * * * /usr/bin/python /home/atweb/Documents/opengrok/setup_and_restart.py >       /home/atweb/Documents/opengrok/restart_log.txt 2&>1

或者在 python 中打开 shell 作为子进程。

关于python - crontab:python 脚本正在运行但不执行操作系统命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18476757/

相关文章:

python - 内部装饰器函数如何查看装饰函数参数?

python 数学公式模块/框架?

linux - find + sed 未按预期工作

git pull 自动通过 crontab

python - 在 Python 中使用 "self"访问父方法的问题

linux - Tomcat 未启动 : Permission denied

linux - Grep 按模式搜索字符串,然后在另一个文件中找到该字符串的一部分

linux - Raspberry pi 中的 Crontab 不运行非常简单的脚本

ubuntu - Crontab 不适用于 JAR 文件

python - tensorflow 1.10+ : passing epoch to estimator input_fn?