Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Prevent Users Executing a Script Twice

289 views
Skip to first unread message

Daniel...@gmail.com

unread,
Sep 6, 2006, 8:55:48 PM9/6/06
to
Hi Everyone,

Does anyone know a neat way to prevent users executing a script two or
more time simultaneously? Apart from teaching them to be more patient!
Ideally I'm looking for a way to make the script realise that it is
already running in a previous instance and either just quit or send the
user a message and then quit.

Cheers,
Danny....

Bill James

unread,
Sep 7, 2006, 1:54:36 AM9/7/06
to
One old kludge is to use a flag file with a unique name. If the file exists, exit, otherwise create the file, do your processing, then delete the file. The caveat is that is your script crashes the flag file will still be there preventing the script from running again.

--

Bill James
Microsoft MVP - Shell/User

Windows VBScript Utilities » www.billsway.com/vbspage/
Windows Tweaks & Tips » www.billsway.com/notes_public/

<Daniel...@gmail.com> wrote in message news:1157590548.2...@d34g2000cwd.googlegroups.com...

McKirahan

unread,
Sep 7, 2006, 2:51:10 AM9/7/06
to
<Daniel...@gmail.com> wrote in message
news:1157590548.2...@d34g2000cwd.googlegroups.com...

You could wrap your script in an HTA as it supports
SINGLEINSTANCE="yes"
Another advantage is that it allows a user interface.

Test as-is. Watch for word-wrap.

<html>
<head>
<title>test.hta</title>
<HTA:APPLICATION ID="HTA"
APPLICATIONNAME="TEST"
SYSMENU="no"
SCROLL="yes"
SINGLEINSTANCE="yes"
>
</head>
<script type="text/vbscript">
Option Explicit
Sub Clicked()
document.getElementById("Now").innerHTML = Now
End Sub
</script>
<body>
<form>
<input type="button" value="Click" onclick="Clicked()">
<input type="button" value="Close" onclick="self.close()">
</form>
<span id="Now"></span>
</body>
</html>

Anthony Jones

unread,
Sep 7, 2006, 4:18:12 AM9/7/06
to

>"Bill James" <wgj...@mvps.org> wrote in message
news:%>239KOhJk...@TK2MSFTNGP02.phx.gbl...

>One old kludge is to use a flag file with a unique name. If the file
exists, exit, otherwise create the >file, do your processing, then delete
the file. The caveat is that is your script crashes the flag file will
>still be there preventing the script from running again.

You could hold the file open exclusively and close at the end of the script.
If the script crashes the exclusive lock on the file will be released.

James Garringer

unread,
Sep 7, 2006, 1:00:28 PM9/7/06
to
Danny,

You could also check for a running script host, and then only run
your sub routine when other scripts have finished. This approach,
however, goes for all running scripts using wscript.exe or cscript.exe
(the function checks for only one or the other...whichever is passed as
a parameter) as its host. If this is the functionality you are looking
for give this code a shot.
The OtherScriptsRunning function accepts two parameters, a
computer name, and a script host name. The function returns TRUE if
there are other scripts running, and FALSE if not. To run your sub
routine only when other scripts are not running call it if the
condition is true.

(Please be careful of line wraps from posting.)

----------------------------

If (OtherScriptsRunning (".","wscript.exe")) = True Then
MyScript
Else
MsgBox "Please wait, other scripts are running"
End If


Function OtherScriptsRunning(strComputer,strScriptHost)
Set objWMIService = GetObject("winmgmts:\\" & strComputer &
"\root\cimv2")
Set objShell = CreateObject("Wscript.Shell")
OtherScriptsRunning = False
On Error Resume Next
Do While True
Set colProcesses = objWMIService.ExecQuery _
("Select * from Win32_Process Where Name = '" & strScriptHost
& "'")
If Err.Number = 0 Then
If colProcesses.Count = 0 Then
OtherScriptsRunning = True
Exit Function
End If
Else
MsgBox "Can't query host"
End If
Loop
End Function


Sub MyScript
MsgBox "No other scripts are running."
End Sub


-----------------------------

Hope this helps,

James Garringer

Daniel...@gmail.com

unread,
Sep 7, 2006, 8:29:32 PM9/7/06
to
Thanks everyone for your answers. I have gone for the flag file
solution because it was very quick to implement. However I do like
McKirahan's HTA solution for the longer term. This is the first time
I've heard of HTA and I think it might revolutionise my
scripting.Thanks.....

Danny....

McKirahan

unread,
Sep 7, 2006, 10:14:12 PM9/7/06
to
<Daniel...@gmail.com> wrote in message
news:1157675372.0...@p79g2000cwp.googlegroups.com...

For more info check out these links:

Introduction to HTML Applications (HTAs)
URL:http://msdn.microsoft.com/workshop/author/hta/overview/htaoverview.asp

HTA Developers Center
http://www.microsoft.com/technet/scriptcenter/hubs/htas.mspx

HTML Applications
http://www.htmlgoodies.com/beyond/reference/article.php/3472841


0 new messages