4

Do I need to use an specific exitcode ?

Win32::Process::Create(
    $ProcessObj,
    "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe",
    "firefox -no-remote -P $prof_name",
    0,
    NORMAL_PRIORITY_CLASS,
".")|| die ErrorReport();
$ProcessObj->kill(0);

That way it kills, but not gently, it is generating problems with the Firefox profile.

  • 1
    If it's windowed app (as opposed to a console app), you can send a quit message to its message queue – ikegami Jun 11 '16 at 01:10

2 Answers2

6

Short   Usual methods other than Window's taskkill are forceful. Thanks to mob and melpomene.


The Win32::Process documentation doesn't say what the methods Kill or KillProcess do, but they seem pretty blunt about it. Windows does provide a gradation in how to terminate a process, see for example this post on graceful termination via winapi, even as it does not have UNIX's signals. However, this is apparently not utilized by the module nor by kill (see end).

The Windows's own taskkill should nicely ask the process to terminate. Query it for flags, by running taskkill /? in a console on your system. Here is documentation for taskkill on MS technet. For example, this terminates the process by its $pid, along with its children

my $pid = $ProcessObj->GetProcessID();
system("taskkill /t /pid $pid");

If the method GetProcessID() doesn't return the real ID, see the link below for other ways.

I can't test on Windows right now. There's a bit more related detail in this post, second part.


Perl's kill is apparently forceful on Windows. From perlport

... kill($sig, $pid) terminates the process identified by $pid, and makes it exit immediately with exit status $sig.

Thanks to melpomene for comment and documentation link.

The exitcode, I believe, only tells to process what to report to the system as its exit.

zdim
  • 64,580
  • 5
  • 52
  • 81
  • taskkill's `/F` flag means "and don't be gentle" – mob Jun 10 '16 at 22:05
  • @mob Ah, thank you! Corrected. I should've known -- F may well stand for 'force'. – zdim Jun 10 '16 at 22:11
  • Windows doesn't have signals. On Windows, [`kill $n, $pid` forces the process `$pid` to exit with status `$n`](http://perldoc.perl.org/perlport.html#kill). – melpomene Jun 10 '16 at 22:57
  • 1
    @melpomene _Forces_ it? Does it not send it the termination signal (Window's equivalent), or perhaps asks the OS to do so, allowing the process to clean up? If `$n` makes it exit with that status, then sending terminate should allow it to actually exit in that way? (Can't find any discussion of that in docs right now.) I haven't touched winapi in a long while, but I think that there are various ways to terminate a process. I'd expect `kill` to honor that? Thank you for commenting. **I see that it's a link only now, reading** – zdim Jun 10 '16 at 23:07
1

As far as i know in Perl you could just do

kill -15 (pid)

Who gently kills a process in perl

Cth103
  • 63
  • 5