LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Server (https://www.linuxquestions.org/questions/linux-server-73/)
-   -   How to execute bash script from web page (https://www.linuxquestions.org/questions/linux-server-73/how-to-execute-bash-script-from-web-page-798019/)

mustkill 03-26-2010 06:03 AM

How to execute bash script from web page
 
Hi everyone,

What i want to achieve si simple (i think).

I have this shell command:
Code:

echo -e 'ka 0 00\r' > /dev/ttyS0
and i want to execute it from html or php with a button maybe or a link.(it's a command to turn off the monitor)

I am running Ubuntu 9.04 desktop with apache and php installed .
already searched the forum but didn't find/understand how to achieve this functionality.

Thanks

ps: sorry for the title maybe a little misleading

sci3ntist 03-26-2010 06:30 AM

You can do it from a php script using exec function, like $var = exec('YOUR_COMMAND'), and collect the output from $var, or using you can use backticks, put your shell comannd between ``,

Regards,

mustkill 03-26-2010 06:41 AM

thanks for the fast answer.
i have tried the exec function but it does not turn off monitor. I have no output on php page to "echo -e 'ka 0 00\r' > /dev/ttyS0" because there is no output even if i launch it directly from shell.
if i add something like "echo 'something'" on .php file i can see it in web page, so i think everything (apache php) is ok. I don't understand why it doesn't turn off monitor.

thank you

sci3ntist 03-26-2010 06:54 AM

Sure it won't output on a webpage, since your redirecting your output to a serial device which is /dev/ttys0.

mustkill 03-26-2010 07:11 AM

Quote:

Originally Posted by sci3ntist (Post 3913129)
Sure it won't output on a webpage, since your redirecting your output to a serial device which is /dev/ttys0.

i know there is no output i was just specifying it because you told me to collect the output..

this is the code that i've been trying :
Code:

<?php
shell_exec('echo -e "ka 0 00\r" > /dev/ttyS0');
?>

thanks

sci3ntist 03-26-2010 07:13 AM

Good.

mustkill 03-26-2010 07:22 AM

won't work though..
php code does nothing. (i'm running php as root so it isn't a permission issue).

sci3ntist 03-26-2010 07:34 AM

mustkill, can you please elaborate? I don't understand the real problem, tell me what are you trying to achieve?

mustkill 03-26-2010 07:45 AM

OK i'll try to explane better :

this command :
Code:

echo -e "ka 0 00\r" > /dev/ttyS0
sends "ka 0 00" to the serial port of an LG 37LH2000(monitor) and it turns off the monitor.
If i write it in shell it turns off monitor.
I want to do the same thing clicking on a web link/button.

Hope you understand and sorry for the english if there are errors

sci3ntist 03-26-2010 09:33 AM

Yea, I got it, U mean it doesn't execute from the web page you created, does it? Well, the best thing to do is to check your apache logs if there is an error it'll be shown there, do the following:
tail -f /var/log/http/error_log
and then invoke the web page, then show me the result.

sci3ntist 03-26-2010 09:36 AM

Or you can simulate it on the shell,
php your_php_script.php
this will execute the script on the shell and any errors will appear on the Standard error which is your console.
Both should work well for you,

bathory 03-26-2010 09:44 AM

It's more likely a permissions issue.
Don't forget that whenever you run something through a web page, it's executed with the rights of the user that runs the webserver (for apache it's usually apache, or wwwrun). So I guess the apache user does not have the rights to write to the serial port.

Regards

mustkill 03-26-2010 11:00 AM

yes it is a perimssion issue.
sci3ntist's suggetion to read apache logs was right infact i saw premission errors :
Quote:

sh: cannot create /dev/ttyS0: Permission denied
it works if i
Quote:

chmod o+rw /dev/ttyS0
but how con i make this permanent. I mean after a reboot /dev/ttyS0 premissions return to default.

sci3ntist 03-26-2010 12:24 PM

listen you can create a wrapper shell script contains "echo -e 'ka 0 00\r' > /dev/ttyS0" and configure passwordless sudo for apache user to run this script, this will be %100 work.
You got the idea.
Do the following:
$ ps aux | grep -i http | awk '{print $1}'
The above command will display the user that runs apache on your system.
$ vim /usr/local/bin/wrapper.sh
add your code there "echo -e 'ka 0 00\r' > /dev/ttyS0" and save.
$ chmod 707 /usr/local/bin/wrapper.sh
Make your bash executable.
$ visudo
This will run sudo editor, and insert 'APACHE_USER ALL=NOPASSWD:/usr/local/bin/wrapper.sh' without quotations.
then in your php code,
<?php
exec('sudo /usr/local/bin/wrapper.sh');
?>


Regards,
Sci3ntist

mustkill 03-27-2010 04:41 AM

Since nobody will ever reach this machine (except me)i added
Quote:

www-data ALL=NOPASSWD:ALL
to visudo and indeed works like a charm.
Obviously
Quote:

ps aux | grep -i http | awk '{print $1}'
returns to me as root now.
Thank you for your time sci3ntist.

sci3ntist 03-27-2010 10:15 AM

nice to hear that ^^

slowboy1983 05-20-2010 11:01 AM

I want to do something similar... but I know very little about web programming. Could you give a simple code to do this, that you're saying.

Press a button on a webpage and execute a bash command on a linux server.

Thanks!


All times are GMT -5. The time now is 07:26 AM.