LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Server
User Name
Password
Linux - Server This forum is for the discussion of Linux Software used in a server related context.

Notices



Reply
 
Search this Thread
Old 03-26-2010, 07:03 AM   #1
mustkill
LQ Newbie
 
Registered: Mar 2010
Posts: 7

Rep: Reputation: 0
Post 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

Last edited by mustkill; 03-26-2010 at 07:06 AM. Reason: title error
 
Old 03-26-2010, 07:30 AM   #2
sci3ntist
Member
 
Registered: Dec 2006
Distribution: Debian,Slackware,FreeBSD,CentOS,Red Hat,Windows Server 2008
Posts: 133

Rep: Reputation: 16
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,
 
Old 03-26-2010, 07:41 AM   #3
mustkill
LQ Newbie
 
Registered: Mar 2010
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
Old 03-26-2010, 07:54 AM   #4
sci3ntist
Member
 
Registered: Dec 2006
Distribution: Debian,Slackware,FreeBSD,CentOS,Red Hat,Windows Server 2008
Posts: 133

Rep: Reputation: 16
Sure it won't output on a webpage, since your redirecting your output to a serial device which is /dev/ttys0.
 
Old 03-26-2010, 08:11 AM   #5
mustkill
LQ Newbie
 
Registered: Mar 2010
Posts: 7

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by sci3ntist View Post
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
 
Old 03-26-2010, 08:13 AM   #6
sci3ntist
Member
 
Registered: Dec 2006
Distribution: Debian,Slackware,FreeBSD,CentOS,Red Hat,Windows Server 2008
Posts: 133

Rep: Reputation: 16
Good.
 
Old 03-26-2010, 08:22 AM   #7
mustkill
LQ Newbie
 
Registered: Mar 2010
Posts: 7

Original Poster
Rep: Reputation: 0
won't work though..
php code does nothing. (i'm running php as root so it isn't a permission issue).
 
Old 03-26-2010, 08:34 AM   #8
sci3ntist
Member
 
Registered: Dec 2006
Distribution: Debian,Slackware,FreeBSD,CentOS,Red Hat,Windows Server 2008
Posts: 133

Rep: Reputation: 16
mustkill, can you please elaborate? I don't understand the real problem, tell me what are you trying to achieve?
 
Old 03-26-2010, 08:45 AM   #9
mustkill
LQ Newbie
 
Registered: Mar 2010
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
Old 03-26-2010, 10:33 AM   #10
sci3ntist
Member
 
Registered: Dec 2006
Distribution: Debian,Slackware,FreeBSD,CentOS,Red Hat,Windows Server 2008
Posts: 133

Rep: Reputation: 16
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.
 
Old 03-26-2010, 10:36 AM   #11
sci3ntist
Member
 
Registered: Dec 2006
Distribution: Debian,Slackware,FreeBSD,CentOS,Red Hat,Windows Server 2008
Posts: 133

Rep: Reputation: 16
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,
 
Old 03-26-2010, 10:44 AM   #12
bathory
Guru
 
Registered: Jun 2004
Location: Piraeus
Distribution: Slackware
Posts: 10,950

Rep: Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341Reputation: 1341
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
 
Old 03-26-2010, 12:00 PM   #13
mustkill
LQ Newbie
 
Registered: Mar 2010
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
Old 03-26-2010, 01:24 PM   #14
sci3ntist
Member
 
Registered: Dec 2006
Distribution: Debian,Slackware,FreeBSD,CentOS,Red Hat,Windows Server 2008
Posts: 133

Rep: Reputation: 16
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
 
1 members found this post helpful.
Old 03-27-2010, 05:41 AM   #15
mustkill
LQ Newbie
 
Registered: Mar 2010
Posts: 7

Original Poster
Rep: Reputation: 0
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I execute a bash script from a link on a web page? Is there any way to do this digilifellc Linux - Desktop 6 03-11-2011 03:56 AM
How to execute a shell script from a simple html web page Shreerang Patwardhan Linux - Software 7 03-11-2010 12:12 PM
How do I execute a bash script from a link on a web page? Is there any way to do this digilifellc Linux - Desktop 5 03-10-2010 12:24 PM
Web query to execute commands and output it to a web page ! ashes_sheldon Linux - Newbie 1 05-08-2009 03:45 AM
Execute bash commands from web page solomage Linux - Software 1 10-07-2004 11:54 AM


All times are GMT -5. The time now is 09:51 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration