Oily Rag 1: Wait for process

Oily Rag: Be prepared to get your hands dirty.

Wait for a process to start

Every time I start Uniform Server 3.5-Apollo I get the dreaded error message connection was refused. Clicking the try again button and apanel is displayed.

The connection was refused when attempting to contact localhost.

Though the site seems valid, the browser was unable to establish a connection.

  • Could the site be temporarily unavailable? Try again later.
  • Are you unable to browse other sites? Check the computer's network connection.
  • Is your computer or network protected by a firewall or proxy? Incorrect settings can interfere with Web browsing.

Try again

This situation occurs because because apanel is requested before Apache is running. A quick dirty fixed is to delay this page request thus allowing Apache to be up and fully running before the request is made. A problem with this solution is the choice of delay time, to short and the problem persists to long an it impacts on start-up time. Added to this the delay varies between PC's, OS being used and what processes are running the following looks a method of resolving this problem.

Progress bar

Viewing a blank or static DOS window while something is being processed in the background can be frustrating especially if that process takes a long time. I like user feedback a simple batch file progress bar is a useful feature although not essential.

I produced the following crude progress bar its more therapeutic than functional. It has one redeeming feature can be used inline with text hence does not required a separate DOS window. Image shows a working example:

 

Basics:

Its an incremental star bar, a star being added periodically until the batch file is closed. Save the script on the right to a file named star.bat run the script (double click on file name) to see what it looks like.

  • The script turns echo off (hides the commands) clears screen, outputs a message to user Starting Apache followed by a blank line. Sets variable star equal to the character start.
  • :next and goto next form a loop which is the main workhorse of the batch file.
  • A dummy variable is assigned using set /p which waits for user input. This user input is automatically provided by the <nul file. The prompt string %stars% displays a single star character at the cursor position. The nul file is a valid input to set /p however no data is provided more importantly unlike pressing enter key it does not provide a CR/LF. The cursor remains on the same line after the dummy variable has been set.
  • A delay of one second is provided by ping and the loop is repeated.
@echo off
cls
echo Starting Apache
echo.
set stars=*
:next
(set /p dummy=%stars%) <nul
ping 1.1.1.1 -n 1 -w 1000 > nul
goto :next

Check out Batch file snippets for more information on set/p and ping delay. The above script is a basic building block and requires expanding to make it useful.

Top

Fallback counter

The above progress bar never terminates a fail safe counter is required to terminate this infinite loop. A modified script is shown below save the script to a file named star2.bat run the script (double click on file name) check it terminates.

  • I have introduced a loop counter variable cnt this is initialised to zero.
  • Every time the loop is entered cnt is incremented using set /a cnt+=1.
  • It is then compared to a value 60 how many times we wish to execute the loop before giving up. When this value is reached it jumps to :failed thus terminating the infinite loop.


That completes the status indicator, before adding functionality a method of determining when a process has started is required.

Remember I am using XP Home edition hence options are limited.

Note: To check what processes are running press keys ctrl, alt and del together, this opens Windows Task Manger, click the process tab to view running processes.

@echo off
cls
echo Starting Apache
echo.
set stars=*
set /a cnt=0
:next
(set /p dummy=%stars%) <nul
set /a cnt+=1
if (%cnt%)==(60) goto :failed
ping 1.1.1.1 -n 1 -w 1000 > nul
goto :next
:failed:
echo.
echo Apache failed to start
pause

Top

Is process running

This section looks at two solutions to determine if a process is running.

Solution 1 - Pure batch method

There is a command prompt equivalent to Windows Task Manger, typing qprocess into a command window displays a list of currently running processes.

This command can be run from a batch file and its output redirected to a file. Searching for strings in a file is made easy using the FIND command, hence it is possible to check the existence of a process knowing its name.

The following script searches for the apache.exe process and displays either found or not found.

  • qprocess normally writes to screen this is redirected to a text file result.txt
  • FIND searches the file for the string apache.exe normally writes to screen this is redirected to the nul file.

We are interested in only the return code (ERROR LEVEL) 1:

  • 0 FIND Completed Successfully and at least one Match was Found
  • 1 FIND Completed Successfully, but no Matches were Found
  • 2 or Higher FIND did not Complete due to an Error

