WINDOWS program monitoring and automatic restart and bat script implementation

Recently, in the operation of an old system, the program process is often shut down automatically. Because there is no developer to maintain the system, the program bug can not be handled by modifying the code. Therefore, it is necessary to write a script for automatic monitoring and failure recovery.

phenomenon

One program will shut down automatically. Another program may have BUG, which will cause database deadlock.
Handling ideas:

1. Database monitoring: monitor the data written in the database, and report the exception when the number of written records is less than a certain amount;
2. Monitor and restart the process. If no process is found, restart the process directly.
3. When the monitoring database is found, sometimes the database will be deadlocked, resulting in no data to be queried. Judge the query database, and restart the database service when the query exception is returned.

Script implementation

1. Database monitoring (vb script) gn2.vbs:

Function Format_Time(s_Time)
    Dim y, m, d, h, mi, s
    Format_Time = ""
    If IsDate(s_Time) = False Then Exit Function
        y = cstr(year(s_Time))
        m = cstr(month(s_Time))
        If len(m) = 1 Then m = "0" & m
        d = cstr(day(s_Time))
        If len(d) = 1 Then d = "0" & d
        h = cstr(hour(s_Time))
        If len(h) = 1 Then h = "0" & h
        mi = cstr(minute(s_Time))
        If len(mi) = 1 Then mi = "0" & mi
        s = cstr(second(s_Time))
        If len(s) = 1 Then s = "0" & s
    Format_Time = y  & "-" & m & "-" & d & " "  & h & ":"  & mi & ":"  & s
End Function

'Database connection string
Dim DBConnStr
DBConnStr = "PROVIDER=SQLOLEDB;DATA SOURCE=111.111.222.222,1433;UID=sa;PWD=1122aa.0;DATABASE=database"

Dim v_DBRecordSet
Dim v_DBRecCnt
Dim v_CheckTime

'Calculate query time 5 minutes ahead of current time
v_CheckTime = DateAdd("n",-5,now())
v_DBRecCnt = 0

'Create a Recordset object
On Error Resume Next
Set v_DBRecordSet=CreateObject("ADODB.Recordset")
If Err.Number Then
WScript.Echo "Message:" & Err.Description
WScript.Quit(1)
End If

'Initialize recordset parameters
v_DBRecordSet.CursorType = 0
v_DBRecordSet.CursorLocation = 2
v_DBRecordSet.LockType = 1
v_DBRecordSet.Source = "Select count(*) as CN from RunLog where SysDatetime >'" & Format_Time(v_CheckTime) & "'" 

On Error Resume Next
v_DBRecordSet.ActiveConnection = DBConnStr
If Err.Number Then
WScript.Echo "Message:" & Err.Description
WScript.Quit(1)
End If

On Error Resume Next
v_DBRecordSet.Open()
If Err.Number Then
WScript.Echo "Message:" & Err.Description
WScript.Quit(1)
End If

On Error Resume Next
v_DBRecCnt = v_DBRecordSet.Fields("CN")
If Err.Number Then
WScript.Echo "Message:" & Err.Description
WScript.Quit(1)
End If

'If equal to 0, alarm
if CInt(v_DBRecCnt) >100 Then
                'Close Recordset
           v_DBRecordSet.Close()
                Set v_DBRecordSet = Nothing
    'Normal exit
           WScript.Echo "Number of storage entries in the first 5 minutes:" & v_DBRecCnt
    WScript.Quit(0)
else
                'Close Recordset
           v_DBRecordSet.Close()
                Set v_DBRecordSet = Nothing
    'Abnormal exit
           WScript.Echo "Number of storage entries in the first 5 minutes:" & v_DBRecCnt
    WScript.Quit(1)
end if

2. Monitoring database and restart service related script (bat script):

setlocal enabledelayedexpansion
cscript //nologo c:\gn2.vbs
If ERRORLEVEL 1 (net stop SQLSERVERAGENT
net stop MSSQLSERVER
PING 172.18.18.2 -n 2
net start MSSQLSERVER
net start SQLSERVERAGENT
echo %date:~0,10% %time:~0,8% Restart MSSQLSERVER Service >>c:\RESTART_GN.txt ) else goto check_gn_carrunlog
:check_gn_carrunlog
set /a carrunlog2data=0
PING 172.18.18.2 -n 50
for /f "delims=" %%! in ('cscript //nologo c:\gn2.vbs') do set /a carrunlog2data =%%!
if "%carrunlog2data%" leq "0" (
call RESTART_GN.bat)

else exit

3. Automatic monitoring process and restart process script (bat script):

setlocal enabledelayedexpansion
echo ############################Always monitor, do not close!######################### 
ping 172.18.18.2 -n 10 
tasklist /nh|find /i "w3wp.exe"  
If ERRORLEVEL 1 (start C:\web\web Communication server\w3wp.exe 
echo %date:~0,10% %time:~0,8% Restart w3wp.exe >>c:\Restart_WEB.txt
 ) else exit

4. In order to execute the above script regularly, the original plan is to automatically execute it by turning on the machine, but it is considered that there will be an occasional manual restart program. Instead, it is to call and execute it regularly (5 minutes or 10 minutes) through the WINDOWS scheduled task. At present, the execution effect is good.

Tags: Windows Database less

Posted on Wed, 22 Apr 2020 03:20:48 -0700 by Leveecius