0

I am trying to make a kind of panel where I would like to have "start/stop" operations of application(Game server).

HTTP server is running on Windows Server 2008 R2(WAMP SERVER 64-bit)

I found out that I can use: psExec, it isn't waiting for finish application, and at least providing me PID.

There is a code of .bat:

cd "C:\Program Files (x86)\LucasArts\Star Wars Jedi Knight Jedi Academy\GameData\"
.\jampded  +set dedicated 2 +set net_port 29075 +set fs_game lugormod +exec server.cfg 

and the PHP code:

<?php
$command = "\"C:\\Program Files (x86)\\LucasArts\\Star Wars Jedi Knight Jedi Academy\\GameData\\PsExec.exe\" -d \"C:\\Program Files (x86)\\LucasArts\\Star Wars Jedi Knight Jedi Academy\\GameData\\serverstart.bat\"";
//echo ($command);
exec("$command");
?>

showing: error while executing test.php, execution was longer than 30s

But when I am writing that command in console, it's showing PID, and exiting automatically, even when started prog is still being run.

(sorry for my English ;) )

dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85
Disa
  • 630
  • 13
  • 42
  • possible duplicate of [PHP exec() as Background Process (Windows Wampserver Environment)](http://stackoverflow.com/questions/5367261/php-exec-as-background-process-windows-wampserver-environment) – dev-null-dweller Feb 19 '12 at 11:33
  • Fyi, `"$command"` makes no sense. It is identical to `$command` – ThiefMaster Feb 19 '12 at 11:38
  • Related, [How do you run a .bat file from PHP?](http://stackoverflow.com/q/835941) – jww Nov 15 '16 at 04:04

2 Answers2

2

PsExec should be enough to run process in background, however it requires user to accept EULA first (interactive window). If you are running it from command line, you had already accepted the terms, but apache (and php under apache) runs as system user that have not accepted the terms yet.

You can try running psexec with /accepteula option and see if it helps.

[+] To get PID on output:

echo exec('psexec.exe -accepteula -i -d "c:/file.bat" 2>&1')
dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85
  • Because psexec writes pid to STDERR and system user does not have active session running, so you need to add `-i` to psexec arguments and redirect STDERR to STDOUT: `echo exec('psexec.exe -accepteula -i -d "c:/file.bat" 2>&1')` – dev-null-dweller Feb 19 '12 at 14:29
  • Ok, the showed PID is of CMD.exe, and I need PID of jampded.exe(application which start from bat file), is there a way? – Disa Feb 19 '12 at 14:35
  • Either run the code without bat file or use `tasklist` with filters to find it. – dev-null-dweller Feb 19 '12 at 14:53
0

Try this:

$command = "Path_to_your_bat_file";
pclose(popen("start /B ". $command, "r"));

It is working for me..

Cirrus xxx
  • 11
  • 3