我的我的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。什么都没有,但是当我尝试启动服务器时,它将无法启动。我必须重启整台机器才能启动服务器。我想我在这里想念一些愚蠢的东西。有什么想法吗?
答案 0 :(得分: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