如何终止匹配超过 30 分钟的 grep 的进程?

2022-01-06 00:00:00 linux kill php unix grep

我有一组并行运行的进程.

I have a set of processes that run in parallel.

偶尔有些人会比脚本允许的时间更长:

Occasionally some hang around longer than the script is supposed to allow:

$time_start = microtime(true)
$max_run_time = 30 * 60; // 30 minutes, measured in seconds
while ((microtime(true)-$time_start) < $max_run_time) {
    // do stuff, but quit after 30 minutes
}

我想终止该脚本中运行时间超过 30 分钟的旧进程.

I'd like to kill old processes of this script that have been around longer than 30 minutes.

如何从命令行执行尽可能多的操作?

How can I do as much from the command line?

以下是当前正在运行的进程示例:

Here's a sample of current running processes:

> ps aux | grep script.php
my_user       856 28.3  0.7 546152 234568 ?       S    11:46  88:42 php /path/to/my/script.php
my_user      4931 28.2  1.2 708176 396476 ?       R    07:03 168:06 php /path/to/my/script.php
my_user      6965 28.6  0.7 542132 230764 ?       S    11:51  88:08 php /path/to/my/script.php
my_user      8411 28.3  0.6 536944 225392 ?       S    11:47  88:27 php /path/to/my/script.php
my_user      9087 28.3  1.5 820720 509208 ?       S    06:56 171:12 php /path/to/my/script.php
my_user      9095 27.3  1.1 693496 381992 ?       S    06:56 164:59 php /path/to/my/script.php
my_user     11182 28.1  1.1 704948 393028 ?       R    06:57 169:14 php /path/to/my/script.php
my_user     12490 27.9  0.7 553700 242444 ?       S    11:35  90:32 php /path/to/my/script.php
my_user     14050 28.0  1.1 686380 374972 ?       S    06:58 168:42 php /path/to/my/script.php
my_user     14051 27.9  1.5 828872 517364 ?       S    06:58 168:14 php /path/to/my/script.php
my_user     14888 27.9  0.6 540208 228744 ?       R    11:52  85:55 php /path/to/my/script.php
my_user     16412 27.0  0.9 612436 300828 ?       S    11:48  84:01 php /path/to/my/script.php
my_user     17776 28.2  1.1 693748 382016 ?       S    06:59 169:38 php /path/to/my/script.php
my_user     21159 28.2  1.5 825944 514560 ?       R    07:00 169:02 php /path/to/my/script.php
my_user     21215 28.5  1.2 708172 396724 ?       S    07:00 171:01 php /path/to/my/script.php
my_user     23615 27.3  0.9 632712 321224 ?       S    11:24  91:36 php /path/to/my/script.php
my_user     26225 27.8  1.1 696316 384788 ?       S    07:01 166:42 php /path/to/my/script.php
my_user     31541 28.6  1.5 829628 518124 ?       S    07:02 170:46 php /path/to/my/script.php
my_user     31542 28.4  1.1 704476 393152 ?       S    07:02 170:02 php /path/to/my/script.php

我可以使用 `pkill -f "php/path/to/my/script.php" 将它们全部杀死

I can kill them all using `pkill -f "php /path/to/my/script.php"

但是我怎么能杀死旧的呢?

But how can I just kill the old ones?

推荐答案

要获取运行时间超过 30 分钟的名为 PROCESS_NAME 的进程列表可以发出以下awk命令:

To obtain the list of processes named PROCESS_NAME which are running longer than 30 minutes you can issue the following awkcommand:

ps ax -ocmd,pid,etime | awk '/PROCESS_NAME/{split($(NF),a,":");if(a[1]>30)print}'

只获取运行的pid

ps ax -ocmd,pid,etime | awk '/PROCESS_NAME/{split($(NF),a,":");if(a[1]>30)print $(NF-1)}'

您可以将其通过管道传递给 xargs kill,如下所示:

You can pipe this to xargs kill, like this:

ps ax -ocmd,pid,etime | awk '/PROCESS_NAME/{split($(NF),a,":");if(a[1]>30)print $(NF-1)}' 
| xargs kill 

相关文章