ps

O comando “ps” é sem dúvida o comando mais utilizado, e podemos através dele explorar muitas informações em execução. Segue abaixo alguns exemplos:

Todos os processos usando a syntax BSD.

Resultados com o PID (Process ID)

$ ps ax

PID TTY      STAT   TIME COMMAND
   1 ?        Ss     0:01 /sbin/init splash
   2 ?        S      0:00 [kthreadd]
   3 ?        S      0:00 [pool_workqueue_release]
   4 ?        I<     0:00 [kworker/R-rcu_g]
   5 ?        I<     0:00 [kworker/R-rcu_p]
   6 ?        I<     0:00 [kworker/R-slub_]
   7 ?        I<     0:00 [kworker/R-netns]
   9 ?        I      0:00 [kworker/0:1-events]
  10 ?        I<     0:00 [kworker/0:0H-events_highpri]

Resultados com o PID e USER

$ ps axu

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root           1  0.0  0.0 166708 11380 ?        Ss   22:04   0:01 /sbin/init splash
root           2  0.0  0.0      0     0 ?        S    22:04   0:00 [kthreadd]
root           3  0.0  0.0      0     0 ?        S    22:04   0:00 [pool_workqueue_release]
root           4  0.0  0.0      0     0 ?        I<   22:04   0:00 [kworker/R-rcu_g]
root           5  0.0  0.0      0     0 ?        I<   22:04   0:00 [kworker/R-rcu_p]
root           6  0.0  0.0      0     0 ?        I<   22:04   0:00 [kworker/R-slub_]
root           7  0.0  0.0      0     0 ?        I<   22:04   0:00 [kworker/R-netns]
root           9  0.0  0.0      0     0 ?        I    22:04   0:00 [kworker/0:1-events]
root          10  0.0  0.0      0     0 ?        I<   22:04   0:00 [kworker/0:0H-events_highpri]

Todos os processos com o formatos específicos.

$ ps -eo %cpu,%mem,pid,uid,lstart,cmd 

%CPU %MEM     PID   UID                  STARTED CMD
 0.0  0.0       1     0 Mon Oct 28 22:04:06 2024 /sbin/init splash
 0.0  0.0       2     0 Mon Oct 28 22:04:06 2024 [kthreadd]
 0.0  0.0       3     0 Mon Oct 28 22:04:06 2024 [pool_workqueue_release]
 0.0  0.0       4     0 Mon Oct 28 22:04:06 2024 [kworker/R-rcu_g]
 0.0  0.0       5     0 Mon Oct 28 22:04:06 2024 [kworker/R-rcu_p]
 0.0  0.0       6     0 Mon Oct 28 22:04:06 2024 [kworker/R-slub_]
 0.0  0.0       7     0 Mon Oct 28 22:04:06 2024 [kworker/R-netns]
 0.0  0.0       9     0 Mon Oct 28 22:04:06 2024 [kworker/0:1-events]
 0.0  0.0      10     0 Mon Oct 28 22:04:06 2024 [kworker/0:0H-events_highpri]

Vendo os detalhes de um processo específico. Ex: nginx

$ ps -C nginx -o pid,user,lstart,cmd

    PID USER                      STARTED CMD
   6596 root     Tue Oct 29 21:02:57 2024 nginx: master process /usr/sbin/nginx -g daemon on; master_process on;
   6598 www-data Tue Oct 29 21:02:57 2024 nginx: worker process
   6599 www-data Tue Oct 29 21:02:57 2024 nginx: worker process
   6600 www-data Tue Oct 29 21:02:57 2024 nginx: worker process
   6601 www-data Tue Oct 29 21:02:57 2024 nginx: worker process
   6602 www-data Tue Oct 29 21:02:57 2024 nginx: worker process
   6603 www-data Tue Oct 29 21:02:57 2024 nginx: worker process
   6604 www-data Tue Oct 29 21:02:57 2024 nginx: worker process
   6605 www-data Tue Oct 29 21:02:57 2024 nginx: worker process
   6606 www-data Tue Oct 29 21:02:57 2024 nginx: worker process
   6607 www-data Tue Oct 29 21:02:57 2024 nginx: worker process
   6608 www-data Tue Oct 29 21:02:57 2024 nginx: worker process
   6609 www-data Tue Oct 29 21:02:57 2024 nginx: worker process
   6610 www-data Tue Oct 29 21:02:57 2024 nginx: worker process
   6611 www-data Tue Oct 29 21:02:57 2024 nginx: worker process
   6612 www-data Tue Oct 29 21:02:57 2024 nginx: worker process
   6613 www-data Tue Oct 29 21:02:57 2024 nginx: worker process

