Windows设置tomcat守护进程

1、创建tomcatMonitor.ps1文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
$TomcatStartup="D:\apache-tomcat-7.0.75\bin\startup.bat"
function Log-Info {
param (
$msg
)
if(!(Test-Path "D:\log")){
mkdir "D:\log" 2>&1 |Out-Null
}
$RootName = 'D:\log\tomcatMonitor'
$LogDate = (Get-Date).ToString("yyyyMMdd")
$FileName = $RootName + $LogDate + '.log'
$now=(Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
Add-Content -Path $FileName -Value "[$now] [INFO] $msg"
Write-Host "[$now] [INFO] $msg " -ForegroundColor Green
}
function Log-Error {
param (
$msg
)
if(!(Test-Path "D:\log")){
mkdir "D:\log" 2>&1 |Out-Null
}
$RootName = 'D:\log\tomcatMonitor'
$LogDate = (Get-Date).ToString("yyyyMMdd")
$FileName = $RootName + $LogDate + '.log'
$now=(Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
Add-Content -Path $FileName -Value "[$now] [ERROR] $msg "
Write-Host "[$now] [ERROR] $msg " -ForegroundColor Red
}
function Log-Warn {
param (
$msg
)
if(!(Test-Path "D:\log")){
mkdir "D:\log" 2>&1 |Out-Null
}
$RootName = 'D:\log\tomcatMonitor'
$LogDate = (Get-Date).ToString("yyyyMMdd")
$FileName = $RootName + $LogDate + '.log'
$now=(Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
Add-Content -Path $FileName -Value "[$now] [WARN] $msg"
Write-Warning "[$now] [WARN] $msg " $msg -ForegroundColor Yellow
}
function main {
Log-Info "Tomcat守护程序启动。"
$count = 0
while (1) {
Start-Sleep 3
$isrun = Get-WmiObject Win32_Process | select Handle, name, commandline | where {$_.name -eq "java.exe" -and $_.commandline.contains("\tomcat\") }
if($null -eq $isrun){
Log-Info "Tomcat已经退出。"
Restart-Tomcat
}else{
#Log-Info "Tomcat进程正常运行。"
try {
$res = Invoke-WebRequest -Uri "http://127.0.0.1" -Method 'GET' -UseBasicParsing -TimeoutSec 30
if($null -ne $res -and $res.StatusCode -eq 200){
$count = 0
#Log-Info "Tomcat服务正常。"
Continue
}
}
catch [Exception] {
Log-Error $_.Exception.Message
}
$count+=1
Log-Error "Tomcat服务异常,次数:$count"
if($count -gt 2){
Restart-Tomcat $isrun.Handle
}

}
}

}
function Restart-Tomcat {
param (
$xpid=$null
)
Log-Info "开始重启Tomcat"
if($null -ne $xpid ){
Stop-Process -Id [int]$xpid
}
Start-Sleep 2
$env:CATALINA_HOME = "D:\pack\tomcat"
Start-Process $TomcatStartup
}
main
2、创建tomcatMonitor.bat文件
1
2
3
4
5
6
7
8
9
10
11
12
13
@echo off

title Tomcat 守护进程 BY NONGKG
>NUL 2>&1 REG.exe query "HKU\S-1-5-19" || (
ECHO SET UAC = CreateObject^("Shell.Application"^) > "%TEMP%\Getadmin.vbs"
ECHO UAC.ShellExecute "%~f0", "%1", "", "runas", 1 >> "%TEMP%\Getadmin.vbs"
"%TEMP%\Getadmin.vbs"
DEL /f /q "%TEMP%\Getadmin.vbs" 2>NUL
Exit /b
)
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%tomcatMonitor.ps1
PowerShell -windowstyle hidden -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'"
3、修改tomcatMonitor.ps1配置文件

修改tomcat的启动脚本路径

eFU158.png

修改守护进程的日志目录

eFUwV0.png

eFU0aV.png

eFU2rR.png

修改守护进程检测时间间隔,即多久检测一次

eFU7xH.png

修改守护进程判断进程状态条件,根据java进程,搜索出tomcat的进程,可以在后面加条件,判断的来源是进程的命令行代码,后边的tomcat目录可以修改为不同的目录

eFaEd0.png

修改判断tomcat页面是否能打开,IP和端口可修改

eFaGo6.png

修改tomcat的目录,直接指定到tomcat的安装目录即可

eFawyd.png

4、修改完之后双击tomcatMonitor.bat启动守护进程即可
5、把守护进程的启动脚本放到开机自启
1
shell:startup
6、说明
1
2
1、tomcatMonitor.bat为守护进程的启动脚本,tomcatMonitor.ps1为守护进程的配置文件
2、双击启动tomcatMonitor.bat守护进程之后,进程不会在前台运行,而是在后台运行,可在任务管理器中找到Windows PowerShell的进程

eFd11g.png

博主QQ:1012405802
技术交流QQ群:830339411
版权声明:网站内容有原创和转载,如有侵权,请联系删除,谢谢!!
感谢打赏,93bok因你们而精彩!!(支付宝支持花呗)
0%