DEV Community

maninekkalapudi
maninekkalapudi

Posted on • Originally published at maninekkalapudi.com on

Process management with Linux CLI

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:

  1. Intro to modern systems
  2. What is a process?
  3. Processes in Linux
  4. Interacting with processes in cli
  5. Signals
  6. 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

Untitled.png

3. Processes in Linux

When a linux system boots up, the kernel starts a few processes using init. 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.

Untitled 1.png

Many of these services will run as deamon programs , programs that run in the background and they generally dont 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.

The init process here becomes the parent 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.

a. ps- provides snapshots of all the processes running to the terminal

b. 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

c. <command/script> &- runs the process in the background

d. jobs- shows the list of processes running in the background

e. fg %<job_number>- returns the process to the foreground

a. **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

Untitled 2.png

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 aux command will display the process from all the users(a) that can be attached to any terminal(x) along with its user/owner info(u)

b. **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.

Untitled 4.png

To exit the top command output prompt and get back to the terminal, press q.

c. <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.

Untitled 5.png

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.

Untitled 6.png

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.

d. jobs command

It will show the list of background processes/jobs that are launched from the terminal as shown below

Untitled 7.png

The ps command will also show info about the above process

Untitled 8.png

e. 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

Untitled 9.png

When we press Ctrl+C, the process will terminate

Untitled 10.png

Signals

kill command is used to kill the processes. Heres an example:

Untitled 11.png

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.

The kill command here doesnt 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+C and CTRL+Z will send a signal to interrupt and terminate the process respectively.

  • killall command 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 dont 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:

  1. -h- specifies shutting down the system
  2. 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 +m in place of now. Example, +0 means now and by default the values is +1 when not specified in the command.

Conclusion

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.

Resources

  1. Linux Command Line Books by William Shotts
  2. Linux Command Basics: 7 commands for process management | Enable Sysadmin (redhat.com)
  3. linux - What does aux mean in ps aux? - Unix & Linux Stack Exchange
  4. [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.)
  5. 8 Linux commands for effective process management | Opensource.com

Top comments (0)