1

Using a batch command, I would check every hour (using the Windows scheduler) if Apache web service is running and if not, start it.

I found this command to start Apache:

C:\apache\bin\https.exe start

but I do not know how to check if it's already started.

My Apache version is 2.4.3

My basic idea (in pseudo-code):

if Apache is Stop then
    Apache Start
ar099968
  • 4,434
  • 5
  • 36
  • 88

1 Answers1

4

I believe the following will work

tasklist | find "httpd.exe"
if ERRORLEVEL = 1 "C:\apache\Bin\httpd.exe start"

We are using the tasklist command to see what is running and piping its output to the find command where we are looking for httpd.exe. If the find command comes up blank it should set the error level to 1, if it returns an output the error level should be set to 0. The if command should run the Apache start command if the error level is 1 otherwise the batch script should exit.

I don't have Apache installed so you may have to check the image name in the task list but I was able to verify the functionality using mmc.exe.

Andrei C
  • 733
  • 7
  • 18
Zero
  • 56
  • 3
  • I also just noticed a thread about the way that windows handles the errorlevel variable in bat and cmd files is different, you may want to read [this](http://stackoverflow.com/questions/148968/windows-batch-files-bat-vs-cmd?rq=1) when before making the batch file. – Zero Jun 25 '14 at 18:14