Controlling Processes in Linux System
Tags: ubuntu, linux, computerscience, beginners
Published: 2020-08-16 05:41:03

In the last post, we explored [Processes](https://dev.to/yashsugandh/all-about-processes-and-how-can-we-view-them-1n2i). what are processes and how can we monitor them?

In this post, we will explore how can we control these processes?

We learned in the last post that there are two types of processes :

1. Foreground Processes
The foreground processes are those which can be seen on UI and require some sort of user input.
    For example, a text editor.

2. Background Processes
    The background processes are those which do not require any form of input from the user and run automatically in the background.

Let's take an example, that we want to run a Spring Boot Gradle application using the command `gradle bootrun`.

![spring-boot-run](https://dev-to-uploads.s3.amazonaws.com/i/q5lw76yhb2zzyr30le8g.png)

Now the process has started but we cannot access the terminal.

What if we want to move the process in the background?

To move the process in the background all we have to do is press `Ctrl + Z`

![terminal-background-move](https://dev-to-uploads.s3.amazonaws.com/i/vealrc67djprnx04jobb.png)

In the above example, we can see that the process is now moved to the background and it returns the process id (PID) along with status of suspended.

But what is the meaning of **suspended** here?

The **suspend** state specifies that our process is not terminated but is present in a suspended state.

Okay, so when we use the `Ctrl + Z` the process is temporarily stopped.

What if we wanted to bring the process to the foreground again?

To bring the process to **foreground** we use the command `fg`

![foreground-process](https://dev-to-uploads.s3.amazonaws.com/i/9zobb0nmk36t35ra6ouz.png)

In the above example, we moved our process to the foreground again using the command `fg` and it returned us the status **continued** with PID.

Now, what if we want to terminate the process?

To terminate a process we press `Ctrl + C`

![terminate-process](https://dev-to-uploads.s3.amazonaws.com/i/ffydvcappq8pjt634lb2.png)

In the above example, we pressed `Ctrl + C` to terminate the running process.

Till now we have seen how to run a process in the foreground, move the process to background, and bring the process to the foreground again.

What if we wanted to start the process in the background?

To run a process in the background all we need to do is write `&` at the end of our command

Let's take an example that we want to run `gedit` in the background

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/r11irt0rlsogc72szmnv.png)

In the above example, we used the command `gedit &` and ran the process in the background and we got a process id(PID) in return.

So the process is running in the background but how can we confirm it?

To check all the processes in our current terminal we use the command `jobs`.

![gedit-jobs](https://dev-to-uploads.s3.amazonaws.com/i/p920s01l1ixqlf0uo3im.png)

In the above example, we can see that a job is running in the background named `gedit`.

Now, we have already seen how to bring a process to the foreground but what if there are multiple processes?

To bring a specific process to the foreground we use the following syntax

```
fg #{process_number}
```
Let's take an example and bring our `gedit` job to the foreground

![gedit-foreground](https://dev-to-uploads.s3.amazonaws.com/i/2w47bajwyau6pwlhfezd.png)

In the above example, we used the command `fg #1` and brought the `gedit` process to the foreground again.

Now, let's have a look at how to control processes using signals

We use the kill command to send signals to programs.

There are many different signals we can send using the kill command and we can list these signals using the command `kill -l`.

![kill-list](https://dev-to-uploads.s3.amazonaws.com/i/rwvc9eyiwjs0pg7xrd6i.png)

Let us explore a few of the most used signals

| Signal  	| Value    	| Description                                                                   	|
|---------	|----------	|-------------------------------------------------------------------------------	|
| SIGHUP  	| 1        	| Hangup detected on controlling terminal or death of controlling the process   	|
| SIGINT  	| 2        	| Interrupt from keyboard It is the action performed when we press Ctrl + C     	|
| SIGQUIT 	| 3        	| Quit from keyboard                                                            	|
| SIGKILL 	| 9        	| Immidiately kill the process without any time for cleanup                     	|
| SIGTERM 	| 15       	| It is used to Terminate a process. It is the default action with KILL command 	|
| SIGCONT 	| 19,18,25 	| Used to Continue the process if stopped                                       	|
| SIGSTOP 	| 17,19,23 	| Used to Stop a process                                                        	|

The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.

To kill any process using kill command we need the PID of the respective process.
We can either use the name of the signal or the value of a signal along with PID to kill any process.

Let's take an example, that we want to kill the screenshot application 

![kill-screenshot](https://dev-to-uploads.s3.amazonaws.com/i/aui1n3bf901el05aa85w.png)

In the above example, we first searched for the PID using the command we learned in the last post `ps -ef | grep screenshot`. 

We used the `kill` command along with signal value as well as with signal name to **kill** the screenshot application.

What if we wanted to kill the process with name and not PID?

To kill a process with a name we use the `pkill {regular_expression}`

Let's take an example, we used the command `pkill fire`. This command, when executed will kill every process starting with **fire**.

What if we want to kill all the instances of a running application? Is there any way we can do it in 1 go?

We can kill multiple instances of an application using the `killall` command.

The main difference between `killall` command and `pkill` command is that we need to provide exact value to `killall` command.

![pkill-killall](https://dev-to-uploads.s3.amazonaws.com/i/lrjpyu62td3bm35dho0s.png)

In the above example, we used the commands `pkill` and `killall` to kill **gedit**. While executing we observed that when we give `killall` command an incomplete name the `killall` was unable to find the processes.

This was all about how we can control the processes.

Please let me know if you have any suggestions or queries in the discussion below. See you in the funny papers.