Windows under Bat Curl implements Tomcat daemon

Source: Internet
Author: User
Tags curl tomcat

Friends of the company Tomcat running under the Windows platform, its mention Tomcat is not very stable, always hanging, there is no good way. One of the suggestions given to him was to open a Tomcat daemon. Because Windows does not look like Linux (Linux platform commands, all kinds of syntax, all kinds of convenience), win platform under the bat feel weak and difficult to understand. But here's a look at the Tomcat daemon script that was written before, and changed it to write one.

The same thought, gets the status code for a URL, compares the HTTP status code to 200, or, if not 200, restarts a particular tomcat for that host (for multiple tomcat on a single host). To get the status code, there are two ways to achieve the pure win platform: one is to increase the curl.exe, the other is through the VBS implementation.

First, Curl.exe implementation

The bat code is as follows:

@echo off
: Loop
Set "Httpcode="
CD/D C:\curl
for/f "delims="%%r in (' Curl.exe-sl-w "%%{http_code}" "http://127.0.0.1:8080/"-o nul ") do (
Set httpcode=%%r
)
Echo%httpcode%
If not "%httpcode%" = = "200" (
taskkill/f/fi "WindowTitle eq tomcat_8080"
Ping-n 3 127.0.0.1>nul
Set Catalina_home=c:\tomcat
Set Executable=%catalina_home%\bin\catalina.bat
REM call "%executable%" Start && exit
Call "%executable%" start
) Else (
echo "The Tomcat Run is ok! '
)
Ping-n 127.0.0.1>nul
Goto Loop

Waiting here is done by ping waiting, now using sleep, you need to call the VBS script implementation, you can see the timing waiting under Windows. Get the Httpcode variable here, for example, to get the value is 200, the following is a space, so the condition is written when the following 200 also have spaces, of course, can be curl |findstr way to interpret whether there are 200 to do.

Taskkill here. When you kill a process, it matches the title of the window, which can be differentiated when a host starts multiple tomcat. A way to specify different headings for different tomat is to modify the Tomcat window title.

Second, the VBS implementation

There is no code for a complete functional implementation, because the VBS and BAT are too painful to write, but here's a sample code:

' Get Httpcode
Set Ohttp=createobject ("MSXML2. XMLHTTP ")
Ohttp.open "Get", "https://www.111cn.net/", False
Ohttp.send
If ohttp.status = MB Then
MsgBox ("Http Status Code is OK.")
Else
MsgBox ("normal"). ")
End If
' WScript.Echo Ohttp.status
Set ohttp=nothing

BAT if the VBS script is invoked, it can also be invoked through the cscript command.

Third, other

For multiple URLs, the state and connection time through the bat, and a piece of code is very good, the contents are as follows:

@echo off
setlocal enableextensions disabledelayedexpansion
REM Just to test-generate a file with URLs
> ". \urls.txt" (
Echo http://superuser.com
Echo http://www.111cn.net
Echo http://www.baidu.com
)
> ". \testresults.txt" (
for/f "Useback delims="%%u in (". \urls.txt") does (
Set "Statuscode="
Echo ([%%u]
CD/D c:\curl\
for/f "tokens=1,2 delims=#"%%a in ('
Curl-w "##%%{time_connect}##."-i-s--url "%%~u"
^| FINDSTR/L/b/c: "http/"/C: "# #"
' Do if '%%b ' = = '. (
Setlocal enabledelayedexpansion
Echo (!statuscode!-%%a
Endlocal
) Else (
Set "Statuscode=%%a"
)
)
)

Although it looks more subtle, but still more difficult to understand, this is based on a foreign station on a piece of code changed.

No nutritional content to get here first, this article is just to make a comparison, followed by the preparation of a Python implementation of the Windows daemon. And then through the Pyexe to generate EXE file, feel better than bat a lot.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.