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.
|
@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.
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.
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 |
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.
We are interested in only the return code (ERROR LEVEL) 1:
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 |
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:
Status messages are displayed these are redirected to the nul file. We are interested in only the return code (ERROR LEVEL) 1:
|
@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 |
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.
: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 |
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: :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.
Ric |