Raspberry PI: PHP call python script with sudo - nginx

i'm running Nginx on my Raspberry PI with PHP 5.4.14 (fpm-fcgi). It all works well. But in one script i make a shell call using shell_exec.
echo shell_exec("sudo python " . $file);
it works well if i use the console with
php test.php
but if i use the browser to call this file the php-file will be executed but i don't get any output by the shell_exec (not even an error message).
So i hope you could help me fix this problem.
The called python file makes use of the GPIO so sudo is required, right?
Here is my python code:
#!/usr/bin/env python
try:
import RPi.GPIO as GPIO
except RuntimeError:
print "Error Importing GPIO. Did your forgot the superuser privilieges?"
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(3, GPIO.OUT)
GPIO.output(3, not GPIO.input(3))
print "New LED state is: ", GPIO.input(3)
#GPIO.cleanup()

for future reference:
I found a - i my eyes - not really nice solution:
sudo echo "apache ALL=(ALL) NOPASSWD: ALL" >> /etc/sudoers
Found here.

Mm, i have try with this two simple example:
/Scripts/test.py
#!/usr/bin/env python
print (str('hello world'))
/var/www/test.php
<?php
echo shell_exec('/Scripts/test.py');
?>
via a browser, show me "hello world"
Perhaps try this:
chmod a+x your_py_file.py
Hope that help..

Related

Error while running a .sh script via QProcess

I have written a QT GUI program where pressing a button will execute a .sh script. The contents of the script is-
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import ora_exported.csv qt_ora_exported' | sqlite3 testdatabase.db
basically the script will import a .csv to an sqlite database. And when the script file (script.sh) is run manually from linux terminal ($./script.sh) it successfully imports the .csv file into the database table.
But, when I call the script from my QT program
void MainWindow::on_importButton_clicked()
{
QProcess process;
process.startDetached("/bin/sh",QStringList()<<"/home/aj/script.sh");
}
it compiles successfully but gives an error message in console when the button is pressed at runtime.
Error: near line 1: near "-": syntax error
Error: cannot open "ora_exported.csv"
what could be causing this ???
EDITED
I have changed my .sh script now to--
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import /home/aj/ora_exported.csv qt_ora_exported' | sqlite3 testdatabase.db
Thus providing the path to my ora_exported.csv. As a result the runtime error [Error: cannot open "ora_exported.csv"] has gone but the other message [Error: near line 1: near "-": syntax error] is still coming.
Same as was observed in previous case, using ./script.sh is successfully importing data to sqlite3 db table file but QProcess is unable to.
echo is a built in command of a shell that may behave differently.
E.g. take this test script: echotest.sh
echo -e "123"
Now we can compare different results:
$ bash echotest.sh
123
$ zsh echotest.sh
123
$ dash echotest.sh
-e 123
You are probably on some Ubuntu-like OS, where /bin/sh redirects to dash. That would explain the error around "-". So if you are using echo, set you shell specificially or ensure that your script works on all common shells.
Additionally, you are messing up your quotations
echo -e 'attach database 'testdatabase.db' as 'aj';\n.separator ","\n.import /home/aj/ora_exported.csv qt_ora_exported'
results in (no quotations in the first line)
attach database testdatabase.db as aj;
.separator ","
.import /home/aj/ora_exported.csv qt_ora_exported
but you pobably want
echo -e "attach database 'testdatabase.db' as 'aj';\n.separator ','\n.import /home/aj/ora_exported.csv qt_ora_exported"
It looks strange that you are using external script to update database!
why you don't pass "ora_exported.csv" file name as a script argument? This would help solve the problem.
I was talking (typing) about this solution:
void MainWindow::on_importButton_clicked()
{
QProcess::startDetached("/bin/sh",
QStringList()<<"/home/aj/script.sh",
"<location of: 'ora_exported.csv' file>");
}

Running Groovy script from the command line

When I did which groovy, I got the below output:
/usr/local/bin/groovy
So I went ahead and created a helloworld.groovy with the below content
#!/usr/local/bin/groovy
println "hello world"
After that I did chmod +x helloworld.groovy and attempted to run the file with ./hellworld.groovy and sadly, I got this error ./helloworld.groovy: line 2: print: command not found
I could get rid of the error by changing to
#!/usr/bin/env groovy
println "hello world"
Why would the first method cause the error?
You need to run the script like this:
groovy helloworld.groovy
#!groovy
println("hello world!")
$ chmod +x script.groovy
$ ./script.groovy
It will work on Linux kernel 2.6.28 (confirmed on 4.9.x). It won't work on FreeBSD and other Unix flavors.
Your /usr/local/bin/groovy is a shell script wrapping the Java runtime running Groovy.
See the Interpreter Scripts section of EXECVE(2) and EXECVE(2).
#!/bin/sh
sed '1,2d' "$0"|$(which groovy) /dev/stdin; exit;
println("hello");

