Recuperar el uso de CPU y el uso de memoria de un solo proceso en Linux?


Quiero obtener el uso de CPU y memoria de un solo proceso en Linux - conozco el PID. Con suerte, puedo obtenerlo cada segundo y escribirlo en un CSV usando el comando' watch'. ¿Qué comando puedo usar para obtener esta información de la línea de comandos de Linux?

Author: jww, 2009-08-03

15 answers

ps -p <pid> -o %cpu,%mem,cmd

(Puede omitir "cmd", pero eso podría ser útil en la depuración).

Tenga en cuenta que esto da el uso promedio de CPU del proceso durante el tiempo que se ha estado ejecutando.

 177
Author: caf,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-14 01:10:43

Una variante de la respuesta de caf: top -p <pid>

Esto actualiza automáticamente el uso de la CPU, por lo que es bueno para el monitoreo.

 54
Author: Manki,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:26:24

Puede obtener los resultados por el nombre del proceso usando

ps -C chrome -o %cpu,%mem,cmd

La opción -C le permite usar nombre de proceso sin saber que es pid.

 31
Author: amit,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-21 13:08:45

Use pidstat (desde sysstat - Refiérase a Link).

Por ejemplo, para monitorear estos dos ID de proceso (12345 y 11223) cada 5 segundos, use

$ pidstat -h -r -u -v -p 12345,11223 5
 16
Author: Neon,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-03-24 12:35:53

Inicie un programa y monitoréelo

Este formulario es útil si desea comparar fácilmente un ejecutable:

topp() (
  $* &>/dev/null &
  pid="$!"
  trap ':' INT
  echo 'CPU  MEM'
  while sleep 1; do ps --no-headers -o '%cpu,%mem' -p "$pid"; done
  kill "$pid"
)
topp ./myprog arg1 arg2

Ahora cuando presiona Ctrl + C sale del programa y deja de monitorear. Salida de muestra:

CPU  MEM
20.0  1.3
35.0  1.3
40.0  1.3

Probado en Ubuntu 16.04.

 6
Author: Ciro Santilli 新疆改造中心 六四事件 法轮功,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-11-13 16:28:43

Puede usar top -b y grep el pid que desee (con la bandera -b top se ejecuta en modo por lotes), o también usar la bandera -p y especificar el pid sin usar grep.

 4
Author: Alberto Zaccagni,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-19 15:50:44
ps aux | awk '{print $4"\t"$11}' | sort | uniq -c | awk '{print $2" "$1" "$3}' | sort -nr

O por proceso

ps aux | awk '{print $4"\t"$11}' | sort | uniq -c | awk '{print $2" "$1" "$3}' | sort -nr |grep mysql
 4
Author: Patel95,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-04-26 19:55:00

Como se comentó en la respuesta de caf anterior, ps y en algunos casos pidstat le darán el promedio de vida de la pCPU. Para obtener resultados más precisos, utilice top. Si necesita ejecutar top una vez que se puede ejecutar:

top -b -n 1 -p <PID>

O solo para datos y encabezado del proceso:

top -b -n 1 -p <PID> | tail -3 | head -2

Sin encabezados:

top -b -n 1 -p <PID> | tail -2 | head -1
 4
Author: aviranh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:34:41
ps aux|awk  '{print $2,$3,$4}'|grep PID

Donde la primera columna es el PID,segunda columna Uso de CPU ,tercera columna uso de memoria.

 2
Author: Osama Al-Banna,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-07-21 13:19:30

Para obtener el uso de memoria de su aplicación (a diferencia de las bibliotecas compartidas que utiliza, necesita usar la interfaz smaps de Linux). Esta respuesta lo explica bien.

 1
Author: Paul Biggar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-05-23 12:18:16
ps axo pid,etime,%cpu,%mem,cmd | grep 'processname' | grep -v grep

PID-ID del proceso

Etime-Ejecución del Proceso/Duración en vivo

% cpu-Uso de CPU

%mem-Uso de memoria

Cmd-Command

Reemplace processname con cualquier proceso que desee rastrear, mysql nginx php-fpm, etc...

 1
Author: Siva KR,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-04-21 08:48:04

Todas las respuestas aquí muestran solo el porcentaje de memoria para el PID.

Aquí hay un ejemplo de cómo obtener el uso de memoria rss en KB para todos los procesos de apache, reemplace "grep apache" por "grep PID" si solo desea ver un PID específico:

watch -n5 "ps aux -y | grep apache | awk '{print \$2,\$6}'"

Esto imprime:

Every 5.0s: ps aux -y | grep apache | awk '{print $2,$6}'                                                                                                                                                                                                          
Thu Jan 25 15:44:13 2018

12588 9328
12589 8700
12590 9392
12591 9340
12592 8700
12811 15200
15453 9340
15693 3800
15694 2352
15695 1352
15697 948
22896 9360

Con CPU%:

watch -n5 "ps aux -y | grep apache | awk '{print \$2,\$3,\$6}'"

Salida:

Every 5.0s: ps aux -y | grep apache | awk '{print $2,$3,$6}'                                                                                                                                                                                                       
Thu Jan 25 15:46:00 2018

12588 0.0 9328
12589 0.0 8700
12590 0.0 9392
12591 0.0 9340
12592 0.0 8700
12811 0.0 15200
15453 0.0 9340
15778 0.0 3800
15779 0.0 2352
15780 0.0 1348
15782 0.0 948
22896 0.0 9360
 1
Author: Banana,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-01-25 20:56:42

(Si estás en macOS 10.10, prueba la opción-c acumulativa de top:

top -c a -pid PID

(Esta opción no está disponible en otros linux, probado con Scientific Linux el6 y RHEL6)

 0
Author: Kieleth,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-02-28 17:27:53

Arriba enumere el proceso superior de consumo de cpu y memoria

        ps axo %cpu,%mem,command | sort -nr | head
 0
Author: user8854776,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-11-14 02:51:40

Uso de CPU y memoria de un solo proceso en Linux o puede obtener los 10 principales procesos de cpu utilizados utilizando el comando siguiente

Ps-aux sort sort-pcpu / head-n 11

 -1
Author: jyothish reddy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-07-03 06:14:16