【发布时间】:2021-01-27 14:45:54
【问题描述】:
我有一个从普通 shell 启动时运行良好的 python 脚本。脚本的最终结果是使用subprocess 运行一些命令并将输出写入许多文件通过 cron 运行时,脚本运行但文件从未创建,我知道它运行,因为它将一些输出记录到 syslog,但是我似乎无法让 cron 写入文件。
这是 cron 设置:
SHELL=/bin/bash
0 14 * * * echo 'source ~/venv/python3.8/bin/activate; python ~/scripts/myscript.py' | /bin/bash
脚本的相关部分:
for line in mylist:
cmd = f"/usr/local/bin/bgpq3 -J -A -z {line}"
filename = f"{line}.txt"
logging.debug(f"Running {cmd}")
ret = subprocess.run(
cmd, shell=True, text=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
if ret.returncode == 0:
f = open(filename, "w")
f.write(ret.stdout)
f.close()
else:
print(ret.stderr)
logging.info(f"{ret.stderr} {line}")
结果可能会根据列表创建许多文件。就像我说的,这一切都在从普通 shell 启动时完美运行,并且脚本在使用 cron 启动时运行,但从未创建/写入文件。
谢谢
【问题讨论】: