-
Dec 30th, 2020, 02:34 AM
#1
Thread Starter
New Member
Check running program, start if not running VBscript.
Hello!
First of all, im totaly new to this and i dont know if im posting this in the right forum. Admin can delete/move this post if it's in the wrong place.
I have a Windows 2000 server (Yes i know it's lighyears old but i ) where i run 2 small programs that collects data from different places.
One of those programs chrashes 2-3 times a month and therefore i was looking after a script that could check if the program is running and run it if it isnt.
Easy as pie, so I thought.
Googled alot after different script, all that i found and tried got me different error messages and didnt work in Win 2000, most of them worked on my Win10 PC.
Then I foudn this VBscript that didn't produce any error instead nothing happened. so i thought maybe this could work if it just got modified a bit?
And that is why I need your help here guys, is it possible to manage this in Win2000 ?
Script:
Dim objWMIService, colItems, objItem, strComputer, strFlashEXEFile
Dim count
strFlashEXEFileName = "BarkStat.exe"
strFlashEXEFilePath = "C:\BarkStat\BarkStat" & strFlashEXEFileName
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & strComputer & "\root\cimv2")
Set colItems = objWMIService.InstancesOf("Win32_Process")
count = 0
For Each objItem In colItems
If objItem.Name = strFlashEXEFileName Then
count = count + 1
Else
End If
Next
Set objWMIService = Nothing
Set colItems = Nothing
If count = 0 Then
Dim oShell
Set oShell = WScript.CreateObject ("WScript.Shell")
Return = oShell.run(strFlashEXEFilePath,3, false)
Set oShell = Nothing
End If
Last edited by Theatre; Mar 8th, 2021 at 01:13 AM.
-
Dec 30th, 2020, 03:23 AM
#2
Re: Check running program, start if not running VBscript.
You can write a .bat file (not .cmd, this is important) like this
Code:
@echo off
:restart
c:\path\to\MyApp.exe
goto :restart
. . . and run this auto-restarting script instead of launching MyApp.exe directly.
cheers,
</wqw>
-
Dec 30th, 2020, 03:45 AM
#3
Thread Starter
New Member
Re: Check running program, start if not running VBscript.
Oh... why didn't i even thought about the restart command.
I overdid it a bit then. LOL
It worked flawless, thanks!
Br,
Theatre
-
Dec 30th, 2020, 05:04 AM
#4
Re: Check running program, start if not running VBscript.
strFlashEXEFilePath = "C:\BarkStat\BarkStat" & strFlashEXEFileName
i would assume the should be a \ between the file path and file name, but that would also mean the full file path to be C:\BarkStat\BarkStat\BarkStat.exe, which seems may be an extra folder depth
i rewrote some and put in some testing, the code, tested against some .exe file on my machine, runs right through without error and i am sure it should work correctly
you can remove the testing when you are sure it is doing as required
Code:
Dim objWMIService, colItems, objItem, strComputer, strFlashEXEFile
Dim count
strFlashEXEFileName = "BarkStat.exe"
strFlashEXEFilePath = "C:\BarkStat\BarkStat" & strFlashEXEFileName
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") ' impersonation seems essential on w10, but probably not required on xp or other earlier versions
'Set objWMIService = GetObject("winmgmts:" & strComputer & "\root\cimv2")
Set colItems = objWMIService.InstancesOf("Win32_Process")
count = 0
For Each objItem In colItems
If objItem.Name = strFlashEXEFileName Then
count = count + 1
Exit For
End If
Next
msgbox count
If count = 0 Then
Dim oShell
Set oShell = CreateObject("WScript.Shell")
oShell.Run strFlashEXEFilePath, 3, False ' no return from .run
Set oShell = Nothing
For Each objItem In colItems
If objItem.Name = strFlashEXEFileName Then MsgBox "running": Exit For
Next
Else
MsgBox "already running"
End If
Set objWMIService = Nothing
Set colItems = Nothing
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Dec 30th, 2020, 10:00 AM
#5
Re: Check running program, start if not running VBscript.
Originally Posted by Theatre
Oh... why didn't i even thought about the restart command.
May be because that "command" does not exist?
-
Jan 7th, 2021, 01:38 AM
#6
Thread Starter
New Member
Re: Check running program, start if not running VBscript.
Nah not a command, the script thing or whatever you guys call it... LOL.
Last edited by Theatre; Jan 7th, 2021 at 01:46 AM.
-
Jan 7th, 2021, 01:41 AM
#7
Thread Starter
New Member
Re: Check running program, start if not running VBscript.
Originally Posted by westconn1
i would assume the should be a \ between the file path and file name, but that would also mean the full file path to be C:\BarkStat\BarkStat\BarkStat.exe, which seems may be an extra folder depth
i rewrote some and put in some testing, the code, tested against some .exe file on my machine, runs right through without error and i am sure it should work correctly
you can remove the testing when you are sure it is doing as required
Code:
Dim objWMIService, colItems, objItem, strComputer, strFlashEXEFile
Dim count
strFlashEXEFileName = "BarkStat.exe"
strFlashEXEFilePath = "C:\BarkStat\BarkStat" & strFlashEXEFileName
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") ' impersonation seems essential on w10, but probably not required on xp or other earlier versions
'Set objWMIService = GetObject("winmgmts:" & strComputer & "\root\cimv2")
Set colItems = objWMIService.InstancesOf("Win32_Process")
count = 0
For Each objItem In colItems
If objItem.Name = strFlashEXEFileName Then
count = count + 1
Exit For
End If
Next
msgbox count
If count = 0 Then
Dim oShell
Set oShell = CreateObject("WScript.Shell")
oShell.Run strFlashEXEFilePath, 3, False ' no return from .run
Set oShell = Nothing
For Each objItem In colItems
If objItem.Name = strFlashEXEFileName Then MsgBox "running": Exit For
Next
Else
MsgBox "already running"
End If
Set objWMIService = Nothing
Set colItems = Nothing
This also worked perfect, i had to try this aswell.
Thanks for your time, much appreciated!
Br
-
Jan 7th, 2021, 03:22 AM
#8
Thread Starter
New Member
Re: Check running program, start if not running VBscript.
Originally Posted by Theatre
This also worked perfect, i had to try this aswell.
Thanks for your time, much appreciated!
Br
Strange thing is it worked for a couple of runs, then all of a sudden it says (count = 1) "already running" even thou its not running.
I've checked taskmanager and the proccess is not active there...
EDITED: Found that problem, doesn't belong to the script...
Last edited by Theatre; Jan 7th, 2021 at 06:26 AM.
-
Mar 5th, 2021, 03:06 AM
#9
Thread Starter
New Member
Re: [RESOLVED]Check running program, start if not running VBscript.
Hello!
Im resuming this thread because I've got a new problem.
It has been working for some months now but all of a sudden this happend:
Attachment 180420
https://imgur.com/MNPCMVo
Anyone who has any idea, it doesnt work at all now.
I haven't done anything with code, its the same as above.
Br
Theatre
Last edited by Theatre; Mar 5th, 2021 at 06:04 AM.
-
Mar 5th, 2021, 05:37 AM
#10
Re: [RESOLVED]Check running program, start if not running VBscript.
Try the attachment again, this time on imgur, this forum occasionally and regularly eats attachments
-
Mar 5th, 2021, 06:04 AM
#11
Thread Starter
New Member
Re: [RESOLVED]Check running program, start if not running VBscript.
https://imgur.com/MNPCMVo
https://imgur.com/2qvITE8
For some unknown reason the Line is now 28 instead of 23 as the picture describes.
Still havent change a thing in the code.
Last edited by Theatre; Mar 8th, 2021 at 01:07 AM.
-
Mar 9th, 2021, 03:59 AM
#12
Re: Check running program, start if not running VBscript.
I noticed a recommendation to dim all variables. You can use "Option Explicit" at the top of your code to make vbscript enforce this rule for you.
-
Mar 9th, 2021, 04:36 AM
#13
Re: Check running program, start if not running VBscript.
The two lines mentioned are where variables are used, that have not been previously declared.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|