ps

1. ps (Process Status)

ps is a command used to show information about the currently running processes.

Syntax:

ps [options]

Common Options:

Example:

ps aux

This shows a full list of processes running on the system.

Sample Output:

USER       PID  %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root        1  0.0  0.1 169448  6092 ?        Ss   10:15   0:03 /sbin/init
user      1234  1.2  0.3 254548 12576 pts/0    Ss+  10:45   0:10 bash
user      1300  0.0  0.1 123456  6789 pts/0    S+   10:47   0:00 vim myfile.txt

2. top (Task Manager)

top provides a dynamic, real-time view of the system's processes.

Syntax:

top

Sample Output:

top - 10:47:09 up 5 days,  3:22,  2 users,  load average: 0.15, 0.13, 0.11
Tasks: 130 total,   1 running, 129 sleeping,   0 stopped,   0 zombie
%Cpu(s):  3.8 us,  1.1 sy,  0.0 ni, 95.0 id,  0.1 wa,  0.0 hi,  0.0 si,  0.0 st
MiB Mem :   3921.7 total,   1286.0 free,   1227.3 used,   1408.4 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   2435.2 avail Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND
  123 root      20   0  169448   6092   4120 S   1.2  0.2   0:03.12 init
  1300 user      20   0  254548  12576   1048 S   0.0  0.3   0:10.34 bash

Press q to exit top.


3. htop (Interactive Process Viewer)

htop is a more user-friendly, interactive version of top that allows you to scroll and search processes.

Install htop (if not already installed):

sudo apt install htop  # For Debian/Ubuntu
sudo yum install htop  # For CentOS/RedHat

Syntax:

htop

Sample Output:

  PID USER      PRI  NI  VIRT   RES   SHR S CPU% MEM% TIME+  COMMAND
  1234 user      20   0 254548 12576  1048 S  0.0  0.3  0:10 bash
  5678 root      20   0  169448  6092  4120 S  1.2  0.2  0:03 init

Press F10 to exit htop.


4. pgrep (Process Grep)

pgrep searches for processes based on name or other criteria.

Syntax:

pgrep [options] pattern

Example:

To find all processes with "python" in the name:

pgrep python

Sample Output:

1234
5678

5. kill (Terminate Process)

The kill command is used to send signals to processes, typically to terminate them.

Syntax:

kill [signal] PID

Example:

To kill a process with PID 1234:

kill 1234

To Force Kill:

If a process doesn't respond to a normal kill, use:

kill -9 1234

Sample Output:

No output on success, but if the process is killed successfully, it will stop running.


6. nice (Start a Process with Modified Scheduling Priority)

nice is used to run a command with a specified priority (niceness).

Syntax:

nice -n [priority] command

Example:

To run a command with lower priority:

nice -n 10 my_script.sh

7. renice (Change Process Priority)

renice allows you to change the priority of an already running process.

Syntax:

renice -n [priority] -p PID

Example:

To change the priority of PID 1234 to 10:

sudo renice -n 10 -p 1234

Sample Output:

1234 (process PID) old priority 0, new priority 10

8. pstree (Process Tree)

pstree displays processes in a tree-like format, showing the parent-child relationships between processes.

Syntax:

pstree

Example:

pstree

Sample Output:

init─┬─systemd─┬─sshd───bash─┬─vim
      │        └─sshd───bash───pstree

9. time (Measure Execution Time)

The time command is used to measure the execution time of a command.

Syntax:

time command

Example:

time ls

Sample Output:

real    0m0.004s
user    0m0.001s
sys     0m0.002s

10. watch (Run Command Repeatedly)

The watch command is useful for monitoring changes in real time, such as process updates.

Syntax:

watch [options] command

Example:

To monitor CPU and memory usage every 2 seconds:

watch -n 2 "ps aux"

Sample Output:

Every 2.0s: ps aux                                                            Tue Nov 12 10:50:00 2019

USER       PID  %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root        1  0.0  0.1 169448  6092 ?        Ss   10:15   0:03 /sbin/init