Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

running command in background

Tags:

python

django

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

like image 771
tazim Avatar asked May 14 '26 15:05

tazim


2 Answers

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.

like image 133
Rakis Avatar answered May 16 '26 06:05

Rakis


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.

like image 23
brita_ Avatar answered May 16 '26 04:05

brita_



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!