php shell_exec and exec doesn't work with shell script

1k views Asked by At

So this is my code for the raspberry pi to get a still shot from the raspicam and save it on a directory,

<?php
  exec('raspistill -n -hf -o /var/www/img/image.jpg --timeout 1');
?>

I have given the ownership and the permission to read/write in that forlder using -R. so my ls -al in /var/www is this

drwxr-xr-x  3 www-data www-data 4096 Jun 19 08:05 .
drwxr-xr-x 12 root     root     4096 Jun 19 05:54 ..
-rwxrwxrwx  1 www-data www-data   74 Jun 19 08:30 getImg
drwxrwxrwx  2 www-data www-data 4096 Jun 19 09:21 img
-rw-r--r--  1 root     root       70 Jun 19 10:07 index.php

getImg is the script i tried to run the script as a file like shell_exec('/bin/bash ./getImg'); that also doesn't work.

i have added /bash/bin and tried to run the script without using the script file too but that doesn't get the results.

How ever when i try to run the php file in the terminal, it creates the image as it normally should. So i figure this must be a permission issue, but what else should i do with the permissions? I have given all the rights to the directory.

EDIT

So I have found a workaround to this. since I don't know what the cause for the problem, i'd not mark this as an answer, but please vote it to appear at the top.

I now execute the script using the cgi scripts. I have created a shell script in the /usr/lib/cgi-bin/

#!/bin/bash
echo "Content-type:text/html\n"
sudo raspistill -vf -n -o /var/www/img/image.jpg --timeout 1200 --metering matrix
echo "Status: 204"

I saved this as capture and made this executable, did nothing with the permissions though.

sudo chmod +x capture

now when i open the link http://192.168.1.85/cgi-bin/capture the browser will still get me a 500 internal server error message. how ever, the image would still be created.

I would now need to get the 500 internal server error to be fixed.

1

There are 1 answers

1
Frank On

[I'd add this as a comment but don't have enough points for it]

  • if you use the optional parameters $output and $return_var to capture the output and return value what do you get?

    string exec ( string $command [, array &$output [, int &$return_var ]] )

  • does your command rely on environment variables that may be available when you run it as your user but not as www-data? (you can use the env command to check that)

  • does it still work if you run it via terminal after switching user to www-data?