Page 1 of 1

Help running a shell script from a .php file?

Posted: Sun Oct 22, 2006 2:02 pm
by barrykirk
I am trying to exec a simple shell script from my .php file.

Here is the example at http://www.shuffleboard.no/TestCopy.php
i am having a problem understanding the syntax.

From PHP I am able to upload a file, then create a new one and update it OK.

I then try to do a simple test which executes exec('tst.sh test.txt test1.txt') to add a line (or lines) to the newly created file.

"tst.sh" is just 2 lines

cat $1 > $2
echo "** New line added** " >> $2

Do I need this line at the beginning of my script "#!/usr/local/bin/php -q"??

Should I be executing the tst.sh from the cgi-bin directory?

If I run TestCopy.php from the cgi-bin file I get the following errors:

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, webmaster@shuffleboard.no and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log.

Any help or suggestions would be helpful?



http://www.shuffleboard.no/cgi-bin/TestCopy.php or
http://www.shuffleboard.no/TestCopy.php

Code: Select all

#!/usr/local/bin/php -q 
<?php
echo "Start Now<br>";
$line1 = "<?php\n";
$line2 = "This is  test line created from php\n";
$line3 = "?>\n";

$file = "./Upload/test1.txt";
$handle = fopen("$file", "w");
fwrite($handle, $line1);
fwrite($handle, $line2);
fwrite($handle, $line3);
fclose($handle);
chdir ("Upload");
chmod ("test1.txt", 0777);
echo "Hello just to check that I am Here"; 
echo '<pre>';
$last_line = exec('tst.sh test.txt ./Upload/test1.txt', $retval);
echo '
</pre>
<hr />Last line of the output: ' . $last_line . '
<hr />Return value: ' . $retval;
//exec ("tst.sh test.txt ./Upload/test1.txt"); 	
echo "<br><br> That was it I wonder what the result is?";
?>

Posted: Mon Oct 23, 2006 6:25 am
by Chris Corbyn
:arrow: Moved to PHP Code

The shebang at the start of your code is not needed. You should configure your web server to pass .php files via the PHP interpreter.

No need to run PHP from the cgi-bin directory generally. The internal server error however was likely either because the .php file was not executable or the path at the shebang is wrong.

Re: Help running a shell script from a .php file?

Posted: Thu Dec 18, 2008 2:05 pm
by syth04
to excute the file, you will need this in your php file.

system( scriptname );

Check here for more info