Sumit Sinha
Sumit Sinha

Reputation: 666

Why is this shell script not running as expected from PHP?

I have this PHP program in /var/www folder to run a shell script and load the HTML file created by that shell script. If I run the shell script manually from command line then it works like charm but when run via PHP program from browser then it only creates an empty file.

PHP CODE

$a = './script.sh '.$foo.' '.$bar;
$b = shell_exec($a);
include '/var/tmp/reports/r.html';

SHELL SCRIPT

cat file.ext | awk <something with $foo and $bar> | command > /var/tmp/reports/r.html

(Edit ^ "file.ext" is actually a .log file. It iss located in /var/log/.. And "command" is an other program that creates an .html file from that log file)

Permissions of my files are :

-rw-r--r-- 1 abc    www-data   848 Feb 13 10:43 php.php
-rwxr-xr-x 1 ubuntu www-data   230 Feb 13 10:51 script.sh*

And /var/tmp/reports/r.html doesn't exist before execution of PHP. After execution of script directly via command line it creates r.html file like :

-rw-rw-r-- 1 ubuntu ubuntu 121884 Feb 13 11:42 r.html

But when script is executed via PHP from browser it creates an empty file like this

-rw-r--r-- 1 www-data www-data      0 Feb 13 11:43 r.html

Edit1 : On @lurker's suggestio I tried changing script.sh to

#!/bin/sh

cat file.log | awk '{if(substr($5,2)>="'$1'" && substr($5,2)<="'$2'")print $0}' | awk 'gsub(",", "", $1);' | /usr/bin/command > /var/tmp/reports/r.html

It also generated an empty file only.

Edit2 : I changed the script to ->

#!/bin/bash
sudo echo "sdfsf" > /var/tmp/reports/r.html

Even it wont work.

Upvotes: 1

Views: 263

Answers (1)

Sumit Sinha
Sumit Sinha

Reputation: 666

For the command cat file.ext | awk <something with $foo and $bar> | command > /var/tmp/reports/r.html, when I executed it from script.sh directly then it approached the file.ext as ubuntuuser. ubuntu user was authorized to access file.ext, since it belonged to the group that could read it. But when I executed the command form php.php script via browser then it accessed file.ext as abc user (abc was actually www-data user of apache2). Neither the abc user (I mean www-data user) belong to the group which could access the file nor did the file allowed other users to read it. Hence the php script failed to produce the proper output.

Upvotes: 1

Related Questions