1

Here is my code:

<?php
    require_once 'dbconnect.php';
    function execInBackground($cmd) { 
        if (substr(php_uname(), 0, 7) == "Windows"){ 
            pclose(popen("start /B ". $cmd, "r"));  
        } 
        else { 
            exec($cmd . " > /dev/null &");   
        } 
    } 

    if(isset($_GET['date'])){
        //CHECK LOCK
        $checkLock = "Select IS_FREE_LOCK('overnight') as `lock`;";
        $result = mysql_query($checkLock) or die(mysql_error());
        while($information = mysql_fetch_array($result)){
            if($information['lock'] == 0){
                die('Overnight is already running, please try again later.');
            }
        }
        execInBackground("php overnightQueries.php {$_GET['date']}");
        //echo "<pre>".print_r($output2, true)."</pre>";
        header('Refresh: 3; url=index.php');
        die('running queries...');
    }
    else {

        die('PLEASE SET DATE');

    }
?>

I am using a windows machine.

I get the following warnings:

Warning: popen(start /B php overnightQueries.php 2011_08_12,r): No error in C:\inetpub\GTSA\runOvernight.php on line 5

AND:

Warning: pclose() expects parameter 1 to be resource, boolean given in C:\inetpub\GTSA\runOvernight.php on line 5

Naftali
  • 144,921
  • 39
  • 244
  • 303
  • I honestly do not remember @Muhwu :-( it was > 4 years ago. – Naftali Oct 20 '15 at 13:27
  • Oh wow, didn't notice the year. Can't say that I blame you. It's a shame though. I'm thinking it has something to do with how it processes the output because doing popen("start /B ". $cmd ." > C:\\file.txt", "r"); works but take away the > C:\... part and it doesn't. – Muhwu Oct 20 '15 at 15:57

1 Answers1

0
$handle = popen("start /B ". $cmd, "r");  
if ($handle === FALSE) {
   die("Unable to execute $cmd");
}
pclose($handle);

popen returns false if there was a problem, which you blindly pass to pclose, hence the second error.

As for the first error, check that PHP is in your environment's path - you may need to specify an absolute path to php.exe.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • hmmm php is in the PATH. im going to try your answer, one minute. – Naftali Aug 12 '11 at 15:53
  • ok now i get: `Unable to execute php overnightQueries.php 2011_08_12` what could the issue be? – Naftali Aug 12 '11 at 15:55
  • try adding an absolute path to php, e.g. `c:\bin\php.exe`. Note that if your path has spaces, you need to quote the command, and start has a few oddities with quotes: http://stackoverflow.com/questions/154075/using-the-dos-start-command-with-parameters-passed-to-the-started-program – Marc B Aug 12 '11 at 16:01
  • now i get `Unable to execute c:\php\php.exe overnightQueries.php 2011_08_12` (basically the same thing) – Naftali Aug 12 '11 at 16:02
  • ok. so start or cmd is seing overnightQueries... as part of the command name. you might have to try something like `start "" "c:\bin\php" overnight...`. That or the webserver process this script is running under doesn't have access/execute rights on \php or the php.exe – Marc B Aug 12 '11 at 16:02
  • 1
    also forgot to mention that this same script works fine on a different windows vm – Naftali Aug 12 '11 at 16:05
  • just looked at the IIS logs and the page is being hit. but it diesnt seem to be running the script. how do i make it so it creates an output file? – Naftali Aug 12 '11 at 16:13
  • I still have this issue, if I redirect the command output to a file (i.e. > C:\\temp\\file.txt), that appears to fix the issue. It's really weird. – Muhwu Oct 20 '15 at 12:46
  • hi, its a long time now, can you please let me know how are you getting the date in the overnightQueries.php ? please – Yasar Khalid Jul 17 '21 at 14:22