rbala has asked for the wisdom of the Perl Monks concerning the following question:
Hi All,
I need to write a perl script such that when the script starts, it should check for another instance of the script and if any previous instance of the same script detected, it should stop the previous instance and continue with the current instance .
I have done this in linux systems, using Proc::ProcessTable module.Does anyone know how to achieve this in windows ?
Thanks in Advance!
Re: Kill a perl script execution in windows
by BrowserUk (Patriarch) on Feb 18, 2013 at 09:53 UTC
|
#! perl -slw
use strict;
print "$0 running as $$";
my( $target ) = map {
1+ index( $_->[0] // '', $0 ) && $_->[1] != $$
? $_->[1]
: ()
} map[
(split ',', $_ )[1,24]
], `wmic process list full /format:csv`;
if( $target ) {
print "Found another running as $target; usurping it.";
kill 9, $target;
}
sleep 1e6;
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
|
Hi,
Thanks for your code. In a high level , I am able to understand the script. Can you please explain how $target variable is evaluated and on what the map function is applied ?
I am getting syntax error when i use this code as such .
Thanks,
Bala.
| [reply] |
|
| [reply] [d/l] [select] |
|
|
|
Re: Kill a perl script execution in windows
by nikosv (Deacon) on Feb 18, 2013 at 12:32 UTC
|
You can use named events. They are kernel events so they persist across processes.
You create the event in the first script and when the latter script loads you check for the event's existance. If it already exists then you know that there is another instance already loaded.
Check Win32::Event
$event = Win32::Event->new([$manual, [$initial, [$name]]])
Constructor for a new event object. If $manual is true, you must manua
+lly reset the event after it is signalled (the default is false). If
+$initial is true, the initial state of the object is signalled (defau
+lt false). If $name is omitted or undef, creates an unnamed event obj
+ect.
If $name signifies an existing event object, then $manual and $initial
+ are ignored and the object is opened. If this happens, $^E will be s
+et to 183 (ERROR_ALREADY_EXISTS).
| [reply] [d/l] |
|
| [reply] |
|
you don't need the PID. The first script waits for a signal to terminate as:
use Win32::Event;
use 5.010;
$event = Win32::Event->new(1,0,"myevent");
while ($event->wait(0)==0) {
say $i;
print $i++;
}
say "exiting";
or just $event->wait(), depends on the situation.Then the second one can signal it to abort :
use Win32::Event;
use 5.010;
$event = Win32::Event->new(1,0,"myevent");
say $^E;
$event->set;
| [reply] [d/l] [select] |
Re: Kill a perl script execution in windows
by Anonymous Monk on Feb 18, 2013 at 09:47 UTC
|
[metacpan://single instance ], [metacpan://pidfile], [metacpan://mutex] , [single instance ], [pidfile], [mutex] single instance, pidfile, mutex , single instance , pidfile, mutex | [reply] [d/l] |
|
|