0

I've looked for similar questions, but none of which provided an answer to this issue. Apologize if I missed something.

I'm trying to redirect output of subprocess. I simply want to discard it. When I'm using the call function of subprocess, the command works:

>>> import subprocess as sp
>>> cmd_l = ['service', 'iptables', 'restart']
>>> sp.call(cmd_l)
Redirecting to /bin/systemctl restart  iptables.service
0

But when I'm using Popen with stdout and stderr params,

>>> sp.Popen(cmd_l, stdout=None, stderr=None)
<subprocess.Popen object at 0x7fbc74a6ed90>
>>> Redirecting to /bin/systemctl restart  iptables.service

The shell hangs after the command until its timedout. Also, it still prints the command output to the terminal. I thought it might be related to the stdout and stderr params, so I tried the command without them, but the outcome was the same.

The script is ran locally on the machine.

Why does this happen? How can I discard the output of the command and just keep the return code of the execution? (without using the shell=True option)


Update: I've found this answer useful for my case, and this does what I attempted to do:

with open(os.devnull, 'w') as devnull:
    sp.check_call(cmd, stdout=devnull, stderr=devnull)

Also this implementation works: sp.call(cmd, stdout=sp.PIPE, stderr=sp.PIPE) and ignoring the PIPEs data.

But my question regards why does the Popen execution hangs until SSH session times out? is yet to be answered.

According to the docs sp.call uses Popen and then wait on the proc. So why using only Popen seems to behave different?

Edit: I'm opening a SSH connection to my linux machine, and on that machine I execute in interactive python the command. After I execute Popen, it seems to hang (I get no output, like I'm waiting for command to respond) until I get Putty error that the connection was closed. I can reconnect afterwards.

Chen A.
  • 10,140
  • 3
  • 42
  • 61
  • "the shell hangs" -- are you sure? When multiple processes write to the same terminal, things can get confusing. In particular Python doesn't write its own prompt (">>>> ") again after the output from the subprocess is written to the terminal. – Ulrich Eckhardt Nov 05 '17 at 11:20
  • @UlrichEckhardt yes, I'm connected to the machine through SSH. After I run the command, it hangs until the session is closed. So I'm sure I don't get the prompt back – Chen A. Nov 05 '17 at 12:21
  • @Rawing using `os.devnull` as the output for stderr does what I need. However, I still don't get why the execution flow in my question hangs until the SSH is timedout, so I don't think this is a duplicate. – Chen A. Nov 05 '17 at 12:22
  • Can you be more specific about this ssh session you're referring to? – Mark Plotnick Nov 05 '17 at 14:26
  • @MarkPlotnick I've added an edit to my question, trying to better clarify what I encounter – Chen A. Nov 05 '17 at 14:30
  • How long is the time interval between the Popen call and the ssh connection closing? Is it more like 10 minutes (which would imply a firewall is doing it) or 120 minutes (more like a tcp timeout in the absense of keepalives). – Mark Plotnick Nov 05 '17 at 14:52
  • @MarkPlotnick it is more like 30 seconds.. – Chen A. Nov 05 '17 at 15:43
  • ...and what happens then? Some further directions to look at are the command itself (tweaking network stuff over network) and possibly the intermediate shell (is there one or not?). What happens if you eliminate the SSH connection? What if you use SSH instead of Putty for it? Anyhow, look at the output you quote above: It does return to the Python CLI, which you can see clearly from the ">>> " prompt, so your interpretation isn't fully correct. – Ulrich Eckhardt Nov 05 '17 at 22:03
  • If you run some other command instead of that systemctl, do you see the same behavior? I think restarting iptables might reset connection tracking and in some cases close your (and other users') connections, depending on how much output occurs on those connections during the restart. – Mark Plotnick Nov 05 '17 at 23:15
  • @MarkPlotnick I just tried a different command, and the connection is not being reset. It's strange, because restarting iptables don't reset SSH connections (if I execute solely the command, not thru python for example). – Chen A. Nov 06 '17 at 07:38

0 Answers0