Linux: How to Manipulate Background Process in Linux

The Linux Process Execution

Linux process execution refers to the way in which programs are run on a Linux system. When a user initiates an executable program, the Linux kernel creates a new process to execute that program. The process is responsible for executing the program’s instructions and managing its resources, such as memory allocation and input/output operations. Each process has its own unique process ID (PID) and is associated with a parent process, except for the initial process, which is created by the kernel when the system boots up. The execution of a process can be controlled using signals and system calls, which enable interprocess communication and allow processes to interact with each other. Overall, Linux process execution is a fundamental aspect of the operating system that enables the efficient use of resources and the seamless running of multiple programs simultaneously.

Process (ps)

The programs that run on the Linux system are referred to as processes. The kernel is in charge of them. Every process has its own unique process identifier (PID). The sequence in which processes are created determines the PID. To find the ongoing processes in your system you need to run the command below.

$ ps

 APQci0g GAbrT7Vgean7kz XysUo1C0O4xcJn095Nhj9UfS4p7G3FxSVcMm55hhVYlsBYW2IrPwp8vyw4ycMooGlOf8RvhNJDmzlQlA4AVBc6wcRKXVZRu C6SRuZ9IpKTaqbTMIiocY52hL2e MNXUb5YphkLZDps13yvmKHU8r2 SZ0ZqUoLP0 - Linux: How to Manipulate Background Process in Linux

The result shows the snapshots of two processes running in the system. Additional pieces of information about the processes are also displayed. Let’s dive into the details.

  • PID: Process ID
  • TTY: It stands for ‘teletypewriter’. It controls the terminal associated with the process.
  • TIME: CPU usage time
  • CMD: Name of the command.

Another useful command is ‘top’ which shows real-time information about the currently running processes on the Linux system. It keeps updating the results every 10 seconds.

$ top


iRIK4uh0G4phvSyc0x TEpMT RRuik6R9HRT RQXMPXun9JfkEQKcQCNndmN5M HOgHH6H XB1lkecFXyxfSvlwCfFd1QSmPk Wi7wciGRty8DheEuBJMamJnpaOqF6FucqCE04AUA BcDwgdsK5lYk5Uf3ywsGqfAPJ1STLUFsLUEhMW2UjeF1 - Linux: How to Manipulate Background Process in Linux

Types of Processes

There are mainly two kinds of processes in the Linux system.

  • Foreground processes: Foreground processes are also called as interactive processes as they are associated with terminals and take input from the keyboard.  A user has to be connected to initialize these kinds of processes. Run the following command.
$ pwd

SJff8yiVlXr1PkjtnDz598WYAprQqxwV6akKSfgmjBVtlNa6BjcQfOWWRkfTvqua2q258jMch4xiyLAEqzGK6lzB1SJ3dsiwe45 WePJ9GjKwAD3ULltStcl0RgHYsCAUm5iDaEvMe yiu9UbPFNE8akT5jUJG6oeQCJFO0qcIKBBvarKaP3Trpy - Linux: How to Manipulate Background Process in Linux

When the ‘pwd’ command is running in the foreground, it takes time and no other processes may run in the meantime. Because the ‘pwd’ process must be finished before the prompt can be used. 

  • Background Processes:  They are also known as non-interactive or automatic processes as they are not associated with terminals. They run in the background without requiring keyboard input and wait until it is needed. Hence, other processes can be initialized in parallel. 
$ pwd &

 EAWXs66NAkLPlOZLjaS7KmzS3afX8ZJHsyWosZe5vndjQDk Yk3wq0FpKmpHpV ZPLFLo3c3TMiDxvlTrh74wDcgo QqItgO2mWHt2ngnUVkDCQfB1S1QXqaZ5g8vuGw gNA7 - Linux: How to Manipulate Background Process in Linux

 The command line runs in the background without taking any user input. It reaches the stop state when any user data input is given and moved to the foreground.

Process creation

A new process is created when an existing process makes an exact copy of itself in the memory using the fork system call. Every process must have a parent process in this way. The environment is the same for both the child and parent processes, but their process id (PID) is different. To see details information about the processes, let’s run the command below.

