Linux: Processes
Basics
The kernel tags running command with an identifier called PID = Process ID. The kernel also records the relationships between one process, called the Parent Process, and any process it creates by assigning a second number.
Process ID - PID
The PID is used by the kernel to allocated system resources to a running command/program.
cat /proc/<PID> to read kernel statistics.
kill -9 <PID> to shutdown a signal
renice -n 10 -p <PID> to lower process priority
strace -p <PID> to trace a process' system calls
PID 1 or systemd is the master process that starts first and is never exited from
ps, ps aux, ps -ef to view PIDs
ps -C <process-name>. pgrep <process-ame> to view specific process
Parent PID - PPID
ps -e --forest -o pid,ppid,cmd and pstree -p to PPIDs
When a kill <PPID> signal is sent to a process, all descendent processes receives the same signal.
Orphaned Processes
Orphaned processes are processes with parents that have exited before they finish. They are then adopted by PID 1 and their PID becomes PID 1 and continue running.
Zombie Processes
Zombie processes are processes that completed executing but is still in the process table waiting to be removed by the parent.
Process State
The process state identifies what a process is currently doing. A process can have one of these states:
R= Running: the process is running or ready to run.S= Sleeping - interrupted: the process is waiting for input or for another event to completeD= Blocked - uninterrupted: the processed is doing something important and cannot be interruptedT= Stopped: the process has been manually pausedZ= Zombie: the process has completed and waiting for parent to remove from process tableX= DeadI= Idle
Process Priority
nice
The nice priority command sets the "politeness" (priority) of a process when it competes for CPU time. The value ranges from -20 to 19 (the lowest priority). The default is 0.
nice [-n VALUE] <command> [arguments] to set the priority of a process.
ps -eo,pid,ni,cmd to view the nice value.
renice
renice is used to change the nice value of a running process. Only a user with root privileges can set negative nice values.
The generic syntax is renice [-n VALUE] {-p PID | -g PGID | -u user}
renice -n -5 -p 10234 to change tne nice value to -5 for process with PID 10234.
Process Monitoring Tools
ps - process status
Give a quick look at what is running in your machine. ps -ef give information about all processes including all attributes. ps aux also list processes with detailed information.
ps -e list every process on the system
ps -a list processes from other users terminals
ps -eo [COLUMNS] to customize which column to show
ps -C <command> to list processes with a specific name. ex: ps -C tar
ps -p <PID> to view a process information
| Column | Description |
|---|---|
PID |
Process ID |
PPID |
Parent Process ID |
USER |
Owner of the process |
CMD |
Command used to start the process |
%CPU |
CPU usage |
%MEM |
Memory usage |
VSZ |
Virtual memory size (in KB) |
RSS |
Resident Set Size (physical memory in KB) |
STAT |
Process state (R, S, Z, etc.) + modifiers |
TTY |
Terminal associated with the process |
TIME |
Cumulative CPU time used |
top
top provides a live updates of the process table.
SHIFT + P to sort the processes by CPU usage
SHIFT + M to sort the processes by memory usage
top -d [REFRESH INTERVAL] to set the refresh frequency in seconds. ex: top -d 1 to refresh the list every seconds.
htop
Enhanced top.
atop
atop records and stores metrics for later reviews.
Performance Metrics
mpstat tells how busy each CPU core is. It is part of systat package that is a collection of utilities that collect, report, and log essential system performance metrics.
-P cputargets a single core-P ALLtargets every core
ex: mpstat 3 10 every 3 seconds, print CPU stats. Do this 10 times in a row. or mpstat -P ALL 2 10 to special all CPUs.
pidstat reports CPU, memory, and I/O usage. pidstat is also part of sysstat package.
ex: pidstat 2 shows per process CPU usage updated every 2 seconds.
-ushows CPU usage-rshows memory usage-dshows i/o usage-p <PID>show stats for a specific process
ex: pidstat -u -r -d -p 2345 2 10 shows CPU, memory, i/o activity of process 2345 every 2 seconds for 10 times
Job Control Commands
Ctrl + Zsuspends the active process and returns to the shell prompt without terminating the job.jobslets you see what jobs you have running in the shellbgresumes a suspended job in the background.bg %1resumes job 1 in the backgroundfgbrings a job to the foreground.fg %1brings job 1 to the foregrounddisown %1removes job 1 from job control from the shell's job table and keeps it running even after the shell existskillsends a signal to a job or process to terminate. ex:kill %1to terminate job 1 orkill 2345to terminate a job with PID 2345&runs a command in the background from the start. exmpstat 2 &nohup(no hang up). it is likedisownbut prevents the job from receiving hang up signal from the start. ex:nohup long_job.sh &to start a job, free the terminal for other commands, and continue the job even if the terminal closes.
Job Scheduling
crontabhelps run tasks repeatedly at regular intervals, such as every day, week, month... usecrontab -eto edit user contrab. ex:crontab -e 0 23 * * * my_script.shto run my_script.sh every day at 11:00 PM. Usecrontab -lto view scheduled jobs.athelps schedule one time tasks that will run at a specific data and time in the future. ex:at 11:30 AM tomorrowwith prompt for the command to run tomorrow at 11:30 am.anacronensures scheduled tasks still run even if the computer is turned off when they where originally scheduled to execute.