특정 연령보다 오래된 모든 Linux 프로세스를 어떻게 종료합니까?
가끔씩 죽여야하는 특정 서버의 좀비와 같은 프로세스에 문제가 있습니다. 한 시간 이상 실행 된 파일을 가장 잘 식별 할 수있는 방법은 무엇입니까?
그냥 죽여야한다면 :
if [[ "$(uname)" = "Linux" ]];then killall --older-than 1h someprocessname;fi
일치하는 것을보고 싶다면
if [[ "$(uname)" = "Linux" ]];then killall -i --older-than 1h someprocessname;fi
-i
플래그는 각 프로세스 경기 / 예와 메시지를 표시하지 않습니다.
저에게 맞는 답변을 찾았습니다.
경고 : 이것은 오래 실행되는 프로세스를 찾아서 죽 입니다.
ps -eo uid,pid,etime | egrep '^ *user-id' | egrep ' ([0-9]+-)?([0-9]{2}:?){3}' | awk '{print $2}' | xargs -I{} kill {}
(여기서 user-id 는 장기 실행 프로세스가있는 특정 사용자의 ID입니다.)
두 번째 정규식은 선택적인 요일 숫자가 뒤에 1 시간, 분, 두 번째 구성 요소가있는 시간과 일치하므로 길이가 1 시간 이상입니다.
하루 이상 된 경우
ps aux
답을 줄 것이지만 유용하지 않을 수있는 일 정밀도로 떨어집니다.
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]
Linux 또는 / proc 파일 시스템이있는 다른 시스템을 사용하는 경우이 예에서는 프로세스 1이 6 월 22 일 이후로 실행 중임을 확인할 수 있지만 시작된 시간은 표시되지 않습니다.
stat /proc/<pid>
더 정확한 답변을 드릴 것입니다. 예를 들어 다음은 프로세스 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
이렇게하면 가장 오래된 10 개의 프로세스 목록을 얻을 수 있습니다.
ps -elf | 정렬 -r -k12 | 머리 -n 10
Jodie C와 다른 사람들은 killall -i
사용할 수 있다고 지적했습니다 . 프로세스 이름을 사용하여 죽이려면 괜찮습니다. 그러나와 동일한 매개 변수로 죽이고 싶다면 pgrep -f
순수 bash와 /proc
파일 시스템을 사용하여 다음과 같은 것을 사용해야합니다 .
#!/bin/sh
max_age=120 # (seconds)
naughty="$(pgrep -f offlineimap)"
if [[ -n "$naughty" ]]; then # naughty is running
age_in_seconds=$(echo "$(date +%s) - $(stat -c %X /proc/$naughty)" | bc)
if [[ "$age_in_seconds" -ge "$max_age" ]]; then # naughty is too old!
kill -s 9 "$naughty"
fi
fi
이렇게하면 전체 프로세스 이름을max_age
사용하여 초 보다 오래된 프로세스를 찾아서 종료 할 수 있습니다 . 즉, 명명 된 프로세스는 "offlineimap"을 참조하여 종료 할 수 있지만 여기에 제시된 솔루션은 "python2"문자열에서만 작동합니다./usr/bin/python2 offlineimap
killall
Perl의 Proc :: ProcessTable이 트릭을 수행합니다 : http://search.cpan.org/dist/Proc-ProcessTable/
데비안이나 우분투에 설치할 수 있습니다. sudo apt-get install libproc-processtable-perl
다음은 한 줄짜리입니다.
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" } }'
또는 좀 더 형식화하여 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";
}
}
그런 다음 실행 perl process.pl
이것은 시작 시간에 더 많은 융통성과 1 초 해상도를 제공합니다.
을 사용 bc
하여 mob의 답변에서 두 명령을 결합하고 프로세스가 시작된 후 경과 된 시간 (초)을 얻을 수 있습니다.
echo `date +%s` - `stat -t /proc/<pid> | awk '{print $14}'` | bc
편집하다:
Out of boredom while waiting for long processes to run, this is what came out after a few minutes fiddling:
#file: sincetime
#!/bin/bash
init=`stat -t /proc/$1 | awk '{print $14}'`
curr=`date +%s`
seconds=`echo $curr - $init| bc`
name=`cat /proc/$1/cmdline`
echo $name $seconds
If you put this on your path and call it like this: sincetime
it will print the process cmdline and seconds since started. You can also put this in your path:
#file: greptime
#!/bin/bash
pidlist=`ps ax | grep -i -E $1 | grep -v grep | awk '{print $1}' | grep -v PID | xargs echo`
for pid in $pidlist; do
sincetime $pid
done
And than if you run:
greptime <pattern>
where patterns is a string or extended regular expression, it will print out all processes matching this pattern and the seconds since they started. :)
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.
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
kill $(ps -o pid,bsdtime -p $(pgrep bad_process) | awk '{ if ($RN > 1 && $2 > 100) { print $1; }}')
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.
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.
HTH
In case anyone needs this in C, you can use readproc.h and 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;
}
Came across somewhere..thought it is simple and useful
You can use the command in crontab directly ,
* * * * * ps -lf | grep "user" | perl -ane '($h,$m,$s) = split /:/,$F
+[13]; kill 9, $F[3] if ($h > 1);'
or, we can write it as shell script ,
#!/bin/sh
# longprockill.sh
ps -lf | grep "user" | perl -ane '($h,$m,$s) = split /:/,$F[13]; kill
+ 9, $F[3] if ($h > 1);'
And call it crontab like so,
* * * * * longprockill.sh
My version of sincetime
above by @Rafael S. Calsaverini :
#!/bin/bash
ps --no-headers -o etimes,args "$1"
This reverses the output fields: elapsed time first, full command including arguments second. This is preferred because the full command may contain spaces.
'Development Tip' 카테고리의 다른 글
Node.js-최대 호출 스택 크기 초과 (0) | 2020.11.14 |
---|---|
GCC가 "x && (x & 4242)"의 논리 비트 AND 쌍을 "x & 4242"로 최적화 할 수없는 이유는 무엇입니까? (0) | 2020.11.14 |
Java Swing GUI에 대한 자동화 된 테스트 (0) | 2020.11.14 |
if 문에서 조건 평가 순서에 의존하는 것이 안전합니까? (0) | 2020.11.14 |
C에서 순환 버퍼를 어떻게 구현합니까? (0) | 2020.11.14 |