Page 1 of 1

Invoke a PHP CLI script from a PHP webpage?

Posted: Thu May 29, 2003 3:07 pm
by TimDroz
Can't seem to get this to work. I have a PHP command-line script that I would like to execute by invoking it from a web page. Basically, I want to be able to trigger the start of this process by hitting a php page on the webserver. Ultimately, I would capture and store the PID, so that I could hit another page and terminate the script (it's a looping job that is doing something for me in the background). It may seem dumb, but indulge me. There's a reason I want to do it this way.

What I have running right now doesn't seem to work. No matter how I invoke the command-line script from within my PHP page, it doesn't seem to execute the command, or give any feedback as to why the process isn't starting. Any ideas?

Here's a basic code example:

Code:

Code: Select all

<?php

// The webpage code


`/usr/local/bin/php /home/test.php`;
//
//     no output

// Also tried:

// exec ("/usr/local/bin/php /home/test.php");
//
//     no output

// system ("/usr/local/bin/php /home/test.php");
//
//    results in "Could not open input file: /home/test.php. "

?>

Code: Select all

<?php

// the CLI code

while (1)
&#123;
    // .... do something
&#125;

?>
Any ideas why the command-line program won't start? Is it because the Apache server is running as "nobody" and that user doesn' have permission to run the php command? If so, what steps should I take to allow this - it's a server running on an intranet, so security is not a huge concern.

Posted: Thu May 29, 2003 3:50 pm
by nielsene
Permissions are definately the first thing to check. Make sure that your webserver user (sounds like "nobody" in your case) has read and execute permissions on the cli script AND to the php cli. Also check if your php is in "safe mode" or not as that restricts the operation of exec and system.

Does running the command manually from the command-line work? If you have root on the server su over to the nobody account and try it there from the cli.

Posted: Fri May 30, 2003 8:09 am
by TimDroz
Running the script from the command line works.

Permissions on the cli script are 777 (for purposes of testing), permissions on the php binary are 755, so permissions shouldn't cause a problem.

Tried to su to "nobody" as root and got "This account is currently not available." Don't think it's set up as a login account, just a system account. In fact, the nobody account is set to have /sbin/nologon as it's login shell. However, changing that to /bin/bash doesn't seem to have any effect either.

SAFE mode is currently off.

Doh! Just found the problem. Permissions on the directory where the cli script is were set to 700. That fixed the problem. Thanks for the help!