1

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.

Sumit Sinha
  • 666
  • 3
  • 9
  • 18
  • `shell_exec($output)` ... what's `$output`? Shouldn't it be `shell_exec($a)`? – lurker Feb 13 '15 at 11:50
  • @lurker oops! I corrected that! – Sumit Sinha Feb 13 '15 at 11:51
  • Where is `file.ext` located? And is `command` defined in a PATH that PHP is operating with? It looks like `./script.sh` is executing, but something is failing in the command string inside the script, so the `> ...` is creating the file, but the execution of what fills it up is failing. – lurker Feb 13 '15 at 12:00
  • "file.ext" is actually a .log file. its located in /var/log/.. And "command" is an other program that creates an .html file from that log file – Sumit Sinha Feb 13 '15 at 12:07
  • Right. I was suggesting that maybe `script.sh` didn't know where it was when executed from PHP. Where is `command` defined? It might be in your PATH when running from the shell, but not in the PATH that PHP sees when it executes. Those PATHs are different. In your `script.sh`, you should try, `/path/to/command` in place of `command` to see if that solves it (`/path/to` is the full path that `command` is found in). – lurker Feb 13 '15 at 12:09
  • You may have problems with the usage of variables in `awk`. What if you show the code of the `awk` part? – fedorqui Feb 13 '15 at 12:14
  • How do I set the path for this? Should I do something like this? http://stackoverflow.com/questions/5144238/modifying-path-for-php-system-calls – Sumit Sinha Feb 13 '15 at 12:14
  • @fedorqui the script works like a charm if I execute it directly from command line like ./script.sh Any way awk thing is like -> | awk '{if(substr($5,2)>="'$1'" && substr($5,2)<="'$2'")print $0}' | awk 'gsub(",", "", $1);' | – Sumit Sinha Feb 13 '15 at 12:15
  • @SumitSinha the thing with `awk` scripts is that they may need `$i` to be escaped to work if called from another platform like PHP: `\$i`, etc. – fedorqui Feb 13 '15 at 12:16
  • @fedorqui Tried. It wont work even then. – Sumit Sinha Feb 13 '15 at 12:19
  • @SumitSinha yes, you could modify the path from PHP, or I think it would as good to update the path from within `script.sh`, or just reference the command by its full path as I indicated in my last comment. – lurker Feb 13 '15 at 12:27
  • @lurker I tried your suggestion. I gave me same empty file as earlier. – Sumit Sinha Feb 13 '15 at 12:32
  • 1
    Please show what you tried by adding it to your problem statement. It's not clear to me what your updated code looks like. Also, not knowing what the PATH is in PHP, maybe it can't find `awk` either? Hard to say without knowing what the PATH is. Try a test case, too, by removing `awk` from your script temporarily and just `cat` a file to the output and see if that works. In other words, break the problem down by process of elimination so you can determine where it is. – lurker Feb 13 '15 at 12:34
  • Sure! please see the Edit in the bottom of the question above. – Sumit Sinha Feb 13 '15 at 12:38
  • See additional information in my answer. I'm pretty sure it's a PATH issue, and it's unclear how restricted the default PATH is in your PHP environment. I also offered some debug tips in my prior comment. – lurker Feb 13 '15 at 12:44
  • @lurker Thanks dear! I am trying those tips of yours . – Sumit Sinha Feb 13 '15 at 12:45
  • Do you have permission to run that command? Maybe the user owner of the script is not in sudoers. – fedorqui Feb 13 '15 at 12:55
  • 'ubuntu' user, the owner of script, is in sudoers. I even tried changing the owner of php program to 'ubuntu' . But then it won't even create an empty file – Sumit Sinha Feb 13 '15 at 13:05
  • What are the permissions on the `/var/tmp/reports` directory? – lurker Feb 13 '15 at 16:20
  • @lurker Permissions for reports directory are `drwxrwxrwx 2 root root 4096 Feb 15 10:21 reports/` . – Sumit Sinha Feb 15 '15 at 10:31

1 Answers1

1

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.

Sumit Sinha
  • 666
  • 3
  • 9
  • 18