I need to run a command using python code and I tried to use both os.system and subprocess however both didn't work for some reason. Here's my code:
#app.route('/run-script')
def run_script():
subprocess.call('python3.6 GoReport.py --id 31-33 --format word', cwd="working_dir", shell=True)
return flask.render_template('results.html', **locals())
Running this command from terminal directly works as it should. Trying to reproduce this from python interpreter using command line works as as a charm as well. However it doesn't work when I use Flask. What's the reason for this?
So I've managed to edit my code and import the module instead of using subprocess and os.system. Thanks #tripleee for the explanation!
Related
I wrote a python script that calls a docker-compose exec command via subprocess.run. The script works as intended when I run it manually from the terminal, however I didn't get it to work as a cronjob yet.
The subprocess part looks like this:
subprocess.run(command, shell=True, capture_output= True, text=True, cwd='/home/ubuntu/directory_of_the_script/')
and the command itself is something like
docker-compose exec container_name python3 folder/scripts/another_script.py -parameter
I assume it has something to do with the paths, but all thevreading and googling I did recently didn't get me to fix the issue.
Can anyone help me out?
Thanks!
I added cwd='/home/ubuntu/directory_of_the_script/ to the subprocess hoping to fix the path issues.
Also cd /path/to/script in the cronjob didn't fix the thing
I have python-script, which run bash-scripts via subprocess library. I need to collect stdout and stderr to files, so I have wrapper like:
def execute_chell_script(stage_name, script):
subprocess.check_output('{} &>logs/{}'.format(script, stage_name), shell=True)
And it works correct when I launch my python script on mac. But If I launch it in docker-container (FROM ubuntu:18.04) I cant see any log-files. I can fix it if I use bash -c 'command &>log_file' instead of just command &>log_file inside subprocess.check_output(...). But it looks like too much magic.
I thought about the default shell for user, which launches python-script (its root), but cat /etc/passwd shows root ... /bin/bash.
It would be nice if someone explain me what happened. And maybe I can add some lines to dockerfile to use the same python-script inside and outside docker-container?
As the OP reported in a comment that this fixed their problem, I'm posting it as an answer so they can accept it.
Using check_output when you don't get expect any output is weird; and requiring shell=True here is misdirected. You want
with open(os.path.join('logs', stage_name)) as output:
subprocess.run([script], stdout=ouput, stderr=output)
I want to create a graphical python application that can execute some external programs on Linux without showing the terminal.
First, I tried to use subprocess.run() to see if it actually works, but Python 3.7.3 shows no results to the code I wrote.
import subprocess
subprocess.run(['sudo', 'apt', 'update'])
I changed it to see any results:
import subprocess
a = subprocess.run(['sudo', 'apt', 'update'])
print(a)
but it shows this result instantly:
CompletedProcess(args=['sudo', 'apt', 'update'], returncode=1)
This script will take at least 5 seconds to be finished, and it requires sudo privileges to be able to run it in the first place, so I don't think that Python shell executed this script.
Using pkexec instead of sudo fixed my issue. Thanks for everyone tried to help me especially #Charles Duffy.
Now it looks like this:
import subprocess
result = subprocess.run(['pkexec', 'apt', 'update'], stdout=subprocess.PIPE)
print(result.stdout)
So I have a project I'm working on, and one thing I need to do is to run in the background the "netstat -nb" command at the PowerShell as admin and recive the results to the python program.
I've been searching the web for a solution but never found one efficient out there. I'd be glad for some help.
"netstat -nb"
If you want to execute the netstat command you can do so from Python directly.But you need to be running Python script as a Admin:
import subprocess
subprocess.call("netstat -nb")
If you need to access the powershell netstat values inside the Python script then you can set variable in powershell and pass it to Python script.
Following is the powershell command:
$con=netstat -nb
& python.exe "FullPath of Python script file"-p $con
Python script:
import sys
print(sys.argv[5])
for conn in sys.argv:
print(conn)
Here we are looping the parameters passed (netstat output) and displaying.So you are passing powershell command result to Python script and displaying it there.
Following columns would be displayed:
You can use subprocess library. About subprocess module you can check this documentation for more detail and features. It helps you to solve your problem. You can use subprocess.Popen or subprocess.call or to get output from another script you can use subprocess.check_output.
You can use it like this piece of code:
import subprocess
import sys
output = subprocess.Popen(['python','sample.py','Hello'],stdout=subprocess.PIPE).communicate()
print(output)
Inside of the sample.py script is:
import sys
my_message = "This is desired output. " + sys.argv[1]
print(my_message)
In your case you can use Popen as:
subprocess.Popen(['netstat','-nb'],stdout=subprocess.PIPE).communicate()
Normally you can execute a Python script for example: python myscript.py, but if you are in the interactive mode, how is it possible to execute a Python script on the filesystem?
>>> exec(File) ???
It should be possible to execute the script more than one time.
Use execfile('script.py') but it only work on python 2.x, if you are using 3.0 try exec(open('script.py').read())
import file without the .py extension will do it, however __name__ will not be "__main__" so if the script does any checks to see if it's being run interactively you'll need to bypass them.
Alternately, if you're wanting to have a look at the environment after the script runs try python -i script.py
EDIT: To load it again
file = reload(file)
You might want to look into IPython, a more powerful interactive shell. It has various "magic" commands including %run script.py (which, of course, runs the script and leaves any variables it defined for you to examine).
You can also use the subprocess module. Something like:
>>> import subprocess
>>> proc = subprocess.Popen(['./script.py'])
>>> proc.communicate()
You can run any system command using python:
>>>from subprocess import Popen
>>>Popen("python myscript.py", shell=True)
The easiest way to do it is to use the os module:
import os
os.system('python script.py')
In fact os.system('cmd') to run shell commands. Hope it will be enough.