Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
August 16, 2020 05:41 am GMT

Controlling Processes in Linux System

In the last post, we explored Processes. 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

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

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

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

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

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

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

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

Let us explore a few of the most used signals

SignalValueDescription
SIGHUP1Hangup detected on controlling terminal or death of controlling the process
SIGINT2Interrupt from keyboard It is the action performed when we press Ctrl + C
SIGQUIT3Quit from keyboard
SIGKILL9Immidiately kill the process without any time for cleanup
SIGTERM15It is used to Terminate a process. It is the default action with KILL command
SIGCONT19,18,25Used to Continue the process if stopped
SIGSTOP17,19,23Used 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

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

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.


Original Link: https://dev.to/yashsugandh/controlling-processes-in-linux-system-4974

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To