0

I have a php file

<?php
$op=exec("/usr/bin/sh /home/muralik/www/html/testing/testin.sh >> /home/muralik/www/html/testing/abc.log 2>&1 &",$rs);
echo "OP = ".$op;
echo "<br>RS = <pre>";
print_r($rs);
echo "</pre>"
?>

testin.sh

while(true)
do
echo "standard output"
echo "abc"
sleep 2
done

When i open the php file in localhost or web server. the shell script is not running.

What should i do. thanks in adavance.

2
  • What is the output when you run the script? Commented Aug 16, 2012 at 17:19
  • Nothing i found in abc.log and empty in web page request. Commented Aug 16, 2012 at 17:43

2 Answers 2

1

I believe your script is running, you're just not seeing the output?

The command you're running in your exec redirects both STDOUT and STDERR to abc.log:

/usr/bin/sh /home/muralik/www/html/testing/testin.sh >> /home/muralik/www/html/testing/abc.log 2>&1

Because everything is getting redirected, nothing will end up in $rs, and $op will be empty as well as there will be no last line from the executed command.

Also, your shell script is going to run in an infinite loop, meaning it will never return anything, meaning the exec() should never return as it waits for the execution to finish.

Sign up to request clarification or add additional context in comments.

5 Comments

When i run in command prompt it running fine. But when i am trying to invoke from WEB Browser its not running.
Which are you running in the prompt? The shell script or the PHP file? As mentioned in the answer, you've got an infinite loop in the shell script, so nothing will return to the browser, though if you did a tail -f /home/muralik/www/html/testing/abc.log while you tried to run the PHP, I'd imagine you see the file updating every two seconds (or as output buffer flushes occur). . .
ernie: i have tail -f /home/muralik/www/html/testing/abc.log the file is empty... Even i opened that file and saw. nothing present in the file.... Thanks in adavce..
Once could you try on ur system and do let me know na... It working when i run .php file on terminal..
When i run the same .php file in terminal i can see tail -f /home/,,,,,/abc.log containing standard output and abc...
0

Usually (for security reasons) scripts run from a browser will not run shell commands.

In order to make it work you need to change your .htaccess file (this will tell your server to treat your script as an appliacation)

Add the following lines in your .htaccess:

Options ExecCGI 
AddHandler cgi-script .php

This will allow any php files in the CGI folder to run as an application

3 Comments

Could you tell me where does .htaccess file presents? @pzirkind
Could you tell me where can i edit .htaccess file. I am on ubuntu and LAMP Stack @pzirkind
usually in the root folder of your website - but beware that by default ubuntu ignores them if you need help enabling them see help.ubuntu.com/community/EnablingUseOfApacheHtaccessFiles

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.