Behaviour of subprocess.Popen, ssh and nohup I don't understand
renting at astron.nl
Wed Apr 6 09:47:21 CEST 2011
>>> On 4/1/2011 at 07:33 AM, Kushal Kumaran
<kushal.kumaran+python at gmail.com>
> On Fri, Apr 1, 2011 at 4:31 AM, Adriaan Renting <renting at astron.nl>
>> I have a problem that a background process that I'm trying to start
>> subprocess.Popen gets interrupted and starts waiting for input no
>> what I try to do to have it continue to run. It happens when I run
>> with nohup in the background.
>> I've tried to find a solution searching the internet, but found
>> I've written a small test script that reproduces the problem and
>> maybe here there is someone who can tell me what's going wrong. Any
>> suggestions are welcome.
>> (renting)myhost> cat test.py
>> # script to test subprocess problem
>> import subprocess, sys, time
>> for f in range(3):
>> command = ["ssh", "-T", "localhost", "uptime"]
>> comm = subprocess.Popen(command, shell=False, stdin=None,
>> stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=True)
>> print '1'
>> if comm.returncode:
>> print "error: %i" % (comm.return_code)
>> print '2'
>> (output, output2) = comm.communicate(input=None)
>> print output
>> print output2
>> print '3'
>> (renting)myhost> python --version
>> Python 2.5.2
>> (renting)myhost> nohup ./test.py -O2 &
>>  15679
>> (renting)myhost> 1
>> 22:40:30 up 24 days, 7:32, 1 user, load average: 0.00, 0.00,
>>  + Suspended (tty input) ./test.py -O2
>> (renting)myhost> fg
>> ./test.py -O2
>> 22:40:35 up 24 days, 7:32, 1 user, load average: 0.00, 0.00,
>> 22:40:56 up 24 days, 7:32, 1 user, load average: 0.00, 0.00,
>> Now as you can see, it suspends on the second time through the for
>> until I bring it to the foreground and hit .
>> What you don't see, is that I make it do this by pushing the arrow
>> a couple of times. The same happens when I would exit the shell,
>> it running with nohup.
>> I don't need to exit to make it suspend, any combination of a few
>> keystrokes makes it do this. It seems depending on the timing
>> during the sleep(3) it seems to ignore me, only when subprocess is
>> actually running will it suspend if I generate keystrokes.
>> If the ssh command is executed without -T option it suspends
>> so I think it's related to the ssh command. I log in with a
>> public/private key pair to avoid having to enter a password.
>> Any suggestions are welcome,
> What operating system is this? Try with stdin=open(os.devnull,
> to the Popen call instead. Also, this seems to be similar:
> The "Suspended (tty input)" message means that a background process
> tried to read from stdin, so got suspended. This is part of the job
> control mechanism.
This solves the problem using stdin=open(os.devnull, 'rb') instead of
stdin=None makes it run even if there is input from stdin in the
The operating system is Ubuntu 8.04
I understood what Suspended (tty input) means. I don't understand why
it waits for input if stdin=None.
Thank you for your help.
More information about the Python-list