Php run batch file in background

Thanks for the response, but this did not work for me. I found that many solution ideas in the internet, but nothing worked for me.

Now i finally found the solution! The keyword is popen! This works nice:

$handle = popen ['start /B C:\Data\restart_TS3.bat > C:\Daten\restart_TS3.log 2>&1', 'r'];
pclose [$handle];
header["Location:ts3_status.php"];

This do following: popen opens a background process that starts the batch file, output goes to the log file. Then you need to close it with pclose. [the batch is still running in background] After that, it opens another website, in my case its a "see current status" website.

If you dont want a log, you can also output [like MarkB told before] with >nul. This would look like that:

$handle = popen ['start /B C:\Data\restart_TS3.bat >nul 2>&1', 'r'];

And be careful with empty spaces in the path! I dont know why, but this will not work:'start /B "C:\Data Folder\restart_TS3.bat" >nul 2>&1' In that cases you need " in the folder like that:

$handle = popen ['start /B C:\"Data Folder"\restart_TS3.bat >nul 2>&1', 'r'];

This works fine with empty space in Folder names.

I try to execute a .bat file [Windows Server with xampp] from php in Background. After i klick on the button, it also should go to another Website.
But when i click on the button, the browser is waiting for the script to finish. Mostly this is ending in a timeout.

my php code:

if [isset[$_POST['test']]]
{
    exec['C:\Daten\test.bat'];
    header["Location:test_status.php"];
}

how can i tell the php exec, to dont wait?

I tried also following, but does not work:

exec['C:\Daten\test.bat' . '> /dev/null &'];

    Table of contents
  • Execute .bat file from php in background
  • Run .cmd or a .bat file from php?
  • Create Batch File [.BAT] to Run EXE Program

Execute .bat file from php in background

if [isset[$_POST['test']]]
{
    exec['C:\Daten\test.bat'];
    header["Location:test_status.php"];
}
exec['C:\Daten\test.bat' . '> /dev/null &'];
$handle = popen ['start /B C:\Data\restart_TS3.bat > C:\Daten\restart_TS3.log 2>&1', 'r'];
pclose [$handle];
header["Location:ts3_status.php"];
$handle = popen ['start /B C:\Data\restart_TS3.bat >nul 2>&1', 'r'];
$handle = popen ['start /B C:\"Data Folder"\restart_TS3.bat >nul 2>&1', 'r'];
exec['start c:\daten\test.bat'];
exec["start cmd /c test.bat", $output];
var_dump[$output];

Run .cmd or a .bat file from php?

Method1
$shell = new COM["WScript.Shell"] or die["Requires Windows Scripting Host"];
$exePath = ["C:\TestFolder\Batch.bat"];//also tried Command.cmd
$shell->exec[$exePath];

Method2
$result = `C:\TestFolder\Batch.bat`;//also tried Command.cmd

Method3
exec["C:\TestFolder\Batch.bat", &$Result];//also tried Command.cmd

Method4
system["cmd /c C:\TestFolder\Batch.bat"];//also tried Command.cmd

How to run a php script in background

Syntax : {command} &

Example: ls -l & exec php index.php > /dev/null 2>&1 & echo $
ps -l [list all process]
ps -ef [all full details of process]
Syntax: nohup exec arg1 arg2 > /dev/null &

Example: nohup exec php process.php hello world > /dev/null &
$proc=new BackgroundProcess['exec php /process.php hello world'];
$proc=new BackgroundProcess[];
$proc->setCmd['exec php /process.php hello world'];
$proc->start[];
$proc=new BackgroundProcess[];
$proc->setCmd['exec php /process.php hello world']->start[];
$process=new BackgroundProcess["curl -s -o /log/log_storewav.log  -d param_key="];
$proc=new BackgroundProcess[];
print_r[$proc->showAllPocess[]];
$proc=new BackgroundProcess[];
$proc->setProcessId[101]->stop[]; //set the process id.

can't execute .bat file from php

c:\scripts\labor\scheduled\cscript.exe c:\scripts\labor\scheduled\labor.vbs /store_list:"store1;store2" /start_date:"2013-01-21" /end_date:"2013-01-24"
$cmd .= 'c:\scripts\labor\scheduled\cscript.exe c:\scripts\labor\scheduled\labor.vbs /store_list:"' . urldecode[$store_list] . '" /start_date:"' . urldecode[$start_date] . '" /end_date:"' . urldecode[$end_date] . '"';
$fname = 'c:\scripts\labor\scheduled\repost_labor.bat';
$fp = fopen[$fname, 'w'];
fwrite[$fp, "@echo off\n" . $cmd];
fclose[$fp];
exec[$fname, $out, $retval];
print_r[$out];
echo[$retval];
Array
[
   [0] => 
   [1] => C:\inetpub\wwwroot\functions>c:\scripts\labor\scheduled\cscript.exe c:\scripts\labor\scheduled\labor.vbs /store_list:"store1;store2" /start_date:"2013-01-21" /end_date:"2013-01-24"
]
@echo off
echo Running
cmd /c cscript.exe c:\scripts\labor\scheduled\labor.vbs /store_list:"store1;store2" /start_date:"2013-02-03" /end_date:"2013-02-05"
cmd /c cscript.exe c:\scripts\labor\scheduled\labor.vbs /store_list:"store1;store2" /start_date:"2013-02-03" /end_date:"2013-02-05"
cmd /c schtasks /delete /tn "repost_labor" /u  /p  /f /s 

Exec

Returned with status 0 and output:
Array
[
    [0] => cmb
]

Create Batch File [.BAT] to Run EXE Program

start "C:\Path\Program.exe"
start "MyProgram" "C:\Path\Program.exe" /param1 /param2
start "" "C:\Path\Program.exe" /param1 /param2
pause
@echo off
start "" "C:\Path\Program.exe" /param1 /param2

Next Lesson PHP Tutorial

Chủ Đề