简体   繁体   中英

PHP executing background command line script

Not sure how to word this... but here goes.

I have a php script that I want to run in the background, but I want to call it from another php web page.

This is how I'm trying to call it :

if (isset($_REQUEST['go']) && $_REQUEST['go'] =="monitor" ) {
   echo "background script running";
   $cmd = "php -n monitor.php";
   shell_exec("$cmd > /dev/null 2>&1 &");
   exit;
}

monitor is quite simple. It logs into a database and checks for a specific value. If the value is not found it keeps repeating the process, checking every xx seconds to see if the value has been set.

If it is found it updates a log and exits.

The problem I have is that monitor works fine from the command line, but I can't seem to get it to run when called via another page in the browser.

Any ideas ?

Thanks

UPDATE

   $cmd = "php -n monitor.php";
   shell_exec($cmd);

This works but it keeps the page calling it active. As soon as I try to run it in the background it fails.

Any idea why ?

Try using the following approach

$command = '/usr/bin/php -n [PATH]/[FILENAME].php >> /tmp/test.log';

Make sure you have write permission to the log file defined.

Use exec or backtick instead of shell_exec.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM