Introduction
Explore essential Linux process management commands, from ps and top to renice and cgroups. Optimize your system performance with real-time insights into process prioritization and resource control
Linux provides a variety of commands for process management.
Here is a list of commonly used commands:
ps aux
top: Display and update sorted information about processes.
top
htop: An interactive process viewer, an enhanced version of top.
htop
kill: Terminate a process by sending a signal to its process ID.
kill [signal] [PID]
pkill: Send a signal to a process or processes based on their name.
pkill [signal] [process_name]
killall: Kill processes by name.
killall [process_name]
pgrep: Display the process IDs of processes matching a name.
pgrep [process_name]
killpg: Terminate a process group.
killpg [process_group_ID]
renice: Change the priority of a running process.
renice [priority] [PID]
nice: Run a command with a modified scheduling priority.
nice [priority] [command]
jobs: Display status of jobs in the current session.
jobs
bg: Move a job to the background.
bg %job_number
fg: Bring a job to the foreground.
fg %job_number
nohup: Run a command immune to hangups, with output to a non-tty.
nohup [command] &
pstree: Display a tree of processes.
pstree
at: Schedule a command to be run at a later time.
at [time] [date] [command]
cron: Schedule commands to be run periodically.
crontab -e
systemctl: Control the systemd system and service manager.
systemctl [command] [service_name]
Remember to consult the man pages for each command (man command) for more detailed information and options.
Process Prioritization:
Here are some key concepts and commands related to process prioritization:
Nice Value:
The nice value (or niceness) is a measure of a process’s priority in the Linux scheduling algorithm.
Values range from -20 to 19, where lower values represent higher priority.
The nice and renice commands are used to set or adjust the nice value of a process.
# Set the nice value when starting a process nice -n [nice_value] [command]
# Change the nice value of an already running process
renice [nice_value] [PID]
Priority Levels:
Processes are scheduled based on their priority levels.
Higher priority processes get more CPU time.
The ps command can be used to display the priority (NI column).
ps aux
Real-Time Processes:
Some processes may require real-time scheduling, where they are given priority over normal processes.
Real-time processes are assigned priority values in the range of 1 to 99.
The chrt command is used to set or view the real-time attributes of a process.
# Set real-time priority for a process chrt -r [priority] [PID]
Display real-time attributes of a process
# Display real-time attributes of a process chrt -p [PID]
CPU Affinity:
CPU affinity determines which CPUs a process is allowed to execute on.
The taskset command is used to set or retrieve the CPU affinity of a process.
Set CPU affinity for a process
# Set CPU affinity for a process taskset -c [CPU_list] [PID]
Display CPU affinity of a process
# Display CPU affinity of a process taskset -p [PID]
Control Groups (cgroups):
Control groups allow the allocation of resources, such as CPU, memory, and I/O, among a set of processes.
The cgexec and cgclassify commands are used to run processes in a specific control group.
Example of creating and running a process in a control group
# Example of creating and running a process in a control group cgcreate -g cpu:mygroup cgexec -g cpu:mygroup [command]
Scheduler Policies:
The Linux scheduler supports different policies, such as SCHED_FIFO (First In, First Out), SCHED_RR (Round Robin), and SCHED_OTHER (the default).
The chrt command can also be used to set the scheduling policy.
Set scheduling policy for a process
# Set scheduling policy for a process chrt -p [policy] [priority] [PID]
Understanding and managing process prioritization is crucial for optimizing system performance and ensuring that critical tasks receive the necessary resources.
The appropriate adjustments depend on the specific requirements of your system and workload.
ionice:
The ionice command sets or gets the I/O scheduling class and priority for a program.
It allows you to control the priority of I/O operations.
Set I/O priority for a process
# Set I/O priority for a process ionice -c [class] -n [priority] [command]
Display I/O priority of a process
# Display I/O priority of a process ionice -p [PID]
I/O scheduling classes include idle, best-effort, and real-time.
Nice and Renice:
The nice command is used to launch a new process with a specified niceness.
The renice command changes the niceness of an already running process.
Niceness values range from -20 to 19.
Change the nice value of an already running process
# Set the nice value when starting a process nice -n [nice_value] [command]
CPU Usage and Load Average:
The top command provides real-time information about system resource usage, including CPU usage and load average.
Load average indicates the average number of processes in the run queue over 1, 5, and 15 minutes.
top
Scheduler Policies:
The chrt command, in addition to setting real-time priority, can be used to set scheduling policies.
Scheduling policies include SCHED_FIFO (First In, First Out), SCHED_RR (Round Robin), and SCHED_OTHER (the default).
Set scheduling policy for a process
chrt -p [policy] [priority] [PID]
# Set scheduling policy for a process chrt -p [policy] [priority] [PID]
nice, ionice, renice in a single command:
The prlimit command is a powerful tool that allows setting various resource limits for a process, including nice value and ionice class.
prlimit --pid [PID] --nice=[nice_value] --ionice=[class:priority]
This command provides a convenient way to set multiple resource limits in one go.
Remember that the actual impact of these commands depends on the workload and system characteristics. Adjusting process priorities should be done thoughtfully, considering the specific requirements of your system and applications. Always monitor the system’s behavior after making changes to ensure optimal performance.
ulimit:
The ulimit command is used to set or display user-level resource limits for processes.
It controls various system resource limits such as file size, core dump size, and stack size.
To Display all resource limits
ulimit -a # Display all resource limits
To Set core dump size to unlimited
ulimit -c unlimited # Set core dump size to unlimited
System Resource Monitoring:
Tools like sar (System Activity Reporter) and vmstat (Virtual Memory Statistics) provide detailed insights into system resource usage, including CPU, memory, and disk I/O.
sar vmstat
Nice and Priority in ps Output:
The ps command can display the nice value and priority of processes.
ps -o pid,ppid,nice,pri,stat,cmd
This command shows the process ID, parent process ID, nice value, priority, process status, and command.
cpulimit:
The cpulimit command limits the CPU usage of a process.
It is useful for preventing a process from using excessive CPU resources.
cpulimit -l [percentage] -p [PID]
cgroup Resource Control:
Control Groups (cgroups) provide resource control and isolation for processes.
They allow you to allocate resources (CPU, memory, etc.) among user-defined groups of processes.
# Example: Create a cgroup and limit CPU usage
sudo cgcreate -g cpu:mygroup sudo cgset -r cpu.cfs_quota_us=100000 mygroup sudo cgexec -g cpu:mygroup [command]
Thread Priority:
On Linux, processes can have multiple threads, each with its own priority.
The ps command with the -L option can be used to display thread information.
ps -eLf
Nice Value Adjustment by Non-Root Users:
By default, non-root users can increase the nice value of their processes but cannot decrease it. This prevents regular users from setting a higher priority than their default.
Non-root user increasing the nice value
nice +5 [command]
# Non-root user increasing the nice value nice +5 [command]
These commands and concepts provide a comprehensive view of process management, resource control, and optimization on Linux systems. Experimenting with these commands in a controlled environment will help you understand their impact on system behavior and performance. Always exercise caution when adjusting system parameters to avoid unintended consequences.
strace:
strace is a powerful debugging tool that traces system calls and signals, including the ones made by a process.
It can be used to analyze the behavior of a process and identify performance bottlenecks.
strace [command]
nice and ionice with Command Execution:
You can set the niceness and I/O priority for a command directly in the shell.
nice -n 10 ionice -c 2 -n 3 [command]
This example starts a command with a niceness of 10 and an I/O priority of class 2 and priority 3.
schedtool:
The schedtool command allows you to query or change a process’s scheduling policy and priority.
schedtool -a [priority] -e [command]
This example sets the scheduling policy to SCHED_FIFO and the priority to the specified value for the given command.
CPU Governors:
On systems with a dynamic frequency scaling governor, you can control the CPU frequency and power consumption.
cpufreq-info cpufreq-set -r -g [governor]
Common governors include ondemand, performance, and powersave.
nice and renice for User-level Prioritization:
The nice and renice commands can be used by regular users to prioritize their processes.
nice -n 10 [command] renice 10 -p [PID]
Users can adjust the niceness of their processes within certain limits.
Monitor Disk I/O:
Tools like iotop and iostat can help monitor disk I/O usage.
iotop iostat
/proc Filesystem:
The /proc filesystem provides a wealth of information about the kernel, processes, and hardware. You can explore various directories and files within /proc to gather detailed information about running processes.
cat /proc/[PID]/status
This example displays detailed information about a specific process.
Experimenting with these commands and monitoring tools will deepen your understanding of process management on Linux systems.
Always ensure that any adjustments made align with the requirements and constraints of your system and workload.
Here’s a quiz to test your knowledge about Linux process management:
1-What command is used to display real-time information about processes, including CPU usage?
a) ps
b) top
c) htop
d) ps aux
2-Which command is used to terminate a process by sending a signal to its process ID?
a) kill
b) pkill
c) killall
d) terminate
3-What is the range of nice values in Linux?
a) 1 to 10
b) -10 to 10
c) -20 to 19
d) 0 to 100
4-Which command is used to set the CPU affinity for a process?
a) taskset
b) chrt
c) cpulimit
d) ionice
5-What does the renice command do?
a) Changes the priority of a running process
b) Sets the CPU affinity for a process
c) Kills a process by name
d) Displays real-time attributes of a process
6-Which command allows you to schedule a command to be run at a later time?
a) cron
b) at
c) timer
d) schedule
7-What command is used to control the systemd system and service manager?
a) systemd
b) initctl
c) service
d) systemctl
8-Which scheduling policy in Linux is based on a First In, First Out (FIFO) model?
a) SCHED_RR
b) SCHED_OTHER
c) SCHED_FIFO
d) SCHED_BATCH
9-What tool is used to trace system calls and signals made by a process?
a) debug
b) trace
c) strace
d) systool
10-Which command is used to limit the CPU usage of a process?
a) cpulimit
b) cpucontrol
c) limitcpu
d) cputhrottle
Answers:
1-b) top
2-a) kill
3-c) -20 to 19
4-a) taskset
5-a) Changes the priority of a running process
6-b) at
7-d) systemctl
8-c) SCHED_FIFO
9-c) strace
10-a) cpulimit
Quiz 2
1-Which command is used to display a tree of processes?
a) pstree
b) proctree
c) treeview
d) processtree
2-What is the purpose of the ulimit command?
a) Set CPU affinity for a process
b) Adjust scheduling policies
c) Set or display user-level resource limits
d) Change the priority of a process
3-Which command allows you to create and manage control groups (cgroups) on Linux?
a) controlgroup
b) cgroupctl
c) cgmanage
d) cgcreate
4-What does the iotop command do?
a) Displays information about I/O priority of processes
b) Kills processes with high I/O usage
c) Sets I/O priority for a process
d) Monitors network traffic
5-Which governor is commonly used to dynamically adjust CPU frequency based on the system’s load?
a) performance
b) ondemand
c) powersave
d) conservative
6-What command can be used to trace system calls and signals for a running process?
a) debugger
b) tracecall
c) strace
d) signaltrace
7-Which file system provides information about the kernel, processes, and hardware in Linux?
a) /sys
b) /proc
c) /var
d) /mnt
8-Which command allows you to change the scheduling policy and priority of a process in a single step?
a) nice
b) renice
c) chrt
d) schedtool
Answers:
2-c) Set or display user-level resource limits
3-d) cgcreate
4-a) Displays information about I/O priority of processes
5-b) ondemand
6-c) strace
7-b) /proc
8-d) schedtool
Quiz 3
1-Which command is used to set the I/O priority of a process in Linux?
a) ionice
b) iopriority
c) ioset
d) iokind
2-What does the sar command provide information about?
a) System architecture
b) System activity and performance
c) System aliases
d) System alerts
3-Which command allows a user to increase the nice value of their own processes?
a) nice
b) renice
c) niceness
d) increase_nice
4-What is the purpose of the /proc/[PID]/status file?
a) Display CPU usage of a process
b) Provide detailed information about a specific process
c) Set real-time priority for a process
d) Kill a process by name
5-Which command is used to display information about CPU frequency scaling?
a) cpufreq-info
b) cpuinfo
c) freqscale
d) scaleinfo
6-In the context of nice values, which of the following represents the highest priority?
a) 0
b) -10
c) 10
d) -20
7-What does the pgrep command do?
a) Display process tree
b) Display process IDs based on a name
c) Set process group ID
d) Kill processes based on a name
Answers:
2-b) System activity and performance
3-a) nice
4-b) Provide detailed information about a specific process
5-a) cpufreq-info
6-d) -20
7-b) Display process IDs based on a name
Quiz 4
1-Which command is used to limit the CPU usage of a process?
a) cpuset
b) cpumin
c) cpurestrict
d) cpulimit
2-What information does the vmstat command provide?
a) Virtual memory statistics
b) Video mode statistics
c) Volume management statistics
d) Vibration monitoring statistics
3-Which command allows you to control the CPU frequency scaling governor?
a) cpufreq-set
b) freqcontrol
c) governorctl
d) scaling-set
4-What does the chrt command allow you to do?
a) Change the root directory
b) Change the scheduling policy and priority of a process
c) Change the hostname of the system
d) Change the terminal size
5-Which command is used to set the scheduling policy and priority for a process?
a) nice
b) chrt
c) schedset
d) priornice
6-What is the purpose of the nice command?
a) Adjust system niceness settings
b) Set priority for I/O operations
c) Change process priority
d) Schedule commands to run periodically
7-Which command is used to display information about disk I/O usage by processes?
a) diskstat
b) iolog
c) iodisk
d) iotop
8-What tool can be used to set various resource limits for a process, including nice value and ionice class?
a) limitctl
b) resourcectl
c) prlimit
d) resourcelimit
Answers:
1-d) cpulimit
2-a) Virtual memory statistics
3-a) cpufreq-set
4-b) Change the scheduling policy and priority of a process
5-b) chrt
6-c) Change process priority
7-d) iotop
8-c) prlimit
Books:
“The Linux Command Line”
by William Shotts: This book covers various aspects of the Linux command line, including process management.
“UNIX and Linux System Administration Handbook”
by Evi Nemeth, Garth Snyder, Trent R. Hein, and Ben Whaley: This book is a comprehensive guide to UNIX and Linux system administration, including process management.
Websites:
Linux.org
Ubuntu Documentation:
Linux Journals and Magazines:
Linux Journal:
Linux Journal has been a valuable resource for Linux-related articles and tutorials.
Linux Forums:
LinuxQuestions.org:
A community-driven forum where you can find answers to Linux-related questions.