Re: Killing subprocess in windows
by Corion (Patriarch) 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, ...). | [reply] [d/l] [select] |
|
|
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 :)
| [reply] [d/l] [select] |
Re: Killing subprocess in windows
by Discipulus (Canon) 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.
| [reply] [d/l] |
|
|
> 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! :)
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
|
|
|
|
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.
| [reply] [d/l] |
|
|
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. | [reply] [d/l] [select] |
|
|
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
| [reply] |
Re: Killing subprocess in windows
by bliako (Abbot) 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
| [reply] [d/l] |
|
|
> 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.
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
|
|
Re: Killing subprocess in windows
by Anonymous Monk on May 21, 2021 at 10:20 UTC
|
| [reply] |
|
|
| [reply] [d/l] [select] |