Jump to content

Help with using exec to run a .bat file showing console windows


Go to solution Solved by kicken,

Recommended Posts

OS: Server 2012

Web: Apache 2.2

PHP: 5.4.0

 

I run a small game server for that I use to record some game footage. I did not want to have to give direct access to my server since it is really not needed. I wrote 3 very simple .bat files that will start, restart, and shutdown the server process. I can run these just fine from the server when logged in directly and the console windows will show up. So I have written 3 very simple web pages that use these commands for my colleagues to run remotely. The problem is that when they are run from the pages using php the processes run in the background but function properly except no console windows. My question is can these be run using the php and still show the console windows?

Here is the restart page (the most commonly used command)

<html>
   <head>
    <title>Restarting</title>
   </head>
   <body>
   <?php
  exec("restart.bat"); 
  window.close()
   ?> 
 
  </body>
 </html>
 

Here is the batch file that kills the server process and restarts it. The ping is just to delay the next command until server process is running fully since it will fail if not.

taskkill /f /im arma2oaserver.exe
  cd c:\dayz
  @echo off
  echo Starting server...
  start .\Expansion\beta\arma2oaserver.exe -mod=Expansion\beta;Expansion\beta\expansion;ca;@hive;@dayz -name=cfgdayz -config=cfgdayz\server.cfg -cfg=cfgdayz\basic.cfg -profiles=cfgdayz
  PING 1.1.1.1 -n 1 -w 60000 >NUL
  cd c:\dayz\whitelist
  start Whitelister.exe
  exit

I guess you can just echo to them what the action was, and hope it actually executed.

 

Otherwise you can look into popen() or proc_open() to check on the actual processes

 

Another option is to write and then delete a new text file, then have php check with file_exists()

taskkill /f /im arma2oaserver.exe
cd c:\dayz
@echo off
echo Starting server...
start .\Expansion\beta\arma2oaserver.exe -mod=Expansion\beta;Expansion\beta\expansion;ca;@hive;@dayz -name=cfgdayz -config=cfgdayz\server.cfg -cfg=cfgdayz\basic.cfg -profiles=cfgdayz
PING 1.1.1.1 -n 1 -w 60000 >NUL
cd c:\dayz\whitelist
start Whitelister.exe
 
IF EXIST "C:\www\stop.txt" (
DEL "C:\www\stop.txt"
set /p texte=< "C:\www\start.txt"
ECHO %texte%
)

exit

Or within the batch file itself change the data within a text file, and always show the text files data when you exec your bat script from php.

 

I guess it depends how complex you want this to become

Edited by QuickOldCar

If you happen to use MIRC and would like a chat bot to be performing these commands let me know.

 

I manage a video casting site and my moderators can control things like start,stop and restart the stream, start,stop,pause the player, do new playlists, switch to different casting types and all sorts of cool stuff.

 

The two codes below are basic ones to start/stop the cast and player, I have more complex ones with macros,voting,audio messages and so on.

 

MIRC casting bot script.

 

on *:text:!start:#:{
if ( $nick isop $chan ) {
describe $chan Cast is starting... Press play or refresh video.
write -c C:\cast\startcast.txt yes

}
if ( $nick !isop $chan ) {
msg $chan Sorry $nick , only mods can Start Cast }
}

on *:text:!stop:#:{
if ( $nick isop $chan ) {
describe $chan Cast is stopping.
write -c C:\cast\stopcast.txt yes

}
if ( $nick !isop $chan ) {
msg $chan Sorry $nick , only mods can Stop Cast }
}

 

This is a run bat script looking for any new files to perform actions.

 

@echo off
:loop
timeout /T 5

IF EXIST "C:\cast\startcast.txt" (
DEL "C:\cast\startcast.txt"

ECHO startcast.txt deleted

ECHO Killing all Potplayer
taskkill /F /IM PotPlayerMini.exe 2>NUL

ECHO Killing all FME
taskkill /F /IM FMLECmd.exe 2>NUL

ECHO PotPlayer Starting
start /d "C:\Program Files (x86)\Daum\PotPlayer\" PotPlayerMini.exe

ECHO FME Starting

taskkill /F /IM fme.bat 2>NUL

start cmd /k Call fme.bat

)

IF EXIST "C:\cast\stopcast.txt" (
DEL "C:\cast\stopcast.txt"
taskkill /F /IM fme.bat 2>NUL
ECHO stopcast.txt deleted
ECHO player stopping
taskkill /F /IM PotPlayerMini.exe 2>NUL
ECHO FME stopping
taskkill /F /IM FMLECmd.exe 2>NUL
)
cls
goto loop

I am not so sure I relayed what I am asking clear enough as I am an extreme novice with this. So I am attaching two pictures hopefully representing what I mean. So if I just run the batch files directly there are these windows that open showing the server console information. When I run them via the PHP pages they still run and are 100% functional I just like to see the console information regardless on how it is run. Please I do not think that it is you that is not understanding I just did not understand your response so I am assuming it was in my explanation. So you see when they are run from PHP they run in the background. I want them to run in the foreground like when directly run.

 

direct_run.jpg

 

php_run.jpg

I understood the question, you said you do not want to give them access which means the shell will not execute under their name.

 

Your server itself is running the shell.

 

If you need them to do something within Whitelister.exe maybe you should pass arguments along with it

 

If you need to merely display the output of the script, it needs to write the data somewhere into like a buffer

It is merely just so I can see what the programs are doing. I could check the logs but I would prefer to just peek in once in a while to see their output. You cannot input anything into the console windows. It is merely just nit picky on my end, I have web tools for them to administer the server. I just simply wanted to be able to see those consoles open when I open the server. I just want both methods to run the bat files the same way if possible. 

  • Solution

If you have apache setup as a service then it will run on it's own virtual console and not be able to interact with the desktop. The easiest way to resolve this is to switch apache so it is not a service but rather runs as your user and is started when you log in. If you run Apache as yourself rather than a service it will have access to the desktop and that should cause the windows to appear when your scripts run the program.

Hmmm I hadn't though about that but it makes a lot of sense thank you. Since I've had to reboot this before (and not logged in immediately) could I possibly leave it as a service and just change the "log on as" and use the same account as the desktop user for the same effect?

I tried this and ran httpd.exe manually after shutting off the service and voila worked perfectly. When I opened the PHP page it ran the batch and all my console windows opened as intended. I don't suppose I could get the best of both worlds though huh? Like keep it running as a service and still have it run in the foreground to open console windows? If not then I am happy just not seeing the console windows as I would rather have Apache run as a service anyways. I really appreciate your help I thought I was losing my mind.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.