Search code examples
batch-filecoldfusionwget

window bat to check website and restart coldfusion


I am new to bat script. I want below script to check if web is up and if not restart the coldfusion and save all info into log file.

@REM Remove any existing files.
    del C:\Users\Administrator\check.txt

    @REM to download file
    wget -O check.txt --no-check-certificate https://10.2.3.30/ebanking/ch/en-en/index.cfm

    @REM Search for the term in the previously downloaded file.

    FINDSTR  /C:"index.cfm" check.txt
    if errorlevel equ 1  notfound
    found
    cls


    :notfound

    echo notfound > log.txt
    echo stopping ebanking >> log.txt
    call  C:\ColdFusion10\cfusion\bin\cfstop.bat  

    echo starting ebanking >> log.txt
    call  C:\ColdFusion10\cfusion\bin\cfstart.bat
    cls
    exit

    :found
    echo found >> log.txt

    cls
    exit

Solution

  • You missed goto command for the label notfound. For logic, I swapped label between notfound/found

    @echo off
    
    pushd "C:\Users\Administrator"
    
    @REM Remove any existing files.
    del check.txt
    
    @REM to download file
    wget -O check.txt --no-check-certificate https://10.2.3.30/ebanking/ch/en-en/index.cfm
    
    @REM Search for the term in the previously downloaded file.
    
    FINDSTR  /C:"index.cfm" check.txt
    if errorlevel equ 1 (goto notfound)
    
    :found
    echo found >> log.txt
    exit /b 0
    
    :notfound
    echo notfound > log.txt
    echo stopping ebanking >> log.txt
    call  C:\ColdFusion10\cfusion\bin\cfstop.bat  
    
    echo starting ebanking >> log.txt
    call  C:\ColdFusion10\cfusion\bin\cfstart.bat
    exit
    

    you could simplify the code to the following in a loop

    @echo off
    set "someurl=https://10.2.3.30/ebanking/ch/en-en/index.cfm"
    :loop
    wget --server-response -nv --spider %someurl% 2>&1 | find "HTTP/1.1 200 OK">nul
    if errorlevel 1 (
      echo %DATE% %TIME%: stopping ebanking>> log.txt
      call  C:\ColdFusion10\cfusion\bin\cfstop.bat
      timeout 5>nul
      echo %DATE% %TIME%: starting ebanking>> log.txt
      call  C:\ColdFusion10\cfusion\bin\cfstart.bat
    ) else (
      echo %DATE% %TIME%: site is up>> log.txt
    )
    rem Wait 1 hour before loop
    timeout 3600
    goto loop