uWSGI + Python subprocess does not execute shell command - python

uwsgi + Python subprocess
Hello everyone,
I am trying to run a simple command in shell using Python subprocess module, everything works fine till I put
uwsgi on the top. I also use flask as web framework.
Here is very simplified part of the code
if request.method == 'POST':
testquery = subprocess.run( "ifconfig", shell=True,stdout=subprocess.PIPE,universal_newlines=True )
whoisresults=whoisquery.stdout
print(whoisresults)
I was getting following error : /bin/sh: 1: ifconfig: not found
I replaced "ifconfig" with the full path where Python virtual evn runs.
testquery = subprocess.run( "/home/net/netools/netoolsenv/bin ifconfig", shell=True,stdout=subprocess.PIPE,universal_newlines=True )
whoisresults=whoisquery.stdout
print(whoisresults)
But it still does not work, just error is different - /bin/sh: 1: /home/net/netools/netoolsenv/bin: Permission denied
Can anyone please advice in what direction should I dig? I am beginner here.

I was getting following error : /bin/sh: 1: ifconfig: not found
uWSGI daemon usually run as another user, and this user has no PATH set. Use full absolute path to ifconfig.
To find full path to your tools, use which, e.g. run this command at your terminal:
$ which ifconfig
/usr/bin/ifconfig
and use that full path to ifconfig in your Python script.
testquery = subprocess.run( "/usr/bin/ifconfig", shell=True,stdout=subprocess.PIPE,universal_newlines=True )

Related

sh: 1: python: not found when execute python script from php

I'm tring to execute a python script call from php with the command below:
$output = shell_exec('python /var/www/html/sna/server/userManagement.py '. $user.' '. $pass .' \''.$action.'\' 2>&1');
But when I execute it I get this
sh: 1: python: not found
But python is correctly installed in my env.
If I digit
type -a python
I get the path of python in this env like below (not sure because they are two)
python is /home/leonardo/miniconda2/bin/python
python is /home/leonardo/miniconda2/envs/sna/bin/python
At the very beginning of the python script I have include
#! /usr/bin/env python
But I recieve always the same error. How can I solve ?
EDIT
I tried to add python path to the $PATH with command
export $PATH:/home/leonardo/miniconda2/envs/sna/bin/python
But I get the same error anywhay
Your binary is not in the PATH for the webservers user account.
PHP inherits the PATH from Apache. And most distros set a fairly restrained:
SetEnv PATH /bin:/usr/bin
Either change that, or putenv() in PHP, or use absolute paths instead.

Backup Postgres from Python on Win10

I'm trying to backup Postgres from Python on Win10.
I'm working on Anaconda python 3.8, Win10 machine with Postgres12 local. On path environment variable I have postgres (lib and bin), no anaconda, and python 3.8 (no the anaconda one).
I'm able to correctly backup the database using on window's command shell:
pg_dump --dbname=postgresql://postgres:password#127.0.0.1:5432/test > C:\backup\dumpfile3.dump
but when I run it on anaconda:
os.system("pg_dump --dbname=postgresql://postgres:password#127.0.0.1:5432/test > C:\backup\dumpfile3.dump" )
I get as output 1 , witch is error code. It creates the file, but it's empty.
Using:
import subprocess
stk= 'pg_dump --dbname=postgresql://postgres:password#127.0.0.1:5432/test > C:\backup\dumpfile3.dump'
try:
subprocess.check_output(stk, shell=True, stderr=subprocess.STDOUT)
except subprocess.CalledProcessError as e:
raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output))
I get :
RuntimeError: command 'pg_dump --dbname=postgresql://postgres:password#127.0.0.1:5432/test > C:\backup\dumpfile3.dump' return with error (code 1): b"'pg_dump' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n"
If I use: subprocess.run or subprocess.call I doesn't produce error, but the created file it's empty.
It seems that neither os.system or subprocess on the anaconda interpreter, got access to the environment variables on the command shell. How is this possible? and how I can overcome it?. It is different user invoking the shell?
Thanks in advance.
The Computer was restarted, and it solves the issue... . There was no change in the paths, I believe that from the moment things (python ,postgres, ...) were installed, the machine hasn"t been restarted.
import os
os.system("pg_dump --dbname=postgresql://postgres:password#127.0.0.1:5432/test > C:\backup\dumpfile3.dump" )
worked!, and
import subprocess
subprocess.call(r"C:\some\path\backup.bat")
also worked!. Inside backup.bat is:
pg_dump pg_dump --dbname=postgresql://postgres:password#127.0.0.1:5432/test > C:\backup\dumpfile3.dump
I imagine that the issue was that the anaconda interpreter need a system restart to get access to the environment variables (where the postgres variable was), witch make very little sense as return with error (code 1): b"'pg_dump' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n" seen like a console message.
If anyone have a better explanation, is welcome.

