I have a python script which runs fine when started from a normal shell. The end result of the script is to run some commands using subprocess
and write the output to many files When running via cron, the script runs but the files are never created, i know it runs since it logs some output to syslog, but i cant seem to make cron write to the files.
Here is the cron setup:
SHELL=/bin/bash
0 14 * * * echo 'source ~/venv/python3.8/bin/activate; python ~/scripts/myscript.py' | /bin/bash
Relevant part of the script:
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}")
The result could be many files created depending on the list. Like i said, this all works perfectly when kicked off from a normal shell, and the script runs when started with cron, but the files are never created / written.
thanks
question from:
https://stackoverflow.com/questions/65921474/python-script-in-cronjob-not-writing-stdout-to-files 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…