running command in background
I am using python subprocess module to run some command and store its output in background. The command is deployed on my machine. Now whenever i run the command from shell prompt it works fine. But when I try to run the same command using subprocess module it gives following error
The command to be executed is vxswadm listswitch all
process = subprocess.Popen('vxswadm listswitch all > tmp.txt &',shell=True)
>>> Traceback (most recent call last):
File "/usr/bin/vxswadm", line 30, in <module>
l.uname = os.getlogin()
OSError: [Errno 25] Inappropriate ioctl for device
Can anyone help me out to fix this error . Any suggestions will be helpful. Thanks in advance
Tazim
The problem is likely due to the bash shell terminating immediately after the &
and sending the SIGHUP singal to all of it's subprocesses (standard shell behavior).
You can use the subprocess module to directly execute the command and can redirect the output to tmp.txt
yourself by first opening the file and then by passing it's file handle to the stdout
argument of the Popen call.
Try changing it to ['vxswadm', 'listswitch', 'all', '>', 'tmp.txt','&']
and/or changing shell to False
.
I think it might be the shell bit, though (if that fixes it).
You may also try adding stdin=subprocess.PIPE, stdout=subprocess.PIPE
, though I doubt that will affect it.
There is a problem with os.getlogin() and subprocessing and python. See http://code.activestate.com/lists/python-list/288845/
You need to use something else, such as:
pwd.getpwuid(os.getuid()).pw_name
(Unix only)
See also the discussion on a portable way to get the username.
链接地址: http://www.djcxy.com/p/79438.html上一篇: 项目管理使用GitHub
下一篇: 在后台运行命令