Can anyone tell me how to execute a .bat file from a PHP script?
I have tried:
exec("C:\[path to file]");
system("C:\[path to file]");
Nothing is working. I've checked the PHP manuals and googled around but can't find a good answer. Anyone know where I'm going wrong?
I'm running Windows 2003 Server and have successfully manually run the .bat file and it does what I need it to; I just need to be able to launch it programatically.
This question is related to
php
system
exec
batch-file
<?php
pclose(popen("start /B test.bat", "r")); die();
?>
This snippet is from working code.
You can trigger bat file not only from Windows GUI or Task Scheduler, but directly from PHP script when you need it. But in most cases it will have execution for 30-60 sec. depending from your PHP configuration. If job in BAT file is long and you don't want to freeze your PHP scripts, you need to fork BAT job as another process using php.exe
and not be dependable from Apache.
This runs in background mode in Windows, seen as separate processes cmd.exe
and php.exe
from Task Manager not halting your Apache PHP scripts. The messages produced by your script may be stored and retrieved back via log files.
In my case in file_scanner.php
I do some heavy calculations in loop for big array of files which may last for few hours with php function sleep()
not to overload CPU.
The success poiner result from file $r
which you can query via ajax if you want to know success or fauty start. In my case file_scanner.php
writes log file with messages somefile.jpg - OK
wich you can load to your UI with AJAX every few seconds to show progress.
PHP
/**
* Runs bat file in background mode
*
*/
function run_scanner() {
$c='start /b D:\Web\example.com\tasks\file_scanner.bat';
$r=pclose(popen($c, 'r'));
return json_encode(array('result'=>$r));
}
BAT
@echo Off
D:\PHP\php.exe D:\Web\example.com\tasks\file_scanner.php > D:\Web\example.com\tasks\file_scanner.log
exit
<?php
exec('c:\WINDOWS\system32\cmd.exe /c START C:\Program Files\VideoLAN\VLC\vlc.bat');
?>
on my windows machine 8 machine running IIS 8 I can run the batch file just by putting the bats name and forgettig the path to it. Or by putting the bat in c:\windows\system32 don't ask me how it works but it does. LOL
$test=shell_exec("C:\windows\system32\cmd.exe /c $streamnumX.bat");
When you use the exec()
function, it is as though you have a cmd
terminal open and are typing commands straight to it.
Use single quotes like this $str = exec('start /B Path\to\batch.bat');
The /B
means the bat will be executed in the background so the rest of the php will continue after running that line, as opposed to $str = exec('start /B /C command', $result);
where command
is executed and then result
is stored for later use.
PS: It works for both Windows and Linux.
More details are here http://www.php.net/manual/en/function.exec.php :)
For anyone who needs to run a program in the background "without PHP waiting for it to finish" do this:
pclose(popen("start /B ".$cmd, "r"));
where $cmd
is the string command for the program that you need to run (e.g. $cmd
can equal notepad.exe
or node Path\to\server.js
).
Source: https://www.php.net/manual/en/function.exec.php (see Arno van den Brink's note in the section titled "User Contributed Notes").
Source: Stackoverflow.com