1

我的 minecraft 服务器经常崩溃,所以我编写了一个脚本来检查我的 minecraft 服务器,如果它启动它什么也不做,如果它关闭它执行以下代码:

Set oShell= CreateObject("WScript.Shell") 
strProcess = "cmd.exe" 
oShell.Run "TaskKill /im " & strProcess & " /f /t", , True
WScript.sleep 1000
oShell.Run "c:\minecraft_launch.bat"
Set oShell = Nothing 

基本上我杀死任何当前正在运行的服务器(cmd,因为它是从批处理文件运行的),然后我重新启动它。此检查通过任务调度程序每 5 分钟运行一次。

这是批处理文件的内容:

@echo off
"C:\Program Files\Java\jre6\bin\java.exe" -Xmx1024M -Xms1024M -jar "%appdata%\- minecraft_server\minecraft_server.jar" >> "%appdata%\- minecraft_server\s.log"

当我运行它时,它可以工作。每次,但是......当它自动运行时,它停止工作。我不知道它会工作多少次,直到它退出。发生的事情是我注意到它已关闭,所以我检查了我的计算机。没有服务器运行,没有进程运行,没有 javaw.exe 或 cmd.exe 运行。没什么,但是当我尝试启动服务器时,它不会启动。我必须重新启动整个机器才能启动服务器。我想我在这里遗漏了一些愚蠢的简单东西。有任何想法吗?

4

1 回答 1

2

问题可能是超时时间太短,因此您尝试在它仍处于关闭状态时启动它。无论如何,vbscript 本身可以通过更多控制检查和终止进程。有关监视和停止进程的简短脚本,请参见http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/processes/。这是来自 Rob Van der woude 的脚本,它通常是可靠的,它监控 Outlook.exe,所以我猜你会监控 javaw.exe

KillProc "outlook.exe"

Sub KillProc( myProcess )
'Authors: Denis St-Pierre and Rob van der Woude
'Purpose: Kills a process and waits until it is truly dead

    Dim blnRunning, colProcesses, objProcess
    blnRunning = False

    Set colProcesses = GetObject( _
                       "winmgmts:{impersonationLevel=impersonate}" _
                       ).ExecQuery( "Select * From Win32_Process", , 48 )
    For Each objProcess in colProcesses
        If LCase( myProcess ) = LCase( objProcess.Name ) Then
            ' Confirm that the process was actually running
            blnRunning = True
            ' Get exact case for the actual process name
            myProcess  = objProcess.Name
            ' Kill all instances of the process
            objProcess.Terminate()
        End If
    Next

    If blnRunning Then
        ' Wait and make sure the process is terminated.
        ' Routine written by Denis St-Pierre.
        Do Until Not blnRunning
            Set colProcesses = GetObject( _
                               "winmgmts:{impersonationLevel=impersonate}" _
                               ).ExecQuery( "Select * From Win32_Process Where Name = '" _
                             & myProcess & "'" )
            WScript.Sleep 100 'Wait for 100 MilliSeconds
            If colProcesses.Count = 0 Then 'If no more processes are running, exit loop
                blnRunning = False
            End If
        Loop
        ' Display a message
        WScript.Echo myProcess & " was terminated"
    Else
        WScript.Echo "Process """ & myProcess & """ not found"
    End If
End Sub
于 2012-06-09T09:58:58.580 回答