Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Behaviour of subprocess.Popen, ssh and nohup I don't understand

922 views
Skip to first unread message

Adriaan Renting

unread,
Mar 31, 2011, 7:01:19 PM3/31/11
to pytho...@python.org
L.S.

I have a problem that a background process that I'm trying to start with
subprocess.Popen gets interrupted and starts waiting for input no matter
what I try to do to have it continue to run. It happens when I run it
with nohup in the background.
I've tried to find a solution searching the internet, but found none.
I've written a small test script that reproduces the problem and hope
maybe here there is someone who can tell me what's going wrong. Any
suggestions are welcome.

(renting)myhost> cat test.py
#!/usr/bin/python
# 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)
else:
print '2'
(output, output2) = comm.communicate(input=None)
print output
print output2
print '3'
time.sleep(3)

(renting)myhost> python --version
Python 2.5.2

(renting)myhost> nohup ./test.py -O2 &
[1] 15679

(renting)myhost> 1
2
22:40:30 up 24 days, 7:32, 1 user, load average: 0.00, 0.00, 0.00

None
3
1
2

[1] + 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, 0.00

None
3
1
2
22:40:56 up 24 days, 7:32, 1 user, load average: 0.00, 0.00, 0.00

None
3

(renting)myhost>

Now as you can see, it suspends on the second time through the for loop,
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 keys
a couple of times. The same happens when I would exit the shell, despite
it running with nohup.
I don't need to exit to make it suspend, any combination of a few random
keystrokes makes it do this. It seems depending on the timing though,
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 directly,
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,

thanks,

Adriaan Renting

Kushal Kumaran

unread,
Apr 1, 2011, 1:33:31 AM4/1/11
to Adriaan Renting, pytho...@python.org

What operating system is this? Try with stdin=open(os.devnull, 'rb')
to the Popen call instead. Also, this seems to be similar:
http://stackoverflow.com/questions/1365653/calling-fgets-on-popen-of-ssh-is-flushing-the-beginning-of-stdin-of-the-cal

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.

--
regards,
kushal

Adriaan Renting

unread,
Apr 6, 2011, 3:47:21 AM4/6/11
to Kushal Kumaran, pytho...@python.org

>>> On 4/1/2011 at 07:33 AM, Kushal Kumaran
<kushal.kum...@gmail.com>
wrote:

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
foreground process.

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.

Adriaan Renting.

Dan Stromberg

unread,
Apr 6, 2011, 1:43:19 PM4/6/11
to Adriaan Renting, pytho...@python.org
On Wed, Apr 6, 2011 at 12:47 AM, Adriaan Renting <ren...@astron.nl> wrote:
>
>
> 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
> foreground process.
>
> 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.
>
> Adriaan Renting.
> --
> http://mail.python.org/mailman/listinfo/python-list

http://docs.python.org/library/subprocess.html
"With None, no redirection will occur; the child’s file handles will
be inherited from the parent."

When a background process reads from a tty (which is most likely what
its inherited stdin is connected to), it gets a SIGTTIN, suspending
your background process.

See also "ssh -n".

Nobody

unread,
Apr 6, 2011, 7:35:02 PM4/6/11
to
On Wed, 06 Apr 2011 09:47:21 +0200, Adriaan Renting wrote:

> 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
> foreground process.
>
> 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.

"None" is the default value for stdin, stdout and stderr, and means
"do nothing and allow the child process to just inherit the descriptor
from the parent". It doesn't mean "close the descriptor"; the only way
to force a descriptor to be closed in the child is to use preexec_fn= and
have the function close the descriptor.

Adriaan Renting

unread,
Apr 7, 2011, 9:11:54 AM4/7/11
to Dan Stromberg, pytho...@python.org

Thanks, that explains a lot.

Adriaan Renting.


>>> On 4/6/2011 at 07:43 PM, Dan Stromberg <drsa...@gmail.com> wrote:

> On Wed, Apr 6, 2011 at 12:47 AM, Adriaan Renting <ren...@astron.nl>

wrote:
>>
>>
>> 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
>> foreground process.
>>
>> 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.
>>

> "With None, no redirection will occur; the child*s file handles

0 new messages