Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Kill a perl script execution in windows

by rbala (Acolyte)
on Feb 18, 2013 at 08:58 UTC ( [id://1019301]=perlquestion: print w/replies, xml ) Need Help??

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!
  • Comment on Kill a perl script execution in windows

Replies are listed 'Best First'.
Re: Kill a perl script execution in windows
by BrowserUk (Patriarch) on Feb 18, 2013 at 09:53 UTC

    Try

    #! 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.
      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.
        Can you please explain how $target variable is evaluated and on what the map function is applied ?
        • Type the command: wmic process list full /format:csv on a windows command line to see that it produces a csv of information about the running processes.
        • Wrapping that in backticks (`...`) cause perl to run that command and capture the output.
        • It supplies that output one line at a time to the map: map[ (split ',', $_ )[1,24] ] `...`; which:
          1. splits it on the commas,
          2. extracts the second and 25th fields, which are the command line text and the process id respectively.
          3. And places those two fields from each line into its own anonymous array.
        • Those anonymous arrays are passed into the second map (appearing lexically first in the source code):
          ... = map { 1+ index( $_->[0] // '', $0 ) && $_->[1] != $$ ? $_->[1] : () } ...

          Which tests if the first value in the anonymous array ($_->[0]) contains $0 (the name&path of the current program), and if the second value in the anonymous array ($_->[1]) (the process id) is not equal to $$ (the process id of the current perl program),

          then it passes that second value (the pid) out of the map

          otherwise it returns nothing.

        • Hence, if another copy of this script is already running, $target will be assigned its pid.
        I am getting syntax error

        What is the text of the syntax error? What version of perl are you running?


        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.
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).

      That's good for preventing a second copy from starting; but not so useful for killing the previous version if it is running because it doesn't give you the pid.


      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.
        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;
Re: Kill a perl script execution in windows
by Anonymous Monk on Feb 18, 2013 at 09:47 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1019301]
Approved by Ratazong
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (10)
As of 2024-04-19 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found