3

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

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
tazim
  • 585
  • 2
  • 7
  • 17
  • It could be either the way your handling stdout, or an error occuring in stdout itself. Have you been able to successfully run the command by itself outside of python? – Evan Plaice Jun 23 '10 at 10:33
  • yes command works properly on shell prompt – tazim Jun 23 '10 at 10:35

3 Answers3

2

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
  • 7,779
  • 24
  • 25
2

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.

Community
  • 1
  • 1
brita_
  • 487
  • 7
  • 13
0

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
  • 49,299
  • 29
  • 200
  • 290