First, obvious from my text: I asked for a portable way.
All right, create a sub called "KillProc()" and then put an if statement in that sub. Execute block A if it's Linux, or execute block B if it's Windows. Portable. Done!
A command which only works on Windows doesn't help.
Why? Can't you detect the current running OS from your Perl script and put an if statement into your program? Is this against your religion? WHATEVER solution you are looking for, if you look deep down into it, somewhere at the deep inside a module, you'll find an if statement that executes two different set of codes for Linux and Windows, because the two don't work the same!
It's not hard. I have done this many times. For example:
sub CLS
{
$^O =~ m/MSWIN/i and return system('CLS');
$^O =~ m/DOS/i and return system('COMMAND.COM /C CLS');
$^O =~ m/LINUX/i and return print "\x1Bc\x1B[0m\x1B[3J\x1B[H\x1B[2J"
+;
print (($^O =~ m/DARWIN/i) ? "\x1B[3J" : ' ' x 4000);
}
So, you want to kill one specific task...
C:\>TASKLIST
Image Name PID Session Name Session# Mem Usag
+e
========================= ====== ================ ======== ===========
+=
System Idle Process 0 0 28
+K
System 4 0 236
+K
smss.exe 456 0 384
+K
csrss.exe 508 0 3,320
+K
winlogon.exe 548 0 1,348
+K
SERVICES.EXE 592 0 3,752
+K
LSASS.EXE 604 0 1,312
+K
SVCHOST.EXE 776 0 3,448
+K
SVCHOST.EXE 820 0 3,848
+K
SVCHOST.EXE 888 0 14,340
+K
SVCHOST.EXE 932 0 4,560
+K
SPOOLSV.EXE 1032 0 4,132
+K
SVCHOST.EXE 1096 0 3,276
+K
EXPLORER.EXE 1396 0 27,320
+K
SVCHOST.EXE 1548 0 3,796
+K
mypal.exe 696 0 194,636
+K
Notepad2.exe 1540 0 5,984
+K
cmd.exe 1588 0 2,332
+K
wmiprvse.exe 1840 0 5,692
+K
tasklist.exe 1152 0 4,100
+K
C:\>TASKKILL /F /PID 1540
SUCCESS: The process with PID 1540 has been terminated.
C:\>
It's that simple!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.