Creating Batch Process On Win Box?

Discussion in 'PHP' started by tdd1984, Jun 7, 2008.

  1. #1
    I'm just curious how can I create a batch process (php script to run in the background) on a windows box?

    I don't think shell_exec() works the same on a windows box as it does a *nix box. I'll eventually use the shell_exec() when I upload the actual files to my server, but right now I'm testing on a windows box and just curious on how I can create a batch process through my current box I'm testing on?

    Also how can I get the PID number so I can pause or stop the batch file when I need to?
     
    tdd1984, Jun 7, 2008 IP
  2. joffer

    joffer Peon

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hello , to run something in the background on windows you need to use the program called "start".
    In the cmd type "help start" to get more info for it.
    About the php script you can do something like this :

    exec('start php.exe script.php parameters');
    Code (markup):
     
    joffer, Jun 11, 2008 IP
  3. tdd1984

    tdd1984 Well-Known Member

    Messages:
    2,357
    Likes Received:
    42
    Best Answers:
    0
    Trophy Points:
    150
    #3
    I know that, but what I'm saying will it act the same as what linux does?

    I see what you did though so I need to start the php before the actual script. I did'nt do that before. Before I just entered in exec('/script.php') then the file would show up in my editor :)
     
    tdd1984, Jun 11, 2008 IP
  4. joffer

    joffer Peon

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    The way with the "start" will work the same way as on a linux box & , offcourse you probably know that it cant actually work the same way :p
     
    joffer, Jun 13, 2008 IP
  5. tdd1984

    tdd1984 Well-Known Member

    Messages:
    2,357
    Likes Received:
    42
    Best Answers:
    0
    Trophy Points:
    150
    #5
    Thanks man...

    That's what I was wondering because before it would open the actual file up through my editor and not through the background in PHP.

    Also you wouldn't happen to know how to grab the PID number would you because I will want to stop it during certain periods?
     
    tdd1984, Jun 13, 2008 IP
  6. joffer

    joffer Peon

    Messages:
    85
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Best way is to use pstools :

    <?php
    // pstools.inc.php
    
        function PsExecute($command, $timeout = 60, $sleep = 2) {
            // First, execute the process, get the process ID
            $pid = PsExec($command);
            
            if( $pid === false )
                return false;
            
            $cur = 0;
            // Second, loop for $timeout seconds checking if process is running
            while( $cur < $timeout ) {
                sleep($sleep);
                $cur += $sleep;
                // If process is no longer running, return true;
                if( !PsExists($pid) )
                    return true; // Process must have exited, success!
            }
            
            // If process is still running after timeout, kill the process and return false
            PsKill($pid);
            return false;
        }
        
        function PsExec($command) {
            exec( dirname(__FILE__). "\\psexec.exe -s -d $command  2>&1", $output);
    
            while( list(,$row) = each($output) ) {
                $found = stripos($row, 'with process ID ');
                if( $found )
                    return substr($row, $found, strlen($row)-$found-strlen('with process ID ')-1); // chop off last character '.' from line
            }
            
            return false;
        }
        
        function PsExists($pid) {
            exec( dirname(__FILE__). "\\pslist.exe $pid 2>&1", $output);
    
            while( list(,$row) = each($output) ) {
                $found = stristr($row, "process $pid was not found");
                if( $found !== false )
                    return false;
            }
            
            return true;
        }
        
        function PsKill($pid) {
            exec( dirname(__FILE__). "\\pskill.exe $pid", $output);
        }
    ?>
    Code (markup):
    And dont forget to download the pstools from here http://technet.microsoft.com/en-us/sysinternals/bb896649.aspx
     
    joffer, Jun 16, 2008 IP