Run ElasticSearch using Python - python

I am trying to turn on ElasticSearch node using python. I am doing this by running a shell script via python. I have tried the below commands in python to run the shell script :
import os
cmd = 'cd C:\Users\anoop\desktop\kibi\elasticsearch 2.3.3\bin && .\elasticsearch'
os.system(cmd)
from subprocess import call
call('cd C:\Users\anoop\desktop\kibi\elasticsearch 2.3.3\bin && .\elasticsearch')
import subprocess
p = subprocess.Popen('C:\Users\anoop\Desktop\RunES.bat', shell = True)
p.communicate()
None of the above three methods is working. Let me know if anyone can help on this!!

Related

How to run program in Cygwin using python (3.8) script from windows 10?

I am trying to open the Cygwin terminal with the help of the python (3.8.3) script on my windows 10 laptop and execute two separate commands.
import subprocess
from subprocess import Popen, PIPE
import subprocess
import os
os.chdir(b"folder path")
p = subprocess.Popen('C:\\cygwin64\\bin\\mintty.exe -i /Cygwin-Terminal.ico -',
stdin=subprocess.PIPE, stdout=subprocess.PIPE)
p.stdin.write(b"cd newfolder") #b in front to denote bytes object
p.stdin.write(b"mysecondcommand") #stdin doesn't take strings
p.stdin.close()
I can open the Cygwin terminal with python script but my commands are not executed, can anyone help with this?

How to solve 'AttributeError: 'module' object has no attribute 'run'' from subprocess.run()

I am creating a bash script which calls a python script that in turn runs other processes in bash using subprocess.run(). However, when the bash script runs the python script within it, in the line where subprocess.run is called, I get an error message:
run_metric = subprocess.run(command, shell=True, stdout = subprocess.PIPE, universal_newlines = True)
AttributeError: 'module' object has no attribute 'run'
1) I made sure I ran the script using python 3 by activating a conda environment with python=3.6, which should not bring me any problem to call subprocess.run. The interesting thing is that if I change subprocess.run() to subprocess.Popen() the script works, but I could not work out how to get run_metric.stdout properly.
2) I do not have any subprocess.py file within any directory I am working in
3) the result of print(subprocess.__file__) is showing me that python is not 3.6: /usr/lib/python2.7/subprocess.pyc
Also, I tried to use something like
from subprocess import run
and making sure in both the python script and the function I had import subprocess
The bash script is as follows:
SWC_FOLDER_PATH=$(pwd)
sudo chmod +x /media/leandroscholz/KINGSTON/Results_article/Tracing_data/run_metrics.py
echo "run /media/leandroscholz/Tracing_data/run_metrics.py ${SWC_FOLDER_PATH} /media/leandroscholz/KINGSTON/Results_article/TREEStoolbox_tree_fixed.swc"
python /media/leandroscholz/Tracing_data/run_metrics.py ${SWC_FOLDER_PATH} /media/leandroscholz/TREEStoolbox_tree_fixed.swc
And the python script I run calls a certain function that uses subprocess.run() this way (just part of the code where the problem arises):
import subprocess
import glob
import numpy as np
def compute_metrics(swc_folder_path, gt_file_path):
# first get list of files in swc_folder_path
swc_files = (glob.glob(swc_folder_path+"/*_fixed.swc"))
n_swc_files = len(swc_files)
workflow_dict = gets_workflow_dict(swc_files)
n_images = get_n_images(swc_files)
n_workflows = len(workflow_dict)
for swc in range(0,n_swc_files):
command = "java -jar /home/leandroscholz/DiademMetric.jar -G " + swc_files[swc] +" -T " + gt_file_path
run_metric = subprocess.run(command, shell=True, stdout = subprocess.PIPE, universal_newlines = True)
I am using subprocess.run within python because, in the end, I want to get a string of the run_metric.stdout after running the process in bash so I can later store it in an array and save it to a txt file.
I hope I was sufficiently clear and provided enough information.
Thanks!
After the comments received, I tested the output of print(subprocess.__file__), which showed that python being used was python2.7,
Thus, I changed the call of the python script from python script.py to python3 script.py. I've found this question, which also shows another way to call python programs from terminal.
Running Python File in Terminal

Execute shell command using Python subprocess