Exibindo o que está sendo executado por um usuário específico. Ex: www-data

$ ps -U www-data -u www-data u

USER         PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
www-data    6598  0.0  0.0  55872  5392 ?        S    21:02   0:00 nginx: worker process
www-data    6599  0.0  0.0  55872  5392 ?        S    21:02   0:00 nginx: worker process
www-data    6600  0.0  0.0  55872  5392 ?        S    21:02   0:00 nginx: worker process
www-data    6601  0.0  0.0  55872  5392 ?        S    21:02   0:00 nginx: worker process
www-data    6602  0.0  0.0  55872  5392 ?        S    21:02   0:00 nginx: worker process
www-data    6603  0.0  0.0  55872  5392 ?        S    21:02   0:00 nginx: worker process
www-data    6604  0.0  0.0  55872  5392 ?        S    21:02   0:00 nginx: worker process
www-data    6605  0.0  0.0  55872  5392 ?        S    21:02   0:00 nginx: worker process
www-data    6606  0.0  0.0  55872  5392 ?        S    21:02   0:00 nginx: worker process
www-data    6607  0.0  0.0  55872  5392 ?        S    21:02   0:00 nginx: worker process
www-data    6608  0.0  0.0  55872  5392 ?        S    21:02   0:00 nginx: worker process
www-data    6609  0.0  0.0  55872  5392 ?        S    21:02   0:00 nginx: worker process
www-data    6610  0.0  0.0  55872  5392 ?        S    21:02   0:00 nginx: worker process
www-data    6611  0.0  0.0  55872  5392 ?        S    21:02   0:00 nginx: worker process
www-data    6612  0.0  0.0  55872  5264 ?        S    21:02   0:00 nginx: worker process
www-data    6613  0.0  0.0  55872  5392 ?        S    21:02   0:00 nginx: worker process

Listando ordenando os processos por consumo de CPU.

$ ps axo pid,stat,%cpu,%mem,comm --sort %cpu

 PID STAT %CPU %MEM COMMAND

3472 Sl    0.1  0.0 conky
4645 Sl    0.1  0.3 code
4657 Sl    0.1  0.3 code
5259 Sl    0.1  0.5 chrome
3202 Sl    0.2  0.3 chrome
3516 Sl    0.2  0.6 chrome
3763 Sl    0.2  0.2 mintreport-tray
4661 Sl    0.2  0.3 code
5162 Sl    0.3  0.6 chrome
6930 Ssl   0.3  0.1 gnome-terminal-
4509 SLl   0.4  0.5 code
1981 Ssl   0.5  0.1 packagekitd
4662 Sl    0.5  0.5 code
3143 SLl   0.6  0.8 chrome
2331 Sl+   0.9  0.5 Xorg
3187 Sl    0.9  0.9 chrome
4549 Sl    0.9  0.7 code
 331 S     1.1  0.0 irq/110-nvidia
3609 Sl    1.2  1.0 chrome
2470 Ssl   1.7  1.3 gnome-shell
4575 Sl    4.1  1.0 code

Vendo as threads relacionadas a um serviço.

$ ps -C nginx -To user,pid,ppid,comm

USER         PID    PPID COMMAND
root        6596       1 nginx
www-data    6598    6596 nginx
www-data    6599    6596 nginx
www-data    6600    6596 nginx
www-data    6601    6596 nginx
www-data    6602    6596 nginx
www-data    6603    6596 nginx
www-data    6604    6596 nginx
www-data    6605    6596 nginx
www-data    6606    6596 nginx
www-data    6607    6596 nginx
www-data    6608    6596 nginx
www-data    6609    6596 nginx
www-data    6610    6596 nginx
www-data    6611    6596 nginx
www-data    6612    6596 nginx
www-data    6613    6596 nginx

Vendo as threads relacionadas a um PID.

$ ps -T -p 6596

PID    SPID TTY          TIME CMD
6596    6596 ?        00:00:00 nginx

Segue abaixo alguns argumentos, que podem ser utilizados tanto no ps -eo como no ps axo

%cpu: cpu utilisation
%mem: memory percentage utilisation
args: The command with all its arguments
c: processor utilisation
cmd: The command
comm: The command name only
cp: CPU Usage
cputime: CPU Time
egid: Effective group id
egroup: Effective group
etime: Elapsed time
euid: Effective user id
euser: Effective user
gid: Group id
group: Group name
pgid: Process group id
pgrp: Process group
ppid: Parent Process ID
start: Time the process started
sz: Size in physical pages
thcount: Threads owned by the process
time: Cumulative time
uid: User Id
uname: User name

Obrigado pela leitura!