【发布时间】:2014-10-14 10:13:57
【问题描述】:
当我通过 cron 或 rc.local
进行午餐时访问命令的输出 (stderr stdout) 时遇到问题它在常规 shell 中完美运行,但通过 rc.local 失败
cat /root/watchdog.py
import subprocess
cmd = ( 'echo "TEST" |gnokii --config /root/.config/gnokii/config --sendsms +123456789xx ')
#p = subprocess.Popen([cmd, '2>&1'], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
p = subprocess.Popen([cmd, '2>&1'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
output = p.stdout.read()
output += p.stderr.read()
logFile = open("/root/logfile", 'a+')
#####
#Idea to read line by line:
#output = ''
# for line in iter(p.stdout.readline,''):
# print "captured line: %s" % line.rstrip()
# #logFile.write(line.rstrip())
# output += line
logFile.write(output)
logFile.close()
从控制台运行时的输出如下:
/root/watchdog.py
GNOKII Version 0.6.30
Cannot open logfile /root/.cache/gnokii/gnokii-errors
WARNING: cannot open logfile, logs will be directed to stderr
Send succeeded with reference 186!
在我的 rc.local 中
/root/watchdog.py > /root/mywatchPY.out 2>&1 &
这看起来很有趣: Redirect subprocess stderr to stdout 但这并不能解决问题。
知道如何在没有完整外壳的情况下捕获子进程运行的 sdterr/stdout 吗?
【问题讨论】:
标签: python cron subprocess stdout