‘Autoit’ – giám sát tiến trình và khởi động lại nếu tiến trình ngừng chạy

0. Cách giám sát một tiến trình (qua PID) và khởi động lại tiến trình đó nếu phát hiện ngưng chạy, thông qua ‘autoit’

1.Source:

https://www.autoitscript.com/forum/topic/140046-_alwaysrun-monitor-a-program-and-restart-if-it-closed/

STEP_1:

Function: Using a Batch file. Save as _AlwaysRun.au3

// _AlwaysRun($sFilePath[, $sCommandline = ”])
#include-once
#include <Constants.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: _AlwaysRun
; Description ...: Monitor when a program closes and then restart it.
; Syntax ........: _AlwaysRun($sFilePath[, $sCommandline = ''])
; Parameters ....: $sFilePath           - FilePath of the program to monitor and restart.
;                  $sCommandline        - [optional] A commandline string to pass to the program on restart. Default is ''.
; Return values .: Success - Returns the PID of the batch file.
;                   Failure - Returns 0 & sets @error to non-zero
; Author ........: guinness
; Example .......: Yes
; ===============================================================================================================================
Func _AlwaysRun($sFilePath, $sCommandline = '')
    If $sFilePath = @ScriptFullPath And @Compiled = 0 Then
        Return SetError(1, 0, 0)
    EndIf

    Local Const $STR_NOCASESENSEBASIC = 2
    Local Const $sAppName = StringTrimLeft($sFilePath, StringInStr($sFilePath, '\', $STR_NOCASESENSEBASIC, -1))
    Local $sFileName = StringReplace(@ScriptName, StringTrimLeft(@ScriptName, StringInStr(@ScriptName, '.', $STR_NOCASESENSEBASIC, -1) - 1), '')
    While FileExists(@TempDir & '\' & $sFileName & '.bat')
        $sFileName &= Chr(Random(65, 122, 1))
    WEnd
    If $sCommandline Then
        $sCommandline = ' ' & $sCommandline
    EndIf
    Local Const $sData = ':START' & @CRLF _
             & 'PING -n 2 127.0.0.1 > nul' & @CRLF _
             & @CRLF _
             & 'TASKLIST /NH /FI "IMAGENAME EQ ' & $sAppName & '" | FIND /I "' & $sAppName & '" >nul && GOTO START' & @CRLF _
             & 'GOTO END' & @CRLF _
             & ':END' & @CRLF _
             & 'START "_AlwaysRun" "' & $sFilePath & '"' & $sCommandline & @CRLF _
             & 'GOTO START' & @CRLF

    Local Const $hFileOpen = FileOpen(@TempDir & '\' & $sFileName & '.bat', $FO_OVERWRITE)
    If $hFileOpen = -1 Then
        Return SetError(2, 0, 0)
    EndIf
    FileWrite($hFileOpen, $sData)
    FileClose($hFileOpen)
    Return Run(@TempDir & '\' & $sFileName & '.bat', @TempDir, @SW_HIDE)
EndFunc   ;==>_AlwaysRun

 

STEP_2

Compiled this autoit file, to run with ‘Script full path’

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7
#include <Constants.au3>

#include '_AlwaysRun.au3'

; Compile the following script before running.
If @Compiled Then
    Example()
EndIf

Func Example()
    Local $iPID = IniRead(@ScriptDir & '\_AlwaysRunPID.ini', 'Monitor', 'PID', 0)

    ; Check if the monitoring process is running. If not then call the function _AlwaysRun. (This is the reason why the PID was saved to an INI file.)
    If ProcessExists($iPID) = 0 Then
        ; Start monitoring the current script path for when it closes and restart if necessary. No commandline parameters are passed.
        $iPID = _AlwaysRun(@ScriptFullPath, '')

        If @error Then
            Exit MsgBox($MB_SYSTEMMODAL, '_AlwaysRun()', 'The running script must be a compiled exe to work correctly.')
        EndIf

        ; Save the PID returned by _AlwaysRun to an INI file in case we need to close the monitoring process in the future.
        IniWrite(@ScriptDir & '\_AlwaysRunPID.ini', 'Monitor', 'PID', $iPID)
    Else
        Local Const $aArray[2] = ['No', 'Yes']
        If MsgBox(BitOR($MB_YESNO, $MB_SYSTEMMODAL), '_AlwaysRun Running: ' & $aArray[Number(ProcessExists($iPID) > 0)], 'Would you like to continue running the _AlwaysRun Example?') = 7 Then
            FileDelete(@ScriptDir & '\_AlwaysRunPID.ini')
            Exit ProcessClose($iPID)
        EndIf
    EndIf
    Exit 0
EndFunc   ;==>Example
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s