Restart a vbs script if it crashes

时光总嘲笑我的痴心妄想 提交于 2019-12-06 11:40:38

问题


I'm trying to make a vb script that will restart another vb script if it crashes. I have searched, and searched but all I get is how to restart a program and since a vb script is a background process it doesn't work when you search in Win32_Process.

Here is my code

set Service = GetObject ("winmgmts:")
set Shell = WScript.CreateObject("WScript.Shell")

sEXEName = "Test_To_Block.vbs"

while true
 bRunning = false

 for each Process in Service.InstancesOf ("Win32_Process")
  if Process.Name = sEXEName then
   bRunning=true
   msgbox("I am active")
  End If
 next


if bRunning=False then
 msgbox("I am not active.")
 Shell.Run sEXEName
end if


WScript.Sleep(100)

wend

The problem is that it never see's the file running and just opens hundreds of "Test_To_Stop.vbs"'s which resolves in me having to restart the computer.

In my opinion what should be changed is where the code is looking for.

for each Process in Service.InstancesOf ("Win32_Process")

Instead of looking in "Win32_Process" you need to look in wherever background process' run.

I am new to coding so sorry if this is a simple question.

Thank you in advance.

Regards,

A Viper


回答1:


The below code restarts itself via WshShell.Exec() method and trace state of the running script via .Status property of returned object:

If Not WScript.Arguments.Named.Exists("task") Then
    Do
        With CreateObject("WScript.Shell").Exec("""" & WScript.FullName & """ """ & WScript.ScriptFullName & """ ""/task""")
            Do While .Status = 0
                WScript.Sleep 1
            Loop
        End With
    Loop
End If

MsgBox "This script will be restarted immediately after termination"

Another way is to use .Run() method with third parameter set to True to wait until launched process terminated:

If Not WScript.Arguments.Named.Exists("task") Then
    Do
        CreateObject("WScript.Shell").Run """" & WScript.FullName & """ """ & WScript.ScriptFullName & """ ""/task""", 1, True
    Loop
End If

MsgBox "This script will be restarted immediately after termination"

Or even simplier:

If Not WScript.Arguments.Named.Exists("task") Then
    Do
        CreateObject("WScript.Shell").Run """" & WScript.ScriptFullName & """ ""/task""", 1, True
    Loop
End If

MsgBox "This script will be restarted immediately after termination"



回答2:


Probably because the name of the running process is 'wscript.exe' and not 'Test_To_Block.vbs'. You may be able to use the hack mentioned on this page to change the name of the process:

If you're running the scripts locally and running some regular scripts, one common hack is just to copy and rename wscript.exe to a particular name, such as "MyScript1.exe". Then run the script with a shortcut as "...\MyScript1.exe MyScript1.vbs". Then the process will show up as MyScript1.exe.

Then you can use sEXEName = "MyScript1.exe"

Note: instead of using Shell.run sExeName use Shell.run "Test_To_Block.vbs"



来源:https://stackoverflow.com/questions/43968275/restart-a-vbs-script-if-it-crashes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!