0

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 ?

8
  • does monitor.php have read and execute permission for other users? Sep 3, 2014 at 10:51
  • have you tried passing the full path? $cmd = "php -n /var/www/.../monitor.php"
    – iswinky
    Sep 3, 2014 at 10:52
  • @KaranPunamiya monitor has the same permissions as the main page that is calling it.
    – Rocket
    Sep 3, 2014 at 10:53
  • @iswinky using the full path doesn't work :(
    – Rocket
    Sep 3, 2014 at 10:55
  • What is the permission set to? The other files are called by your user. The monitor.php file is called by www-data. Sep 3, 2014 at 10:57

1 Answer 1

0

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.