2

I am running an Apache webpage on my Raspberry Pi. When I connect to the Pi's network I am able to access that webpage. On this webpage I would like to have a button that runs a shell script located inside the pi itself. All the script is doing is running fswebcam so that the usb webcam attached to the pi takes a screenshot. What I am not able to do is call that .sh script from the pi's webpage. Perhaps running it as .cgi from the cgi-bin? Not even sure how the syntax would look in php. Any help is appreciated.

Here's the .sh script running fswebcam in case its relevant:

#!/bin/bash
DATE=$(date +"%Y-%m-%d_%H%M")
fswebcam -r 1920x1080 --no-banner /var/www/html/$DATE.jpg

Cheers!

1
  • 1
    You can either run it via PHP using system() or exec() or shell_exec() or similar. Alternatively configure apache to run shell scripts using cgi-bin (maybe httpd.apache.org/docs/current/howto/cgi.html can help) Commented Jul 6, 2016 at 15:15

1 Answer 1

1
<?php
$last_line = system('path/to/your_script.sh', $retval);
echo "<pre>".print_r($last_line,true)."</pre>";
echo "<pre>".print_r($retval,true)."</pre>";

If you open the page this should run the script and print as much informationa bout that as possible in a readable way.

Sign up to request clarification or add additional context in comments.

7 Comments

Absolute path dosnt seem to go through, I get a value of 0 displayed when I open the page. I changed it to refer to its own local folder and moved the .sh there and now it returns 127. Not sure what to conclude from these numbers though?
Did you get a snapshot from the webcam? Otherwise take a look into the webcam itself. If you can access it from the commandline there should also be information about the results you can get there.
When I run the shell from the command line I get a snapshot, but when I open the webpage nothing happens.
I am attempting to run it as a .cgi from /usr/lib/cgi-bin/ by typing its location into the browser bar and I get this error: "Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. More information about this error may be available in the server error log." But if I run a simpler script from the same folder it works, like a hello world script: #!/bin/bash echo -e "Content-type: text/html\n\n" echo "<h1>Hello World</h1>"
Then check the error log for what you get in there.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.