-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkillProcByTime.pl
More file actions
78 lines (70 loc) · 1.77 KB
/
killProcByTime.pl
File metadata and controls
78 lines (70 loc) · 1.77 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/usr/bin/perl -w
use POSIX;
if ($#ARGV != 1)
{
Help();
exit(1);
}
unless(@ARGV)
{
Help();
exit(1);
}
($procName, $numOfMinutes) = @ARGV;
unless (isnumber($numOfMinutes))
{
print "error in minutes value\n";
Help();
exit(1);
}
open(PS, "ps -ef|") or die "error running command: $!\n";
while(<PS>)
{
chomp();
next if $_ =~ /$0/;
next if $_ !~ /$procName/;
@fields = split;
if ($fields[4] =~ /[a-zA-Z]/)
{
push(@deathrow, $fields[1]);
}
elsif($fields[4] =~ /\b\d\d/)
{
@time = split(/:/, $fields[4]);
$secondsSinceStart = checkTime(@time);
@currentTime = ((localtime)[2], (localtime)[1], (localtime)[0]);
$currentSeconds = checkTime(@currentTime);
$diff = $currentSeconds - $secondsSinceStart;
if ($diff < 0)
{
$currentSeconds = $currentSeconds + 86400;
$diff = $currentSeconds - $secondsSinceStart;
}
$inMinutes = $diff / 60;
if ($inMinutes > $numOfMinutes)
{
push(@deathrow, $fields[1]);
}
}
}
kill 9, @deathrow;
sub Help
{
print "USAGE: $0 <process_string_to_kill> <num_of_minutes>\n";
}
sub isnumber
{
shift =~ /^-?\d+\.?\d*$/;
}
sub checkTime
{
($hour, $min, $sec) = @_;
$mday = (localtime)[3];
$mon = (localtime)[4];
$year = (localtime)[5];
$wday = (localtime)[6];
$yday = (localtime)[7];
$isdst = (localtime)[8];
$unixtime = mktime ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst);
return $unixtime;
}