Skip to content

Instantly share code, notes, and snippets.

@nathanharper
Last active August 29, 2015 14:02
Show Gist options
  • Save nathanharper/f7c8b9f1e543d11537b6 to your computer and use it in GitHub Desktop.
Save nathanharper/f7c8b9f1e543d11537b6 to your computer and use it in GitHub Desktop.
simple process search/destroy menu
#!/usr/bin/env perl
BEGIN { $Curses::OldCurses = 1; }
use strict;
use warnings;
use Curses;
use perlmenu;
my $ARGC = $#ARGV + 1;
my $killid = 15;
my @all_pids;
my $prefix = "";
if ($ARGC < 1) {
print STDERR "At least one argument required.\n";
exit 1;
}
&menu_init(1,"Select process(es) to kill."); # Init menu
&menu_item("All", "all");
foreach my $i (0 .. $#ARGV) {
if ($ARGV[$i] eq "-9") {
$killid = 9;
}
elsif ($ARGV[$i] eq "-11") {
$prefix = "sudo";
}
else {
my @procs = split(/\n/, `ps aux|grep -v grep|grep -v ikill|grep '$ARGV[$i]'`);
foreach my $j (0 .. $#procs) {
($_,my $pid,$_,$_,$_,$_,$_,$_,$_,$_,my @proc) = split(/\s+/, $procs[$j]);
push @all_pids, $pid;
&menu_item(join(' ', @proc), $pid);
}
}
}
&menu_item("Cancel", "cancel");
my $sel = &menu_display("Which process(es)?"); # Get user selection
if ($sel eq "all") {
$sel = join("' '", @all_pids);
}
elsif ($sel eq "cancel") {
print "Nothing to do.\n";
exit 0;
}
my $cmd = "$prefix kill -$killid '$sel'";
my $retcode = system $cmd;
if ($retcode != 0) {
print "Failed with code: $retcode\n";
}
print "$cmd\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment