Hello! In my last post I have written about permissions in linux. In this post we will explore about processes in linux and how to manage them from cli. Let's go!
Topics covered in this post:
- Intro to modern systems
- What is a process?
- Processes in Linux
- Interacting with processes in cli
- Shutting down the system with cli
1. Intro to modern systems
Many modern systems are usually multitasking, meaning they can perform more than one task at once or at least they pretend to do this so well. In reality, the kernel in the operating system rapidly switches from one process to another and provides an impression that the system is multitasking.
This is true for linux as well. When we switch from a text document to a terminal, the linux kernel also switches the underlying processes as well. Switching a process means allotting the CPU time execution cycle and resources like memory to the processes
2. What is a process?
A program or a script is stored in a file and a process is nothing but a program in motion. Whenever we execute a program, the linux kernel has to allocate certain resources like CPU and memory to that program. The kernel also tracks the execution by assigning an ID called PID or Process ID.
This is shown graphically in operating systems like windows as below
3. Processes in Linux
When a linux system boots up, the kernel starts a few processes using
init is the first program that is launched and it also launches a series of shell scripts (located in
/etc) called “init scripts” which start all the system services. The following image shows these init scripts in my system.
Many of these services will run as “deamon programs”, programs that run in the background and they generally don’t have any UI or require user interaction.
Even when we just login to the system and not necessarily perform any tasks, it manages few processes to keep the system up.
init process here becomes the “parent process” or "grandparent process " to all the process launched through it. The processes launched by other processes are called “child processes”.
4. Interacting with processes in cli
The following commands are widely used in monitoring and managing the processes in linux.
ps- provides snapshots of all the processes running to the terminal
top- provides a dynamic view of all the processes running on the system within the terminal. The output looks similar to the GUI of a modern task manager
<command/script> &- runs the process in the background
jobs- shows the list of processes running in the background
fg %<job_number>- returns the process to the foreground
**ps command: **
It is the most commonly used command to view processes for the user. It shows the snapshot of the processes running on the system at that time. The result looks like the below image
“PID” or Processes ID, is the number assigned by the kernel for that process to track resources allotted. Ex: CPU time (TIME) and memory. “TTY” or “teletype,” refers to the controlling terminal for the process. "TIME", the amount of CPU time consumed by the process.
ps auxcommand will display the process from all the users(
a) that can be attached to any terminal(
x) along with its user/owner info(
**top (table of processes) command: **
It displays a real-time view of the running processes, their resource consumption and also displays kernel-managed tasks. The output is refreshed every 3 seconds by default.
To exit the
top command output prompt and get back to the terminal, press
<command/script> & command
When we run a program with GUI like notepad (gedit) from the cli, it will open a window and the terminal will be busy until the window is closed.
Any command/script that is suffixed with
& operator will run in the background and the terminal is available for the user. For example,
gedit & command will launch the program and terminal will be available to the user right after it.
Notice that there is a number printed on the terminal after the command. It is the PID assigned a shell feature called “job contol” which shows the PID of the process.
It will show the list of background processes/jobs that are launched from the terminal as shown below
ps command will also show info about the above process
fg %<job_number> command
Our process (job id 1) is running in the background and any process running in the background is immune from terminal keyboard input, including any attempt to interrupt it with Ctrl-c.
fg %<job_number> command will bring the process to the foreground
When we press
Ctrl+C, the process will terminate
kill command is used to “kill” the processes. Here’s an example:
gedit & command will launch the program in the background and our PID is available on the terminal. Next, we use
kill <PID> command to terminate the process.
kill command here doesn’t actually “kill” the program, rather it will send a signal to the process to terminate. This gives the process to save the work in progress and the processes will also listen to these signals.
When the process is running on the foreground,
CTRL+Z will send a signal to interrupt and terminate the process respectively.
killallcommand will terminate multiple processes with same or matching name or by username.
killall -u <username>- will kill all the processes under the username provided
killall name- will kill all the processes which matches the provided name
In the above example, multiple instances of
gedit program is launched in the background and will
killall command, we can terminate all those instances at once.
Note: If one user wants to terminate the processes that don’t belong to them, they need superuser privileges.
6. Shutting down the system with CLI
Yes, we can do it! Shutting down a system involves orderly termination of all the processes in the system. It also requires admin privileges to perform this action.
sudo reboot- restart the system
sudo shutdown -h now- shuts down the system without any delay
sudo shutdown -r now- reboots the system without any delay
shutdown command options:
-h- specifies shutting down the system
now- The time string may either be in the format "hh:mm" for hour/minutes (24h format) specifying the time to execute the shutdown at. We can also specify minutes with
+min place of “now”. Example, +0 means now and by default the values is “+1” when not specified in the command.
Process management is a task that is usually maintained by the sysadmins and devops engineers to name a few. This helps in maintaining the health of the machines or servers and resource monitoring as well. Managing a linux system using cli is effective and swift.
- Linux Command Line Books by William Shotts
- Linux Command Basics: 7 commands for process management | Enable Sysadmin (redhat.com)
- linux - What does aux mean in
ps aux? - Unix & Linux Stack Exchange
- [How to Use the top Command in Linux (phoenixnap.com)](https://phoenixnap.com/kb/top-command-in-linux#:~:text=The%20top%20(table%20of%20processes,including%20CPU%20and%20memory%20usage.)
- 8 Linux commands for effective process management | Opensource.com
Did you find this article valuable?
Support mani nekkalapudi by becoming a sponsor. Any amount is appreciated!