Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP on a windows machine; Start process in background

I'm looking for the best, or any way really to start a process from php in the background so I can kill it later in the script.

Right now, I'm using: shell_exec($Command); The problem with this is it waits for the program to close.

I want something that will have the same effect as nohup when I execute the shell command. This will allow me to run the process in the background, so that later in the script it can be closed. I need to close it because this script will run on a regular basis and the program can't be open when this runs.

I've thought of generating a .bat file to run the command in the background, but even then, how do I kill the process later?

The code I've seen for linux is:

$PID = shell_exec("nohup $Command > /dev/null & echo $!");
// Later on to kill it
exec("kill -KILL $PID");

EDIT: Turns out I don't need to kill the process

like image 341
William W Avatar asked Jan 14 '10 22:01

William W


4 Answers

shell_exec('start /B "C:\Path\to\program.exe"');

The /B parameter is key here.

I can't seem to find where I found this anymore. But this works for me.

like image 66
Jelmer Avatar answered Nov 10 '22 12:11

Jelmer


Will this function from the PHP Manual help?

function runAsynchronously($path,$arguments) {
    $WshShell = new COM("WScript.Shell");
    $oShellLink = $WshShell->CreateShortcut("temp.lnk");
    $oShellLink->TargetPath = $path;
    $oShellLink->Arguments = $arguments;
    $oShellLink->WorkingDirectory = dirname($path);
    $oShellLink->WindowStyle = 1;
    $oShellLink->Save();
    $oExec = $WshShell->Run("temp.lnk", 7, false);
    unset($WshShell,$oShellLink,$oExec);
    unlink("temp.lnk");
}
like image 25
Alix Axel Avatar answered Nov 10 '22 12:11

Alix Axel


Tried to achieve the same on a Windows 2000 server with PHP 5.2.8.

None of the solutions worked for me. PHP kept waiting for the response.

Found the solution to be :

$cmd = "E:\PHP_folder_path\php.exe E:\some_folder_path\backgroundProcess.php";
pclose(popen("start /B ". $cmd, "a"));  // mode = "a" since I had some logs to edit
like image 29
Rohit Avatar answered Nov 10 '22 11:11

Rohit


From the php manual for exec:

If a program is started with this function, in order for it to continue running in the background, the output of the program must be redirected to a file or another output stream. Failing to do so will cause PHP to hang until the execution of the program ends.

ie pipe the output into a file and php won't wait for it:

exec('myprog > output.txt');

From memory, I believe there is a control character that you can prepend (like you do with @) to the exec family of commands that also prevents execution from pausing - can't remember what it is though.

Edit Found it! On unix, programs executed with & prepended will run in the background. Sorry, doesn't help you much.

like image 31
Adam Hopkinson Avatar answered Nov 10 '22 12:11

Adam Hopkinson