Stopping a run away VBScript (.vbs)

Is there any way I can stop a VBscrip (.vbs)  gracefully if it gets hung in a loop?

Can I program logic into the .vbs script to respond to a signal or break key?
Christopher ScheneSystem Engineer/Software EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

code please ...???
Serge FournierAnalyst ProgrammerCommented:
kill the wscript.exe process
(ctrl alt del, task manager, process, vbscript.exe, terminate process)

this will stop any WSH or VBS from running

since vbs use a child parent system, the remaning links (open files etc) will close when the process is ended

you can also programe some event stuff in the vbs
but the event will call a sub inside your own program

sub IE_onQuit()
end sub

this sub is execute if internet explorer object (created earlier in script) is closed
but i dont know any event that can trigger on a key press, like a break
in vba (macro in office etc) you can use ctrl break
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
code please ...??? This is basically a home grown network tester that is running in an infinite loop. The "SendHttpRequest" is just some code that send an http request over the network

While True
      On Error Resume next
      actual_loop_cnt = actual_loop_cnt + 1
      For i = 1 To Hostname_count
            targetURL = Replace(cannedstrURLGetAgent, "TheHostName", Hostname_table(i))            
            SendHttpRequest objWinHttp, "hist", targetURL,response,status
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Serge FournierAnalyst ProgrammerCommented:
you could ping the host before sending the request

here is a code for a local network ping before i access a computer to send it stuff

of course you have to change the name of the computer you ping to a www name or something

'My fast ping function
Function FastPing(strComputer)
   return = objshe.Run("ping "&strComputer&" -n 1 -w 500", 0, true)
   if return = 0 then 
      return = 1
      return = 0
   End if
   fastping = return
End function

Open in new window

Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
I should have been a bit more specific. I created a network test .vbs script that is used to test http requests sent to my servers and load balancer. I am doing this because I think there is a network problem in the connection to the servers.

The code I created runs in an infinite loop continually sending http requests every n seconds. I want the code to to run continuously until I want it to stop and I would like a graceful way of stopping other than just killing the wscript.

So, what I am asking for is the graceful way of stopping the test script.

Make sense?
Serge FournierAnalyst ProgrammerCommented:
the terminate process in taks manager is gracefull, it will ask the wscript.exe to terminate gracefully
then if it does not want to, it will ask to terminate the task forcefully

so it was a gracefull way

maybe you dont want to use the console/task manager everytime?

ok there is a way without using the console

use pskill.exe in a .bat file:
--------.bat file ------------
pskill.exe wscript.exe

unfortunatly, it will kill all the scripts in progress and it does not ask them to be terminated gracefully this time

Serge FournierAnalyst ProgrammerCommented:
i can post a script that search for name and id in process to terminate the right wscript.exe

but the initial script would have to write this id somewhere for the second script to know wich process to terminate in a gracefull way

if you want all that complicated ;)
Hi cschene,

My advice would be to run it using CScript and not WScript.  This way, when you want the program to finish, just close the window.

Either commandline:
CScript "C:\path\something.vbs"

or right-click the vbs file and "Open with command prompt".

The down side to this is if the script gets an error it wont produce an error message box, it will close the window.  So test with WScript and once you are sure it is robust switch to CScript.

Hope this helps,
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:

I tried running it using cscript and when I closed the DOS windows it kept on running
And when I look at task manager it is still running
Hi cschene,

When you say 'still running', what exactly is still running?  The VBScript process when run with CScript is cscript.exe so that should end as soon as you close the DOS-style window.  Unless your script calls another script using WScript.exe then any wscript.exe processes would be unrelated to the cscript.exe process.

I think we're at the stage now where it would be most helpful to see your whole script ..  :o)

Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
I actually solved this one myself: My VBscript is checking for the existence of a file and if that file exists, the code exits.

While True
      On Error Resume next
      If fso.FileExists(".\stopdebug") Then
      End If
      EchoAndLog "date = " & Date
      actual_loop_cnt = actual_loop_cnt + 1
      EchoAndLog " "
            EchoAndLog "*******************************************************************"
      Dim i
      For i = 1 To Hostname_count
            EchoAndLog " "
            EchoAndLog "Loopset:    " & actual_loop_cnt
            targetURL = Replace(cannedstrURLGetAgent, "TheHostName", Hostname_table(i))            
            SendHttpRequest objWinHttp, "hist", targetURL,response,status

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.