summaryrefslogtreecommitdiffstats
path: root/.local/bin/dmkill
blob: 0ff1257c733200f54e476a3a33dac751b0567b82 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env perl

use strict;
use warnings;
use File::HomeDir qw(home);
use constant KILL_PATH => '/bin/kill';
use constant CONFIRM => [
	"no",
	"yes"
	];

sub confirm
{
	my ($pid, $proc) = @_;
	my $list;
	my $choice;

	for (@{+CONFIRM}) {
		$list .= $_ . "\n";
	}
	$choice = `printf "%s" "$list" | dmenu -i -p "kill $proc ($pid)?"`;
	chomp $choice;
	if ($choice eq ${+CONFIRM}[1]) {
		return (1);
	}
	return (0);
}

sub action
{
	my ($var, $user) = @_;
	my $pid;
	my $proc;

	chomp $var;
	if (not $var) {
		return;
	}
	if ($var =~ /^\s*(\d+)\s/) {
		$pid = $1;
	}
	if ($var =~ /^\s*\S+\s+\S+\s+(\S+)/) {
		$proc = $1;
	}
	if (confirm($pid, $proc) == 1) {
		exec(KILL_PATH, '-9', $pid);
	}
	return;
}

sub main
{
	my $choice;
	my $ps;
	my $user;

	$user = getpwuid($<);
	$choice = `ps -U "$user" -o pid,user,comm,time,%cpu,%mem | dmenu -i -l 100`;
	action($choice, $user);
	return (0);
}

main();

__END__