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 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 |
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, |
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 |
Sure it won't output on a webpage, since your redirecting your output to a serial device which is /dev/ttys0.
|
Quote:
this is the code that i've been trying : Code:
<?php |
Good.
|
won't work though..
php code does nothing. (i'm running php as root so it isn't a permission issue). |
mustkill, can you please elaborate? I don't understand the real problem, tell me what are you trying to achieve?
|
OK i'll try to explane better :
this command : Code:
echo -e "ka 0 00\r" > /dev/ttyS0 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 |
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. |
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, |
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 |
yes it is a perimssion issue.
sci3ntist's suggetion to read apache logs was right infact i saw premission errors : Quote:
Quote:
|
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 |
Since nobody will ever reach this machine (except me)i added
Quote:
Obviously Quote:
Thank you for your time sci3ntist. |
nice to hear that ^^
|
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. |