Invoking history command on terminal via Python script - python

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()

Related

Subprocess no output when running a shell

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.

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

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

subprocess.Popen in different console

I hope this is not a duplicate.
I'm trying to use subprocess.Popen() to open a script in a separate console. I've tried setting the shell=True parameter but that didn't do the trick.
I use a 32 bit Python 2.7 on a 64 bit Windows 7.
To open in a different console, do (tested on Win7 / Python 3):
from subprocess import Popen, CREATE_NEW_CONSOLE
Popen('cmd', creationflags=CREATE_NEW_CONSOLE)
input('Enter to exit from Python script...')
Related
How can I spawn new shells to run python scripts from a base python script?
from subprocess import *
c = 'dir' #Windows
handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True)
print handle.stdout.read()
handle.flush()
If you don't use shell=True you'll have to supply Popen() with a list instead of a command string, example:
c = ['ls', '-l'] #Linux
and then open it without shell.
handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE)
print handle.stdout.read()
handle.flush()
This is the most manual and flexible way you can call a subprocess from Python.
If you just want the output, go for:
from subproccess import check_output
print check_output('dir')
To open a new console GUI window and execute X:
import os
os.system("start cmd /K dir") #/K remains the window, /C executes and dies (popup)
On Linux shell=True will do the trick:
command = 'python someFile.py'
subprocess.Popen('xterm -hold -e "%s"' % command)
Doesn't work with gnome-terminal as described here:
https://bbs.archlinux.org/viewtopic.php?id=180103

Categories