Barry Thomas
Barry Thomas

Reputation: 389

Batch File executed via PHP runs all commands as background processes

I am having trouble with a batch file and executing it through PHP.

We have some node servers and Avaya CMS running on a server with restricted access.

The servers need restarting daily as they sometimes crash, so I created a batch file to shut down all cmd windows, node and avaya.

Then it opens all the node servers and avaya, kicks off an AHK script that logins into avaya, and finally launches a few avaya reports.

This all runs fine when I double click the batch file, or if I run it via Task Scheduler, however we now would like to be able to run it via a button click on a PHP page.

This is where I am running in to trouble.

From what I can see, when I click the button, the batch file runs, closes all node servers and thats it, or so I thought, at first.

Through some digging I have found that it is in fact executing the whole batch file, however it runs all the cmd windows and avaya as background processes, and I think this is where my problem lies.

AFAIK when executing batch files from PHP there is no way to have it open cmd, however I thought that the batch file itself would run the cmd windows and avaya in the foreground.

Is there a way to achieve this?

Code below in case it is needed.

PHP

$test = escapeshellcmd('C:\Windows\System32\cmd.exe /k 
"C:\wamp\www\batch_files\R - Wallboards Script"');
system($test);

Batch File

title Launching Servers...
echo Kill Avaya
@echo off
taskkill /f /im "acsCNTRL.exe" /T
taskkill /f /im "acsApp.exe" /T
taskkill /f /im "wscript.exe" /T
taskkill /f /im "ACScript.exe" /T
taskkill /f /im "acsSRV.exe" /T
taskkill /f /im "acsRep.exe" /T


@echo off

call getCmdPID
set "current_pid=%errorlevel%"

for /f "skip=3 tokens=2 delims= " %%a in ('tasklist /fi "imagename eq cmd.exe"') do (
if "%%a" neq "%current_pid%" (
TASKKILL /PID %%a /f /T >nul 2>nul
)
)
timeout 5
echo Kill Node
for /f "skip=3 tokens=2 delims= " %%a in ('tasklist /fi "imagename eq node.exe"') do (
if "%%a" neq "%current_pid%" (
TASKKILL /PID %%a /f /T >nul 2>nul
)
)


@REM DEFINE PATH TO BATCH FILES
SET batch_file_Path="C:\wamp\batch_files"

@REM Change directory to batch files
cd %batch_file_Path%

@REM #######################
@REM # LAUNCH NODE SERVERS #
@REM #######################
echo Launch Node Servers
@REM RADAR
start cmd /k launch_server.bat C:\wamp\www\radar

@REM RADAR_SCHEDULE
start cmd /k launch_server.bat C:\wamp\www\radar_schedule


@REM ######################################################################
@REM # LAUNCH NODE SERVERS WITH "NODE FILENAME.JS" INSTEAD OF "NPM START" #
@REM ######################################################################

@REM LOGIN_DATA_SERVER
start cmd /k launch_server_node.bat login_data_server.js

@REM WALLBOARD_SERVER
start cmd /k launch_server_node.bat wallboard_server.js

@REM SERVER
start cmd /k launch_server_node.bat server.js


@REM ########################
@REM # LAUNCH AVAYA REPORTS #
@REM ########################
echo Launch Avaya
@REM Launch CMS Supervisor and wait 10 seconds
"C:\Program Files (x86)\Avaya\CMS Supervisor R16\acsRun.exe" /L:enu
"C:\Program Files\AutoHotkey\AutoHotkey.exe" "\\Path\To\File\cmsLogin.ahk"
timeout 10
echo Launch Avaya Reports
@REM Launch Reports
"\\Path\To\File\Agent Status.acsup"
"\\Path\To\File\RE - Wallboard.acsup"
"\\Path\To\File\RD - Wallboard.acsup"
"\\Path\To\File\WT - All Agent LoginLogout.acsup"
"\\Path\To\File\WT - Agent Realtime.acsup"

timeout 2
exit

Thanks for your time :)

Barry

Upvotes: 3

Views: 559

Answers (1)

YudhiWidyatama
YudhiWidyatama

Reputation: 1704

As one possible solution, consider separating the PHP that running as web application with the PHP that runs the batch file, and with some mechanism between them to communicate with each other. For example, the first PHP could insert a mysql table row with status 'SUBMIT-RESTART', and the second PHP could be run in windows task scheduler each 2 minutes that does :

  • query the mysql table whether there is a row with 'SUBMIT-RESTART' status
  • if there is such row, change the status into 'RESTARTING', and proceed to run the batch commands,
  • upon finishing, change the status into 'RESTART-DONE'

Upvotes: 1

Related Questions