Tuesday, July 3, 2007

call bash script inside php web page

here is a simple way to call a simple bash script inside php script page and have a return value printed to your web page.

below is a simple bash script called


echo "Successful call from PHP : "
exit 0

chmod 700 so it would be root executable, and saved it to your apache root folder say /var/www/html/

and here's a simple browseable php script called


$program = "/var/www/html/php.sh";
$ver1 = system($program, $retval);
if ($retval == 0 ) {
echo "Returned 0";
else {
echo "Returned not zero";

saved to same root location where your php.sh currently resides.

If you browse the page test.php, it would have a return value spitted out by php.sh bash script into test.php web page.

There are moments from a page that you need to call a server side script and linux tool to determine and know something first (return value) before proceeding to next set of php commands, this would be expanded and could really be helpful if you do php coding as well.

hope this helps


Sign up for PayPal and start accepting credit card payments instantly.
ILoveTux - howtos and news | About | Contact | TOS | Policy