Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Killing subprocess in windows

by LanX (Sage)
on May 20, 2021 at 15:45 UTC ( #11132781=perlquestion: print w/replies, xml ) Need Help??

LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I'm using this to start a tail -f like window on Win to visualize logged output of a background process

sub start_ps_tail { my ( $logfile ) = @_ ; print system <<"__CMD__"; start "MONITORING $logfile" cmd /c powershell gc -tail 10 -wait $l +ogfile __CMD__ }

problem is I need to be able to kill the sub-process again when the logfile stopped being open for writing (i.e. isn't locked anymore)

I though about sending a 'kill', but Perl doesn't seem to give me the PID.

My workaround for the time being is to rename the logfile, b/c powerhell will terminate the tail then, but my boss doesn't want that (though I could rename it back)

Any better ideas, there must be a way to kill a sub-process...

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re: Killing subprocess in windows
by Corion (Pope) on May 20, 2021 at 18:34 UTC

    system does not return the PID when used in your way, but you can simulate start by using system(1, ...):

    my $pid = system(1, "cmd /c powershell gc -tail 10 -wait $logfile" );

    That way, you can kill it. See perlport on the form system(1, ...).

      Hi

      > but you can simulate start by using system(1, ...):

      hm ... not really, start was also necessary to launch a new window in parallel

      > See perlport on the form system(1, ...).

      yeah I missed that, great.

      > That way, you can kill it.

      yes it works, but with a negative signal like with kill("-KILL",$pid)

      That's because I have to target the child process(es) of start/cmd ...

      Thanks :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re: Killing subprocess in windows
by Discipulus (Abbot) on May 20, 2021 at 16:59 UTC
    Hello LanX,

    instead of your unusual syntax, you can try to fork (anyway system does fork under the hood) so that you'll have back the PID?

    You can also use Win32::Process to create and kill the subprocess.

    As you just need to tail a file you can try other simpler ways: is File::Tail usable on Windows ? tail and grep for Windows

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
      > instead of your unusual syntax, you can try to fork

      could you please show me how? From the docs:

      fork() creates a new process by duplicating the calling process.

      I don't wanna replicate the Perl script but spawn a PS window.

      > tail and grep for Windows

      does this offer the -f option? I don't think so.

      > is File::Tail usable on Windows ?

      File::Tail seems overkill, and like the title suggests there might be problems on Win

      get-content -wait is a built-in in all modern Win versions

      > You can also use Win32::Process to create and kill the subprocess.

      That's a very good idea, thanks! :)

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        Hello LanX,

        > (about fork) I don't wanna replicate the Perl script but spawn a PS window.

        You are already forking!

        From system

        > Does exactly the same thing as exec, except that a fork is done first and the parent process waits for the child process to exit.

        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: Killing subprocess in windows
by Marshall (Canon) on May 20, 2021 at 18:35 UTC
    I don't know if this is helpful or not?
    I ran:
    use strict; use warnings; open my $fh, '>'. "testingwrite" or die; my $count = 20; while ($count--) { print $fh "count = $count\n"; sleep (5); }
    in one Win10 command window.

    Then I ran ">handle testingwrite" in another command window:

    C:\Users\xxx\Documents\PerlProjects>handle testingwrite Nthandle v4.22 - Handle viewer Copyright (C) 1997-2019 Mark Russinovich Sysinternals - www.sysinternals.com perl.exe pid: 2488 type: File E8: C:\Users\xxx\Documents\PerlPro +jects\testingwrite C:\Users\xxx\Documents\PerlProjects>
    This will give you the pid of processes that have an open file handle to "testingwrite".

    handle is freeware from Microsoft as part of sysinternals.

      That's helpful ... :)

      ... but not for this problem.

      The PS scripts are reading from not writing to the file, and there might be more than one reader

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re: Killing subprocess in windows
by bliako (Monsignor) on May 20, 2021 at 21:05 UTC
    problem is I need to be able to kill the sub-process again when the logfile stopped being open for writing

    what's the problem? that you need to kill a process initiated with system? or that you want to know "when the logfile stopped being open for writing"? or both?

    -tail 10 -wait $logfile

    surely one can implement this in Perl and avoid getting into that swamp made by M$ regurgitating a proper-OS manual, diagonally read, anchored on !dollars! (that's a '$' for me and you).

    bw, bliako

      > what's the problem?

      getting the PID for killing. The file is locked as long as it is open, hence I can check that it was closed.

      > surely one can implement

      I thought about it, but getting the PID is always helpful.

      And I didn't want to fiddle much with stat and seek to read the newest portion of the file plus the idiosyncrasies of Win OS when applying a Unix-born language.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        I will admit that I am confused. I just don't understand the basic problem that you have. Could you write a few sentences that describe that? BTW..ich kann deutsch.

        I am just looking for some words that will help me understand what the problem is.

Re: Killing subprocess in windows
by Anonymous Monk on May 21, 2021 at 10:20 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (1)
As of 2021-09-26 22:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?