When a system starts up, kernel initiates few of its own activities as processes and launches a program called init
init starts systemd which starts all system services
Each process is assigned a number called Process ID (PID)
PIDs are assigned in ascending order
init always has PID 1
Like files, processes also have owners, user ids, effective user ids etc.
Finding process
pgrep
Finds PID based on process name
# returns only PID> pgrep xlogo28236# returns PID as well as name> pgrep -l xlogo28236 xlogo
Viewing processes
ps
Shows processes snapshot
Columns
PID: Process ID
TTY: controlling terminal
TIME: CPU time consumed by process
CMD
USER: owner of process
%CPU: CPU usage in percent
%MEM: Memory usage in percent
VSZ: Virtual memory size
RSS: Resident set size
This is the amount of physical memory (RAM) the process is using in KB
STAT: Process state
START: Time when the process started
PPID: Parent PID
C: CPU Utilization factor
used internally by kernel to determine how resource hungry process is
STIME: Start Time
Wall clock time when process started
# shows processes from current terminal sessionps
BSD style flags
# show all processes including without controlling terminalps x# a: all processes with controlling terminal including other users# u: user oriented format# x: include processes without controlling terminal# show all processes with extra infops aux# show info about specific processps u <PID>
Standard flags
# e: every process# f: full format layout# shows all processes with info including PPIDps -ef# F: Extra full format layout# shows more info as compared to `f` like %CPUps -eF# show info about specific processps -f -p <pid># show processes as treeps -ef --forest
Process states
R: Running
S: (Interruptible sleep) The process is waiting for something to happen
D: (Uninterruptible sleep) Process is waiting for I/O
Z: Zombie process
T: Stopped
Priority modifiers
<: High priority process
N: Low priority (nice) process
pstree
Outputs a process list arranged in a tree-like pattern showing the parent-child relationships between processes
top
shows continuously updating display of system processes in order of activity
Heading
top — name of program
14:59:20 — current time of day
up 6:30 — uptime since last boot
2 users — number of users logged in
load average: 0.07, 0.02, 0.00 — the number of processes that are in a runnable state and are sharing the CPU
cach: RAM that keeps copies of frequently read data so applications don’t have to fetch them from the slow disk again
nothing to do with swap
Columns
PR: Priority
NI: Nice value
VIRT: Virtual Memory
includes physical RAM + Swap + Shared files
RES: Resident memory
actual physical RAM
SHR: Shared memory
The portion of memory the process shares with other programs
example: Kernel loads one single copy of system libraries into memory and lets multiple programs share it
S: Process status
TIME+: Total clock time CPU spent actively executing instructions
htop
interactive alternative to top
supports coloring, mouse support, easy killing of processes
vmstat
Outputs a snapshot of system resource usage including, memory, swap, and disk I/O
# show current snapshotvmstat# prints snapshot at every 5svmstat 5
Controlling processes
Putting process in background
Use & to put process in background
# runs xlogo in background# returns job ID: 1, PID: 28236> xlogo &[1] 28236# show running jobs> jobs[1]+ Running xlogo &# bring back job 1 to foreground# job id is optional if there is one background job> fg %1
Pausing a process
Use Ctrl + Z to pause the process and put in background
It sends SIGTSTP signal
Use bg to resume process
It sends SIGCONT signal
# press Ctrl + Z> xlogo[1]+ Stopped xlogo# resume background process# job id is optional if there is one background job> bg %1[1]+ xlogo &
Make process hang proof
When terminal session ends (hangs up/terminate), it sends SIGHUP signal to all its controlling processes
nohup prevents SIGHUP signal to reach process and keeps it alive after terminal session ends
It writes stdout to nohup.out file
use it with & to put in background
# does not put in background# can be stopped using Ctrl+C which sends SIGINT not SIGHUPnohup xlogo# put in backgroundnohup xlogo &# close terminal and run in another terminal# This shows TTY as ? since controlling terminal is terminatedps u <pid>
disown
Removes an active background job from the shell’s internal job table
It stays a child of your current terminal until the terminal is closed
Once current terminal closes, kernel re-parent the orphaned process to init (PID 1)
# Start a process and put it in the background> xlogo &[1] 28236# Remove it from the shell's job table> disown %1# Verify it is gone from your job tracking list> jobs# (Returns nothing! The process is now running independently)# Get PID> pgrep xlogo356714# TTY and PPID> ps -f -p 356714UID PID PPID C STIME TTY TIME CMDkartoos 356714 210142 0 22:55 pts/0 00:00:00 xlogo# Close the terminal and run this in new terminal# Notice TTY is now "?" and PPID is "1" now> ps -f -p 356714UID PID PPID C STIME TTY TIME CMDkartoos 356714 1 0 22:55 ? 00:00:00 xlogo
Changing priority of process
Niceness (NI) refers to the scheduling priority given to a process by kernel
Niceness Range: [-20, 19]
Default: 0
less niceness —> more priority
Priority (PR) refers to the actual, dynamic value the kernel scheduler uses internally to rank processes for CPU execution
You cannot control priority
Priority Range: [0, 39]
Default: 20
Less priority —> higher precedence
Priority=20+Niceness
calculates baseline priority
kernel can change it as required
# launch program with more niceness (less priority)nice -n 10 <program># launch program with less niceness (more priority)sudo nice -n -10 <program># change priority of existing processrenice -n 19 <pid>