I want to execute the following command via a python script:
sudo cat </dev/tcp/time.nist.gov/13
I can execute this command via the command line completely fine. However, when I execute it using subprocess, I get an error:
Command ['sudo','cat','</dev/tcp/time.nist.gov/13'] returned non-zero exit status 1
My code is as follows
import subprocess
subprocess.check_output(['sudo','cat','</dev/tcp/time.nist.gov/13'])
As I mentioned above, executing the command via the command line gives the desired output without any error. I am using the Raspbian Jessie OS. Can someone point me in the right direction?
You don't want to use subprocess for this at all.
What does this command really do? It uses a bash extension to open a network socket, feeds it through cat(1) to reroute it to standard output, and decides to run cat as root. You don't really need the bash extension, or /bin/cat, or root privileges to do any of this in Python; you're looking for the socket library.
Here's an all-Python equivalent:
#!/usr/bin/env python3
import socket
s = socket.create_connection(('time.nist.gov', 13))
try:
print(s.recv(4096))
finally:
s.close()
(Note that all of my experimentation suggests that this connection works but the daytime server responds by closing immediately. For instance, the simpler shell invocation nc time.nist.gov 13 also returns empty string.)
Give this a try:
import subprocess
com = "sudo cat </dev/tcp/time.nist.gov/13"
subprocess.Popen(com, stdout = subprocess.PIPE, shell = True)

Use python to run multiple commands inside the wine cmd?

Using python I can get either of these to work:
subprocess.call(['wine', 'cmd'])
os.system("wine cmd")
I'm using Ubuntu and python 3.5, Once I get into the wine cmd prompt I can no longer run commands, non of the ways to run multiple commands that I have seen online work, they don't error out, it just opens the cmd and pauses, I think it treats the cmd once open as a running command and is waiting to move on to the next command which it assumes is for the shell not the wine cmd, how can i then run commands inside the wine cmd once opened?
edit: Basically any time I run a command that requires further user input from within that command, how do I interact inside of that command?
You could build up from DOS through BASH to python as in the example code here. I cut and paste the code into python 2.7 and it worked, but you might like to confirm on 3.5
If you specifically need interaction rather than just running a DOS command then you could use subprocess.Popen.communicate to interact with your script which then interacts with wine/dos.
import subprocess, os, stat
from subprocess import Popen
from subprocess import PIPE
from subprocess import check_output
command_script="/tmp/temp_script.sh"
f1 = open(command_script,'w')
f1.write("#!/bin/bash\n")
#to run a dos command
#f1.write(r'WINEPREFIX=/path/tp/wine/prefix wine cmd /c #mydoscommand argval1'+'\n')
#for example
f1.write(r'wine cmd /c #echo Hello_world'+'\n')
#or to run a specifically pathed executable
#f1.write(r'WINEPREFIX=/path/tp/wine/prefix wine "c:\\Program Files (x86)\\path\\to\\executable.exe" additionalargs '+'\n')
f1.close()
st = os.stat(command_script)
os.chmod(command_script, st.st_mode | stat.S_IEXEC)
p = Popen(command_script, stdin=PIPE, stdout=PIPE, stderr=PIPE)
output, err = p.communicate(b"input data that is passed to subprocess' stdin")
rc = p.returncode
print output
os.remove(command_script)
Have a look at the answers where I nicked some of the code from Running windows shell commands with python and calling-an-external-command-in-python

Python create shell for several processes

I have several command execution in python on Windows using subprocess.call(), but for each one I need to execute batch file with environmet setup before calling proper command, it looks like this
subprocess.call(precommand + command)
Is there way to "create" shell in python that will have batch file executed only once and in that shell command will be executed several times?
Write commands to a bat-file (tempfile.NamedTemporaryFile())
Run the bat-file (subprocess.check_call(bat_file.name))
(not tested):
#!/usr/bin/env python
from __future__ import print_function
import os
import subprocess
import tempfile
with tempfile.NamedTemporaryFile('w', suffix='.bat', delete=False) as bat_file:
print(precommand, file=bat_file)
print(command, file=bat_file)
rc = subprocess.call(bat_file.name)
os.remove(bat_file.name)
if rc != 0:
raise subprocess.CalledProcessError(rc, bat_file.name)
Do you need to get output from every command separately? If no - you can convey these commands using &&, || or ;
cd dir && cp test1 test2 && cd -

Categories