0

I've done some extensive research on this topic. But I found nothing. It seems when I run shell_exec() it always returns false. Even when the output is true.

PHP script:

$tmp1 = exec("/var/www/html/online/files/test.sh 2>&1");

test.sh Bash script:

var=$(screen -ls | awk -F'[. ]' '/.onoff/ {print $1}')
if [ -z "$var" ]; then
  echo "false"
else
  echo "true"
fi

It's testing the screen PID for a screen called 'onoff' which was running at the time of being ran, yet still returns false. Even when I use the command in PuTTY it returns true.

Here are the permissions of the files:

root@s94029:~# ls -la /var/www/html/online
drwxr-xr-x  3 root root 4096 Dec 18 12:34 .
drwxr-xr-x 12 root root 4096 Dec 18 09:30 ..
drwxr-xr-x  2 root root 4096 Dec 18 11:54 files
-rwxr-xr-x  1 root root  221 Dec 18 04:59 index.css
-rwxr-xr-x  1 root root 4310 Dec 18 12:55 index.php
-rwxr-xr-x  1 root root  417 Dec 18 06:40 servers.php
root@s94029:~# ls -la /var/www/html/online/files
drwxr-xr-x 2 root root 4096 Dec 18 11:54 .
drwxr-xr-x 3 root root 4096 Dec 18 12:34 ..
-rwxr-xr-x 1 root root  115 Dec 18 11:54 test1.sh
-rwxr-xr-x 1 root root  114 Dec 18 11:54 test.sh

Any help would be nice!

Thanks!

2
  • 1
    Owner is root, that explains it.. On a side note, exec returns false, not your command. Do a === check. Commented Dec 18, 2016 at 19:42
  • Even after setting the owner to www-data, it doesn't work. And where exactly should I put a '==='? Commented Dec 18, 2016 at 19:59

1 Answer 1

1

try exec($command, $output, $status) check output and status variable. non zero in status variable is error. zero is success.

After doing this check ownership of the file... add www-data (apache) user to the ownership of the file if you still face problem.

exec('/var/www/html/online/files/test.sh 2>&1', $output, $status);

print_r($output);
echo $status;
Sign up to request clarification or add additional context in comments.

9 Comments

When I use your suggestion, none of them are set. Could you give an example, I feel I'm using it wrong.
Here is the output of the page: Ahh yes, here is the output: Output: Array Status: 0
I forgot to add the code things, Output: Array Status: 0
Here is print_r($output): Array ( [0] => false )
this means your script is running and returning "false" string from the script
|

Your Answer

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