为什么运行 subprocess.Popen() 后无法写入文件?

为什么运行 subprocess.Popen() 后无法写入文件?

我无法写入文件后

> Popen() runs

conf_file='/home/'+ service_ac +'/conf/keep-allGateways-alive.cfg'

def find_gw_ips():
        fp=open(conf_file,'w')
        fp.write('hello') #this gets printed in file
        t = subprocess.Popen("ifconfig", stdout=subprocess.PIPE)
        result=t.stdout.read()
        fp.write('hello_2') #this does not get printed in file

find_gw_ips()

我使用 crontab 来运行此脚本,没有 crontab 也可以运行良好,没有任何问题

* * * * * python /home/vtu/bin/script.py vtu >/tmp/out.txt 2>&1

我正在使用 RHEL 7.4,python 2.7.5

我尝试重定向并收到错误:

Traceback (most recent call last):
  File "/home/vru/bin/keep-allGateways-alive.py", line 34, in <module>
    find_gw_ips()
  File "/home/vru/bin/keep-allGateways-alive.py", line 30, in find_gw_ips
    t = subprocess.Popen("ifconfig", stdout=subprocess.PIPE)
  File "/usr/lib64/python2.7/subprocess.py", line 711, in __init__
    errread, errwrite)
  File "/usr/lib64/python2.7/subprocess.py", line 1327, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory

相关内容