你如何杀死所有超过特定年龄的 Linux 进程?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you kill all Linux processes that are older than a certain age?
提问by yukondude
I have a problem with some zombie-like processes on a certain server that need to be killed every now and then. How can I best identify the ones that have run for longer than an hour or so?
我对某个服务器上的一些类似僵尸的进程有问题,需要时不时地将其杀死。我如何才能最好地识别运行时间超过一个小时左右的那些?
采纳答案by Jodie C
If they just need to be killed:
如果他们只是需要被杀死:
if [[ "$(uname)" = "Linux" ]];then killall --older-than 1h someprocessname;fi
If you want to see what it's matching
如果你想看看它匹配的是什么
if [[ "$(uname)" = "Linux" ]];then killall -i --older-than 1h someprocessname;fi
The -i
flag will prompt you with yes/no for each process match.
-i
对于每个进程匹配,该标志将提示您是/否。
回答by abyx
Using ps is the right way. I've already done something similar before but don't have the source handy. Generally - ps has an option to tell it which fields to show and by which to sort. You can sort the output by running time, grep the process you want and then kill it.
使用 ps 是正确的方法。我之前已经做过类似的事情,但手边没有源代码。通常 - ps 有一个选项来告诉它显示哪些字段以及按哪些字段排序。您可以按运行时间对输出进行排序,grep 所需的进程,然后将其终止。
HTH
HTH
回答by Mark Harrison
For anything older than one day,
对于超过一天的任何事情,
ps aux
will give you the answer, but it drops down to day-precision which might not be as useful.
会给你答案,但它会降到日精度,这可能没有那么有用。
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 7200 308 ? Ss Jun22 0:02 init [5]
root 2 0.0 0.0 0 0 ? S Jun22 0:02 [migration/0]
root 3 0.0 0.0 0 0 ? SN Jun22 0:18 [ksoftirqd/0]
root 4 0.0 0.0 0 0 ? S Jun22 0:00 [watchdog/0]
If you're on linux or another system with the /proc filesystem, In this example, you can only see that process 1 has been running since June 22, but no indication of the time it was started.
如果您使用的是 linux 或其他带有 /proc 文件系统的系统,在本例中,您只能看到进程 1 自 6 月 22 日以来一直在运行,但没有指示它启动的时间。
stat /proc/<pid>
will give you a more precise answer. For example, here's an exact timestamp for process 1, which ps shows only as Jun22:
会给你更准确的答案。例如,这是进程 1 的确切时间戳,ps 仅显示为 Jun22:
ohm ~$ stat /proc/1
File: `/proc/1'
Size: 0 Blocks: 0 IO Block: 4096 directory
Device: 3h/3d Inode: 65538 Links: 5
Access: (0555/dr-xr-xr-x) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2008-06-22 15:37:44.347627750 -0700
Modify: 2008-06-22 15:37:44.347627750 -0700
Change: 2008-06-22 15:37:44.347627750 -0700
回答by yukondude
Found an answer that works for me:
找到了一个对我有用的答案:
warning: this will find and killlong running processes
警告:这将找到并杀死长时间运行的进程
ps -eo uid,pid,etime | egrep '^ *user-id' | egrep ' ([0-9]+-)?([0-9]{2}:?){3}' | awk '{print }' | xargs -I{} kill {}
(Where user-idis a specific user's ID with long-running processes.)
(其中user-id是具有长时间运行进程的特定用户 ID。)
The second regular expression matches the a time that has an optional days figure, followed by an hour, minute, and second component, and so is at least one hour in length.
第二个正则表达式匹配具有可选天数的时间,后跟小时、分钟和秒组件,因此长度至少为一小时。
回答by ggasp
In this way you can obtain the list of the ten oldest processes:
通过这种方式,您可以获得最旧的十个进程的列表:
ps -elf | sort -r -k12 | head -n 10
回答by Maniraj Patri
do a ps -aef
. this will show you the time at which the process started. Then using the date
command find the current time. Calculate the difference between the two to find the age of the process.
做一个ps -aef
。这将显示进程开始的时间。然后使用date
命令查找当前时间。计算两者之间的差异以找到过程的年龄。
回答by Peter V. M?rch
Perl's Proc::ProcessTable will do the trick: http://search.cpan.org/dist/Proc-ProcessTable/
Perl 的 Proc::ProcessTable 可以解决这个问题:http: //search.cpan.org/dist/Proc-ProcessTable/
You can install it in debian or ubuntu with sudo apt-get install libproc-processtable-perl
您可以在 debian 或 ubuntu 中安装它 sudo apt-get install libproc-processtable-perl
Here is a one-liner:
这是一个单行:
perl -MProc::ProcessTable -Mstrict -w -e 'my $anHourAgo = time-60*60; my $t = new Proc::ProcessTable;foreach my $p ( @{$t->table} ) { if ($p->start() < $anHourAgo) { print $p->pid, "\n" } }'
Or, more formatted, put this in a file called process.pl:
或者,更格式化,将其放在名为 process.pl 的文件中:
#!/usr/bin/perl -w
use strict;
use Proc::ProcessTable;
my $anHourAgo = time-60*60;
my $t = new Proc::ProcessTable;
foreach my $p ( @{$t->table} ) {
if ($p->start() < $anHourAgo) {
print $p->pid, "\n";
}
}
then run perl process.pl
然后运行 perl process.pl
This gives you more versatility and 1-second-resolution on start time.
这为您提供了更多的多功能性和开始时间的 1 秒分辨率。
回答by Rodney Amato
I did something similar to the accepted answer but slightly differently since I want to match based on process name and based on the bad process running for more than 100 seconds
我做了一些类似于接受的答案但略有不同的事情,因为我想根据进程名称和运行超过 100 秒的坏进程进行匹配
kill $(ps -o pid,bsdtime -p $(pgrep bad_process) | awk '{ if ($RN > 1 && > 100) { print ; }}')
回答by mob
stat -t /proc/<pid> | awk '{print $14}'
stat -t /proc/<pid> | awk '{print $14}'
to get the start time of the process in seconds since the epoch. Compare with current time (date +%s
) to get the current age of the process.
以秒为单位获取进程的开始时间。与当前时间 ( date +%s
) 进行比较以获取进程的当前年龄。
回答by David Jeske
In case anyone needs this in C, you can use readproc.h and libproc:
如果有人在 C 中需要这个,你可以使用 readproc.h 和 libproc:
#include <proc/readproc.h>
#include <proc/sysinfo.h>
float
pid_age(pid_t pid)
{
proc_t proc_info;
int seconds_since_boot = uptime(0,0);
if (!get_proc_stats(pid, &proc_info)) {
return 0.0;
}
// readproc.h comment lies about what proc_t.start_time is. It's
// actually expressed in Hertz ticks since boot
int seconds_since_1970 = time(NULL);
int time_of_boot = seconds_since_1970 - seconds_since_boot;
long t = seconds_since_boot - (unsigned long)(proc_info.start_time / Hertz);
int delta = t;
float days = ((float) delta / (float)(60*60*24));
return days;
}