Start python file in Swift

I want to start a pythonscript by pushing a Button in a swift macOS application. I come up with:
let process = Process()
process.launchPath = "/usr/bin/python3"
process.currentDirectoryPath = "\(NSHomeDirectory())" + "/PycharmProjects/untitled5"
process.arguments = ["myscript.py"]
process.launch()
but I get "launch path not accessible" error by executing. If I change launchPath to:
process.launchPath = "/usr/bin/python"
everything works fine, but now I getting python compiling errors because myscript is written in python3.6.0, I have to use python3 because of using a library.
When I open Finder and go to "/usr/bin/python3" it says not found, but python3 is installed, I used it in Pycharm and I'm also able to start python3 in terminal.
In terminal "python3 ~/PycharmProjects/untitled5/myscript.py" works.
On your terminal type
which python3
this will return the path that is accessed when you run python3 from the command line

Python fabric cant be executed from python using subproccess

In python I am trying to execute a fabfile and ,I get the below error from the subproccess output. I installed fabric using easy install. If I run the code from the command line it works. From python no go. I assume there is an issue with how I am using the Popen command?
/bin/sh: 1: fab: not found
Below is how I start fabric from python:
cmd = """fab -H 111.111.111.111 aws_bootstrap initial_chef_run:aws_server.json,aws-test,development -w """
os.chdir(fab_path) #change to the dir where the fabfile is located
p = subprocess.Popen(cmd, shell=True,stderr=subprocess.STDOUT,stdout=subprocess.PIPE)
PS I added the below to the Popen but get the below error:
executable="/bin/bash"
/bin/bash: fab: command not found
From the command line I get the below which means the terminal can find fab.
fab
Fatal error: Couldn't find any fabfiles!
Remember that -f can be used to specify fabfile path, and use -h for help.
Aborting.
Try to use the whole path for fab. To get the path to fab on your system, you can use which fab.
However, I can't think of any reason why calling fab from python might be better than using the execute function of fab:
from fabric.tasks import execute
import my_fabfile
r = execute(my_fabfile.aws_bootstrap, hosts=["root#%s" % '111.111.111.111])
Return value r will contain a dict with the hosts as key(s).

Calling fswebcam through apache / python script

I am running the following command in my python script:
res = subprocess.Popen(["fswebcam", "-r 640x480", "grab.jpeg"], shell=False, stdout=subprocess.PIPE, cwd="/var/www/pics");
output = res.communicate()[0];
which runs fine when calling the script manually.
But when running the script through apache, I get no error message but no image appears in the /var/www/pics directory. This directory as been assigned to www-data:www-data and has the write permissions.
I don't understand what I am missing to get this working.
Finally found the issue, the www-data user was not allowed to run fswebcam, so I added a coniguration file in the /etc/sudoers.d directory to allow access to fswebcam by user www-data.
Here is the content of my file:
Defaults:www-data !requiretty
%www-data ALL = NOPASSWD: /usr/bin/fswebcam
I also had to modify the call to fswebcam in my python script by adding "sudo" as begginning of the command.
res = subprocess.Popen(["sudo", "fswebcam", "-r 640x480", "grab.jpeg"], shell=False, stdout=subprocess.PIPE, cwd="/var/www/pics");

Categories

Resources