Custom PS1 not showing the current working directory

I have customized the primary prompt for bash shell but unfortunately it is not showing the current working directory as can be seen below.
[cifsamidala]w# pwd
/home/Vinod/SearchIdIssue
[cifsamidala]w#
Here is what I have customized my PS1:
HOST=`tput bold``hostname``tput sgr0`
PS1="[${HOST}]\w # "
Here is the Shell info for your reference.
[cifsamidala]w# echo $SHELL
/sbin/sh
[cifsamidala]w# echo $0
-sh
I also tried with 'pwd' as below but of no use.
PS1="[${HOST}]`pwd` # "
Kindly let me know if I am missing something here.
Thanks in avance.
Regards,
Vinod Yadav
Your shell is not the bash shell and this is probably the reason why it won't work. Ask your sysadmin to give you an unrestricted shell.

Pydev Eclipse console does not support curses.setupterm

I cannot run a script in Eclipse that works perfectly in a terminal.
It seems that Eclipse console cannot support some functions. I am looking for a workaround to be able to debug the script using Pydev.
Is it possible to set PyDev to use for example /usr/bin/gnome-terminal instead of the Eclipse native console ?
Otherwise it there a way to define a wrapper as a python interpreter for PyDev that will launch an terminal external to Eclipse (I've tried but failed on that).
Thank you
Nga
Right now, curses-based apps really don't run well inside Eclipse/PyDev, so, you must really launch it externally. To debug you can use the remote debugger: http://pydev.org/manual_adv_remote_debugger.html
If you use Aptana Studio, there's a terminal view which should emulate a terminal better... try running python (i.e.: running your program) from inside that view. If it does work properly there, maybe I could check a way to better integrate there and launch directly in that view.
Thank you for your reply. I have finally defined kind of a wrapper as a bash script calling python in a xterm. Pydev is checking some configuration by calling eclipse/plugins/org.python.pydev_2.4.0.2012020116/PySrc/interpreterInfo.py so the script first echo the format expected by PyDev. here is the script "
#!/bin/bash
# dummy return for Eclipse Pydev - respect interpreter info format
echo "EXECUTABLE:/home/user/python_custom/python_xterm|
|/home/user/eclipse/plugins/org.python.pydev_2.4.0.2012020116/PySrc
|/usr/lib/python2.5
|/usr/lib/python2.5/plat-linux2
|/usr/lib/python2.5/lib-tk
|/usr/lib/python2.5/lib-dynload
|/usr/local/lib/python2.5/site-packages
|/usr/lib/python2.5/site-packages
|/usr/lib/python2.5/site-packages/Numeric
|/usr/lib/python2.5/site-packages/PIL
|/usr/lib/python2.5/site-packages/gst-0.10
|/var/lib/python-support/python2.5
|/usr/lib/python2.5/site-packages/gtk-2.0
|/var/lib/python-support/python2.5/gtk-2.0
|/var/lib/python-support/python2.5/HTMLgen
|/var/lib/python-support/python2.5/pyinotify
|/usr/lib/python2.5/site-packages/wx-2.6-gtk2-unicode
|/usr/lib/site-python
#
$
|__builtin__
|__main__
|_ast
|_codecs
|_sre
|_symtable
|_types
|errno
|exceptions
|gc
|imp
|marshal
|posix
|pwd
|signal
|sys
|thread
|xxsubtype
|zipimport
"
# activate scrollbar -sb with 6000 lines
# allow logging -l with filename log_$NOW
xterm -g 150x100+0+0 -sb -sl 6000 -si -hold -e "python $*"
that does the job, and I can use Pydev and its debugger

Cygwin: Unable to find Java path.

I am using cygwin on windows 7 env and it is unable to locate java, error message i get is that
ERROR: /cygdrive/c/Program
Files/Java/jdk1.6.0_22 does not exist!
wired path is if I do echo $JAVA_HOME then it shows me
$ echo $JAVA_HOME
/cygdrive/c/Program Files/Java/jdk1.6.0_22
not sure what is happening here, any suggestions?
The problem is that the pathname contains spaces. You need to escape the spaces as described here:
http://www.cygwin.com/faq/faq.using.html#faq.using.filename-spaces
Adding this line to your .bashrc should do it:
export JAVA_HOME='/cygdrive/c/Program Files/Java/jdk1.6.0_22'
Edit: You could try running this script which I found in this blog post:
case "`uname`" in
CYGWIN*) cygwin=true ;;
esac
# For Cygwin, switch paths to Windows format before running java
if $cygwin; then
JAVA_HOME=`cygpath --windows "$JAVA_HOME"`
CLASSPATH=`cygpath --windows --path "$CLASSPATH"`
fi
Use the old school way:
export JAVA_HOME=/cygdrive/c/Progra~1/Java/jdk1.6.0_22
It worked for me.

Resources