user6738254
user6738254

Reputation:

PHP - Shell_exec and exec returning false when ran

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!

Upvotes: 0

Views: 1580

Answers (1)

BetaDev
BetaDev

Reputation: 4674

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;

Upvotes: 1

Related Questions