I am new to web design, and I'm trying to setup an Apache web interface to some python code.
I am running a test python script that just opens a new file in /var/www/html/ called output.txt and attempts to write to it. When the web page is loaded, I use shell_exec('/usr/bin/python3 /var/www/html/ptest.py'); to run my python script. The python script does run and outputs some print messages, but trying to open output.txt fails with:
Traceback (most recent call last): File "/var/www/html/ptest.py", line 8, in f=open("/var/www/html/output.txt", "w") PermissionError: [Errno 13] Permission denied: '/var/www/html/output.txt'. It will also fail if the file already exists and I open to read.
I have confirmed that the script is being run by apache with getpass.getuser(), and I have tried as many different permission combinations as I can think of for /var/www/html/, including 777. I have set apache as the group for every directory from /var/ to html/. I have tried creating the file ahead of time with 777 permissions. I have checked that /var/ to html/ has group execution permission.
I have tried creating and using another folder completely owned by apache.
I have looked through the apache directives to see if there is a directive I need to have, but I haven't found one yet.
I've included the code for my python script and my php page below.
EDIT: I've tried running ptest.py as apache using su -s /bin/bash apache.
ptest.py successfully runs this way, so the problem doesn't seem to be a permission associated with the apache user/group.
ptest.py
#!/usr/bin/python3
import sys
import getpass
import os
sys.stderr = sys.stdout
print(getpass.getuser())
print(os.getgid())
f = open("/var/www/html/output.txt", "w")
f.write("banana")
f.close()
print("I wrote banana, which is a berry")
banana.php
<html>
<head></head>
<body>
<h2>Welcome to the Program Test</h2>
<?php
echo "Created Command", "<br>";
$output = shell_exec('/usr/bin/python3 /var/www/html/ptest.py');
echo "Executed Command", "<br>";
echo $output, "<br>";
echo "End of output", "<br>";
$output = shell_exec('ls /var/www/html/');
echo $output;
?>
</body>
</html>
Credit to domino_pl leaving a comment.
It looks like typical selinux problem. Try selinux setenforce 0 – domino_pl
Related
i want to run a python script from php code using exec or shell_exec. this is php code :
<?php
exec('/usr/bin/python /var/www/html/test.py');
?>
also this is the python script i want to execute it from browser, name test.py :
#!/usr/bin/env python
import os
f= open("/var/www/html/result","w")
f.write("ok")
script runs from command line correctly, but when i execut it from browser , it dose not work. I searched a lot of things and i did following items to solve the problem :
I add 'www-data ALL=(ALL) NOPASSWD: ALL' and 'apache ALL=(ALL) NOPASSWD: ALL' in /etc/sudoers file.
I give 777 permission to /var/www/html directory
I run 'chown -R www-data:www-data /var/www/html'
Remove all functions in front of 'disable_functions' in /etc/php/7.0/fpm/php.ini and /etc/php/7.0/cli/php.ini files
home.php and test.py are in a same directory in /var/www/html
I login with www-data in commandline and executed test.py and it worked correctly
But still the script dose not work when i visit http://server_ip_address/home.php . I'm new in php and apache. can anybody help me to solve the problem. great thanks
Can you try
<?php
exec('/usr/bin/python3 /var/www/html/test.py');
?>
and
#!/usr/bin/env python3
import os
f= open("/var/www/html/result","w")
f.write("ok")
I'm developing a Django app which runs Matlab scripts with Python's Popen class. The python script that calls Matlab scripts lives in the main folder of my Django app (with views.py). When I call the script from command line, it runs like a charm but when I make a request from the client in order to run the corresponding python script, I receive the following warning:
"< M A T L A B (R) > Copyright 1984-2018 The MathWorks, Inc. R2018a (9.4.0.813654) 64-bit (glnxa64) February 23, 2018 To get started, type one of these: helpwin, helpdesk, or demo. For product information, visit www.mathworks.com. >> [Warning: Unable to create preferences folder in /var/www/.matlab/R2018a. Preferences folder location must be writable. Using a temporary preferences folder for this MATLAB session. See the preferences documentation for more details.] >>
My app uses a Python virtual environment and it is being deployed with Apache web server.
Here is my python script that calls Matlab scripts:
import os
import subprocess as sp
import pymat_config
def pymat_run():
pwd = pymat_config.pwd_config['pwd']
cmd1 = "-r \"Arg_in = '/path/to/my/main/folder/input.txt'; Arg_out = '/path/to/my/main/folder/file.txt'; matlab_script1\""
baseCmd1 = ['/usr/local/MATLAB/R2018a/bin/matlab', '-nodesktop', '-nosplash', '-nodisplay', 'nojvm', cmd1]
os.chdir('/path/to/matlab_script1')
sudo_cmd = sp.Popen(['echo', pwd], stdout=sp.PIPE)
exec1 = sp.Popen(['sudo', '-S'] + baseCmd1, stdin=sudo_cmd.stdout, stdout=sp.PIPE, stderr=sp.PIPE)
out, err = exec1.communicate()
return out
Any suggestions ?
Finally I managed to find the solution of that issue by myself. The problem came from the kind of user who called the Matlab's script. When I was running the above script from a Python interpreter or from the shell, it was the user (with the user password) who was running the script while when I was calling the script from the client the user was the web server's user: www-data.
So at first to avoid the above warning I gave permissions to www-data user to the /var/www folder with the following command:
sudo chown -R www-data /var/www/
After that, the "Warning" disappeared but the script still didn't run because it was asking for www-data's password internally and taking user's password from pymat_config file.
To solve this, I edited /etc/sudoers file in order for www-data to be able to call Matlab scripts without asking password. So I added the following line:
www-data ALL=(ALL) NOPASSWD: /usr/local/MATLAB/R2018a/bin/matlab
and now it runs like a charm !
I m running on a remote server a python script using nohup.
First I connected to the remote machine using a VPN and SSH
Second I run a python script using the following command:
nohup python backmap.py mpirun -np 48 &
The python script contains the following lines:
frame = []
file_in = open("Traj_equil_prot.pdb", "r")
for line in file_in:
if line.startswith('TITLE'):
frame.append(line[127:134])
import os
for fileNum in range(631, 29969):
os.system("./initram-v5.sh -f Traj_equil_prot_frame" + str(fileNum) + ".pdb -o Traj_equilprot_aa_frame" + str(frame[fileNum]) + ".gro -to amber -p topol.top")
The script was running just fine the whole day. but now it just crashed and when I try to re-launch it again I'm getting the following error:
Traceback (most recent call last): File "", line 1, in
IOError: [Errno 5] Input/output error
The file is in the working directory. I tried to disconnect/connect again but still the same problem. I don't know what I'm missing. Any help, please?
I had the same problem, I used to run my script using this command:
python MY_SCRIPT.py &
The script will run in the background and the output will be displayed on the terminal until you logout or exit.
By logging out, the script is still running but it's output has nowhere to display thus, exceptions will be made when the script wants to display something (e.g. calling print).
Solution:
I've piped the output to somewhere other than the actual terminal display:
python MY_SCRIPT.py >/dev/null &
Checkout this link for more details:
https://stackoverflow.com/a/38238281/6826476
I finally fixed the problem by opening the file "file_in", modifying it (just adding a point in the REMARK line for example) and saving the changes.
I have followed a few posts on here trying to run either a python or shell script on my ec2 instance after every boot not just the first boot.
I have tried the:
[scripts-user, always] to /etc/cloud/cloud.cfg file
Added script to ./scripts/per-boot folder
and
adding script to /etc/rc.local
Yes the permissions were changed to 755 for /etc/rc.local
I am attempting to pipe the output of the file into a file located in the /home/ubuntu/ directory and the file does not contain anything after boot.
If I run the scripts (.sh or .py) manually they work.
Any suggestions or request for additional info to help?
So the current solution appears to be a method I wrote off in my initial question post as I may have not performed the setup exactly as outline in the link below...
This link -->
How do I make cloud-init startup scripts run every time my EC2 instance boots?
The link shows how to modify the /etc/cloud/cloud.cfg file to update scripts-user to [scripts-user, always]
Also that link says to add your *.sh file to /var/lib/cloud/scripts/per-boot directory.
Once you reboot your system your script should have executed and you can verify this in: sudo cat /var/log/cloud-init.log
if your script still fails to execute try to erase the instance state of your server with the following command: sudo rm -rf /var/lib/cloud/instance/*
--NOTE:--
It appears print commands from a python script do not pipe (>>) as expected but echo commands pipe easily
Fails to pipe
sudo python test.py >> log.txt
Pipes successfully
echo "HI" >> log.txt
Is this something along the lines that you want?
It copies the script to the instance, connects to the instance, and runs the script right away.
ec2 scp ~/path_to_script.py : instance_name -y && ec2 ssh instance_name -yc "python script_name.py" 1>/dev/null
I read that the use of rc.local is getting deprecated. One thing to try is a line in /etc/crontab like this:
#reboot full-path-of-script
If there's a specific user you want to run the script as, you can list it after #reboot.
I have a python script that creates a csv and saves it to:
file = example.csv
fileDone = os.path.abspath('/home/bw/temp/%s'%file1)
with open(fileDone, 'w+') as myFile:
a = csv.writer(myFile, delimiter=',')
a.writerow(['login report for %s from %s to %s:\n\n'%(name[0],start_date,end_date)])
dato = ['Username','Logins','Platform']
a.writerow(dato)
for vizL in vizList:
data =[str(vizL[0]),str(vizL[1]),"Viz"]
a.writerow(data)
for appL in appList:
data =[str(appL[0]),str(appL[1]),"Analytics"]
a.writerow(data)
then a bash script calls the python script:
#!/bin/bash
python scriptname.py
REPORT_MONTH=`/bin/date "+%d %B %Y"`
echo -e "Attached is the login reports for $REPORT_MONTH\n\n\n\n\n\nGenerated by: $0" | mutt -a /home/bw/temp/example.csv -c my#email.com -s "Daily Login Reports For $REPORT_MONTH"
Then cron runs the bash script daily.
When I run the bash script manually, everything works fine and the dates are correct. But when its run by cron, it doesn't seem to overwrite the csv file and just sends one that was already in the /temp folder. So I assume it is not regenerating the csv hence not running the python script.
This is my first time setting up a cron job to run a bash script that runs a python script. Any potential insight as to why this is happening would be greatly appreciated.
There are two things that I would try:
First, try setting the full path to the python script in the bash script, something like this:
#!/bin/bash
python /home/user/scripts/scriptname.py
REPORT_MONTH=`/bin/date "+%d %B %Y"`
echo -e "Attached is the login reports for $REPORT_MONTH\n\n\n\n\n\nGenerated by: $0" | mutt -a /home/bw/temp/example.csv -c my#email.com -s "Daily Login Reports For $REPORT_MONTH"
Second, if the previous didn't work, it might be a problem with the cron job not having permissions to overwrite the file. Which user is the cron running under? (it's usually root). Can you post the output of crontab -l? If you remove the file and let the cron create it, does it gets created?
First I would make sure your cron is using bash; by default it uses sh. You can include SHELL=/bin/bash to change it.