Subprocess no output when running a shell - python

I tried to run a system shell in Python subprocess module:
p = subprocess.Popen("/bin/bash", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p.wait()
print(p.communicate())
However, it printed (b'', b'')
I tried other shells too, but they all failed to work. (Zsh, Sh, and FiSH)
How can I get the output? Thanks in advance!

It works for me as you would expect I guess.
(base) tzane:~/python_test$ python test_1.py
whereis ls
exit
(b'ls: /bin/ls /usr/share/man/man1/ls.1.gz\n', b'')
(base) tzane:~/python_test$
You are not getting any output if you are not telling bash to output anything. Calling .wait() with pipes can cause deadlocks so I would do something like this instead
import subprocess
p = subprocess.Popen("/bin/bash", stdout=subprocess.PIPE, stderr=subprocess.PIPE)
try:
print(p.communicate(timeout=30))
except TimeoutExpired:
p.kill()
print(p.commnunicate())
as suggested in the documentation or just use subprocess.run if you don't have to interact with the subprocess and you just want to catch the output.

Related

Python: get stdout from background subprocess

i'm trying to get informations of a network interface on a linux machine with a python script, i.e. 'ifconfig -a eht0'. So i'm using the following code:
import subprocess
proc = subprocess.Popen('ifconfig -a eth0', shell=True, stdout=subprocess.PIPE)
proc.wait()
output = proc.communicate()[0]
Well if I execute the script from terminal with
python myScript.py
or with
python myScript.py &
it works fine, but when it is run from background (launched by crontab) without an active shell, i cannot get the output.
Any idea ?
Thanks
Have you tried to used "screen"?
proc = subprocess.Popen('screen ifconfig -a eth0', shell=True, stdout=subprocess.PIPE)
I'm not sure that it can work or not.
Try proc.stdout.readline() instead of communicate, also stderr=subprocess.STDOUT in subprocess.Popen() might help. Please post the results.
I found a solution to the problem, i guess that the system is not able to recognize the function ifconfig when executed by the crontab. So adding the full path to the subprocess allows the script to be executed properly:
`proc = subprocess.Popen('/sbin/ifconfig -a eth0',shell=True,stdout=subprocess.PIPE)
proc.wait()
output = proc.communicate()[0]`
and now i can manage the output string.
Thanks

Popen subprocess does not work inside a SublimeREPL?

I use conda to create a Python 2.7 environment including the R package. If I open a Python session in a console, I can check that R is indeed installed with the Popen constructor:
$ python
>>> from subprocess import Popen, PIPE
>>> proc = Popen(["which", "R"], stdout=PIPE, stderr=PIPE)
>>> proc.wait()
0
where the 0 means it is installed. But if I try the same commands from within a Sublime Text 3 REPL running under the exact same Python environment, I get a 1.
Why is this and how can I fix it?
You need to communicate:
proc = Popen(['which', 'python'], stdout=PIPE)
proc.communicate()
('/Users/Kelvin/virtualenvs/foo/bin/python\n', None)
wait just waits for the subprocess to complete and gives you the return code (which is 0 if its successful)
if you get a different error code (1 meaning it failed), I'd look into confirming your virtual environment. try sys.executable

getting output of command in subprocess

I saw a few threads for this, but everything there didn't help me.
I'm running a subprocess to run commands on cmd via python(using 2.7)
p = subprocess.Popen(["start", "cmd", "/k", command], shell=True)
This command works and everything, but I can't manage to capture the output of the command.
I tried check_output or specifying stdout=sp.PIPE or stdout=file, but it didn't work.
Any suggestion will be appreciated.
Thanks
check_output should work fine:
from subprocess import check_output
out = check_output(["echo", "Test"], shell=True)
Output of command:
>>> print out
Test

Invoking history command on terminal via Python script

I am running a python script which is using subprocess to execute "history" command on my Ubuntu terminal. Apparently,I am getting this error
history: not found
I got to know that history can not be invoked by any script by default.
What can I do to overcome this? Or any other possible alternatives.
readline.get_history_item() method isnt working either.
Use this:
from subprocess import Popen, PIPE, STDOUT
e = Popen("bash -i -c 'history -r;history' ", shell=True, stdin=PIPE, stdout=PIPE, stderr=STDOUT)
output = e.communicate()

Running a bash script from Python

I need to run a bash script from Python. I got it to work as follows:
import os
os.system("xterm -hold -e scipt.sh")
That isn't exactly what I am doing but pretty much the idea. That works fine, a new terminal window opens and I hold it for debugging purposes, but my problem is I need the python script to keep running even if that isn't finished. Any way I can do this?
I recommend you use subprocess module: docs
And you can
import subprocess
cmd = "xterm -hold -e scipt.sh"
# no block, it start a sub process.
p = subprocess.Popen(cmd , shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
# and you can block util the cmd execute finish
p.wait()
# or stdout, stderr = p.communicate()
For more info, read the docs,:).
edited misspellings

Categories