tags:

views:

68

answers:

2

hi,

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

A: 

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.

Wayne Werner
+1  A: 

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.

Rakis