If the error level is 1 apache.exe was not found in the list jump to :notfound.

Note: There are two Apache processes running main and sub-process no need to distinguish between them.

@echo off

qprocess > result.txt
FIND "apache.exe" result.txt >nul
IF ERRORLEVEL 1 goto :notfound

echo Apache found
del result.txt
pause
exit 1
:notfound
echo Apache not found
del result.txt
pause
exit 2

Top

Solution 2 - pskill utility

The above pure batch file solution requires creating a temporary file which needs to be searched hence several disk accesses are required. A more elegant solution is to use Uniform Server's built in utility pskill. The above script can be modified as follows note assumes the utility is located in the same folder as the batch file.

Note: pskill.exe is a dual function utility:

  • Run with second parameter set to C will kill a named process.
  • Run without the c parameter will check the status of a named process.

Status messages are displayed these are redirected to the nul file.

We are interested in only the return code (ERROR LEVEL) 1:

  • 1 or Higher named process not running
  • 2 or Higher system error message e.g. platform not supported.
@echo off

pskill.exe apache.exe > nul
IF ERRORLEVEL 1 goto :notfound

echo Apache found
pause
exit 1
:notfound
echo Apache not found
pause
exit 2

Top

Process progress bar

To create a process progress bar integrate process detection, fallback counter with basic progress bar. A working script is shown on the right.

Note 1: Again assumes the utility pskill.exe is located in the same folder as the batch file.

Note 2: Change some of the values to get a feel for what it does.


Note 3: If you are not worried about error checking or having a fancy progress bar and just want to wait for the process to start the following four lines are all that's required.

:next
ping 1.1.1.1 -n 1 -w 1000 > nul
\home\admin\program\pskill.exe apache.exe > nul
IF ERRORLEVEL 1 goto :next
@echo off
cls
echo Starting Apache
set stars=*
set /a cnt=0
:next
(set /p dummy=%stars%) <nul
set /a cnt+=1
if (%cnt%)==(60) goto :failed
ping 1.1.1.1 -n 1 -w 1000 > nul
pskill.exe apache.exe > nul
IF ERRORLEVEL 1 goto :next
echo.
echo Apache was started
goto :end
:failed
echo.
echo Apache failed to start
:end
pause

Top

Server_Start.bat

A quick fix for error message connection was refused is to place these four lines just above line:

start \home\admin\www\redirect.html

:next
ping 1.1.1.1 -n 1 -w 1000 > nul
\home\admin\program\pskill.exe apache.exe > nul
IF ERRORLEVEL 1 goto :next

To grantee both servers are running I popped a similar section just below the line:

start \usr\local\mysql\bin\mysqld-opt.exe..........

:next
ping 1.1.1.1 -n 1 -w 1000 > nul
\home\admin\program\pskill.exe mysqld-opt.exe > nul
IF ERRORLEVEL 1 goto :next

Interestingly I found Apache start up time did not justify the use a progress bar.

In the end I went for the above quick fix option.


Alternative:

This provides a progress bar. Again place this section just above line:

start \home\admin\www\redirect.html

rem ##############################################
echo Starting Apache
rem ### Set character for progress bar. 
set stars=*
rem ###Set fail safe loop counter to 0
set /a cnt=0
:next
rem ### Display progress character no cr/lf
(set /p dummy=%stars%) <nul
rem ### increment loop counter
set /a cnt+=1
rem ### Max times around loop Apache failed to start
if (%cnt%)==(60) goto :FAILED
rem ### delay prevents locking PC in a tight loop
ping 1.1.1.1 -n 1 -w 1000 > nul
rem ### check process started
\home\admin\program\pskill.exe apache.exe > nul
rem ### not started check again
IF ERRORLEVEL 1 goto :next
echo.
echo Apache was started
rem ################################################

Failed: Add Failed section just below the Hint section as shown:
:HINT
CLS
echo The disk %Disk% is busy. Use start.bat [disk letter]
goto :PAUSE

:FAILED
CLS
echo Unable to start Apache
goto :PAUSE

Conclusion

I use the quick fix method it works fine on my slow XP Home machine, not sure how backwards compatible it is. As for the progress bar its a useful feature if you require inline feedback in a batch file.

Top


  Ric