Ωmega
Ωmega

Reputation: 43703

How to list and kill threads/processes in Perl without using external command(s)?

I need to kill some processes and threads (if exist) in Perl application, but I don't want to use external command(s) such as ps, grep, awk, cut, uniq or kill.

My current code is:

      my $appName = $0;
      $appName =~ s/^.*\/([^\/]*)$/$1/;
      $_ = qx(kill -9 `ps -eLao pid,command | grep '$appName\[ 0-9\]*\$' |
              grep -v grep | awk '\$1 != $$' | cut -d' ' -f1 | uniq` 2>&1);

I am using VPS, so my memory is limited. The code above sometimes returns undef, as system cannot allocate memory for call of external command(s). I am looking for alternative solution without using external command(s).

Upvotes: 2

Views: 619

Answers (1)

Dan
Dan

Reputation: 10786

Use internal kill commands like kill and the /proc fs or some modules like Win32::Process::List or anything in the Proc:: namespace.

Upvotes: 5

Related Questions