$ ps -l 

 MQD X0 ny6jOeIWeaW5VfU0DA2YHrWZvpRzgMpAt9mV3hu7FFrMumaxASx7mpzeBcW7jQcdDjfyVUAp5PZD0MwYmqOKAstWdzsu4BL YRySoaQO69hGN5G5H9dlVtwF8jj9ga5a0Egyb6zIO HV7Zb2man8xE4qIc4YSJ3eVgi0F tGZNVJNBysw - Linux: How to Manipulate Background Process in Linux

Here, PPID is the parent id for a process.

Init Process

It is the system’s parent process, in charge of overseeing all other processes.

It runs when the Linux system starts up and does not have a parent process because it is launched by the kernel itself. The PID of the init process is always 1. To find the PID of a process, you may run the following command.

$ pidof systemd 
oyL47Z9e1NckywinOKQp3bJQS baA2Bo0DeX GIBD5NGKbsjWZqtTmNrHd7IGdfgWGd6STz75GKftfNNk8Uo0gDvIpZsNV Qrf5rz6F4rrPDZJrq19O2OqPHkwVyslMT4kRp14JUQ952Dr21gpom0cgA7sjmOP4qeKk3HxngzYRN2yHnI9C1h md - Linux: How to Manipulate Background Process in Linux

From the result, 1580 is the PID number of the bash process. You can also find the PID and parent id (PPID) of a process.

$ echo $$
$ echo $PPID
vqXN1TYc8WXSa AoPJm5Gu3gmJDG yXW6lomoPa2DLKvfsQIKqlT0tiSbVVdByF0Dx2d4zgYOO4FMHoMlLq4BjDAVGgET8K74lymEKVLGWKvUUNhpsJV4hBBpfharwdVRl0o0dMM6jE jYX8SNrdSyZLPzRjLBWL2agYQutq11tAnWmo24yiE tO - Linux: How to Manipulate Background Process in Linux

Process states

Depending on the environment, a process might be in many states. The following sections go through these states. 

  • Running (R): the process is currently running on the system.
  • Waiting (S): the process is in interruptable sleep waiting for an event to finish. 
  • Uninterruptible sleep (D): this process can not be interrupted by the user.
  • Stopped (T): the process is stopped or killed.
  • Zombie (Z): Although the process is no longer active, their statuses are still being gathered.

Let’s run the following command again.

$ ps -l
8ijJwQ6eDaHgC43uGkNKS9MHHAZ6sGJUeDYMhSlXWi169f5QObyNZFOSSm YGKGhgC0miT5Zy7gtg HK0dMyzcwrOyt1qvX 2EEh ww4YYZB24Av JpavXq5AwjJu2c6lFJG0 GIJpW28K - Linux: How to Manipulate Background Process in Linux

Now you can see that all the process has a stage associated with them in the beginning. We can see that it shows that the ‘ps’ command is running.

/proc filesystem

All the process information is stored in the /proc file. Run the command below.

$ ls /proc/


- Linux: How to Manipulate Background Process in Linux

You can see lots of PID values in the location, These are subdirectories that contain information about the process. Let’s dive into one of these directories. 

$ cat /proc/129/status


koiTU ncysEeUeVgvdafHIW2oD29ILw3YPQ1HPI2T 8icEAB u4IYFKZZc8b - Linux: How to Manipulate Background Process in Linux

Results show tons of data about the PID 129 states.

Signals

To control processes in Linux, different types of signals can be sent to the process. To view the signals, let’s run:

$ kill -l
OxfJPWe91RUy CA6O9oeeaa Yiv70hQj0t8d9fcJ4g21bSIFqcXlFWPv0mvFHILZL1zS3A0tvJWfw MUYZMPkkhscQv5wZfxMAhbRvthfvBOhhnz0ZagS9MBpfiRavl2T - Linux: How to Manipulate Background Process in Linux

You can use the kill, pkill, or pgrep commands to deliver a signal to a process. This is how the process will react if it identifies the signals. Each signal is assigned an integer value.

Stopping a process

Stopping or terminating a process is easy. You need to use the kill command. Now, let’s explore these.

$ ps -l


- Linux: How to Manipulate Background Process in Linux

If you want to stop the process that has a PID of 6150, you need to run the command below.

$ kill 6150

By default, it sends the 15 or SIGTERM signal to the process which terminates the process. You can also do this in this way.

$ kill -9 6150

It will send the SIGKILL signals which will kill the process.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.