Nic*_*ick 5 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
Run Code Online (Sandbox Code Playgroud)
而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()
Run Code Online (Sandbox Code Playgroud)
这是输出日志
Tue, 27 Aug 2013 22:41:01 +0000
Running opengrok index..
Run Code Online (Sandbox Code Playgroud)
由于某种原因,脚本已开始运行,但脚本的其他部分尚未完成.我不确定它是OS故障还是cron故障或python.当我从命令行调用脚本时,脚本本身运行正常.
有谁知道为什么会这样?
你需要shell来运行cd命令.在您的crontab中定义sh或bash作为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
Run Code Online (Sandbox Code Playgroud)
或者在python中打开shell作为子进程.