- Job Control
- A Practical Example
- Putting a Program into the Background
- Listing Running Processes
- Killing a Process
- A Little More About kill
- That’s It!
- Further Reading
- Controlling Processes in Linux System
- All You Need To Know About Processes in Linux [Comprehensive Guide]
- Types of Processes
- What is Daemons
- Creation of a Processes in Linux
- How Does Linux Identify Processes?
- The Init Process
- Starting a Process in Linux
- Linux Background Jobs
- States of a Process in Linux
- How to View Active Processes in Linux
- 1. ps Command
- 2. top – System Monitoring Tool
- 3. glances – System Monitoring Tool
- How to Control Processes in Linux
- Sending Signals To Processes
- Changing Linux Process Priority
- If You Appreciate What We Do Here On TecMint, You Should Consider:
Job Control
In the previous lesson, we looked at some of the implications of Linux being a multi-user operating system. In this lesson, we will examine the multitasking nature of Linux, and how it is controlled with the command line interface.
As with any multitasking operating system, Linux executes multiple, simultaneous processes. Well, they appear simultaneous, anyway. Actually, a single processor core can only execute one process at a time but the Linux kernel manages to give each process its turn at the processor and each appears to be running at the same time.
There are several commands that are used to control processes. They are:
- ps — list the processes running on the system
- kill — send a signal to one or more processes (usually to «kill» a process)
- jobs — an alternate way of listing your own processes
- bg — put a process in the background
- fg — put a process in the foreground
A Practical Example
While it may seem that this subject is rather obscure, it can be very practical for the average user who mostly works with the graphical user interface. Though it might not be apparent, most (if not all) graphical programs can be launched from the command line. Here’s an example: there is a small program supplied with the X Window system called xload which displays a graph representing system load. We can execute this program by typing the following:
Notice that the small xload window appears and begins to display the system load graph. On systems where xload is not available, try gedit instead. Notice also that our prompt did not reappear after the program launched. The shell is waiting for the program to finish before control returns. If we close the xload window, the xload program terminates and the prompt returns.
Putting a Program into the Background
Now, in order to make life a little easier, we are going to launch the xload program again, but this time we will put it in the background so that the prompt will return. To do this, we execute xload like this:
In this case, the prompt returned because the process was put in the background.
Now imagine that we forgot to use the «&» symbol to put the program into the background. There is still hope. We can type Ctrl-z and the process will be suspended. We can verify this by seeing that the program’s window is frozen. The process still exists, but is idle. To resume the process in the background, type the bg command (short for background). Here is an example:
Listing Running Processes
Now that we have a process in the background, it would be helpful to display a list of the processes we have launched. To do this, we can use either the jobs command or the more powerful ps command.
Killing a Process
Suppose that we have a program that becomes unresponsive; how do we get rid of it? We use the kill command, of course. Let’s try this out on xload . First, we need to identify the process we want to kill. We can use either jobs or ps , to do this. If we use jobs we will get back a job number. With ps , we are given a process id (PID). We will do it both ways:
A Little More About kill
While the kill command is used to «kill» processes, its real purpose is to send signals to processes. Most of the time the signal is intended to tell the process to go away, but there is more to it than that. Programs (if they are properly written) listen for signals from the operating system and respond to them, most often to allow some graceful method of terminating. For example, a text editor might listen for any signal that indicates that the user is logging off, or that the computer is shutting down. When it receives this signal, it could save the work in progress before it exits. The kill command can send a variety of signals to processes. Typing:
will print a list of the signals it supports. Many are rather obscure, but several are handy to know:
Signal # | Name | Description |
1 | SIGHUP | Hang up signal. Programs can listen for this signal and act upon it. This signal is sent to processes running in a terminal when you close the terminal. |
2 | SIGINT | Interrupt signal. This signal is given to processes to interrupt them. Programs can process this signal and act upon it. We can also issue this signal directly by typing Ctrl-c in the terminal window where the program is running. |
15 | SIGTERM | Termination signal. This signal is given to processes to terminate them. Again, programs can process this signal and act upon it. This is the default signal sent by the kill command if no signal is specified. |
9 | SIGKILL | Kill signal. This signal causes the immediate termination of the process by the Linux kernel. Programs cannot listen for this signal. |
Now let’s suppose that we have a program that is hopelessly hung and we want to get rid of it. Here’s what we do:
- Use the ps command to get the process id (PID) of the process we want to terminate.
- Issue a kill command for that PID.
- If the process refuses to terminate (i.e., it is ignoring the signal), send increasingly harsh signals until it does terminate.
In the example above we used the ps command with the x option to list all of our processes (even those not launched from the current terminal). In addition, we piped the output of the ps command into grep to list only list the program we are interested in. Next, we used kill to issue a SIGTERM signal to the troublesome program. In actual practice, it is more common to do it in the following way since the default signal sent by kill is SIGTERM and kill can also use the signal number instead of the signal name:
Then, if the process does not terminate, force it with the SIGKILL signal:
That’s It!
This concludes the «Learning the Shell» series of lessons. In the next series, «Writing Shell Scripts,» we will look at how to automate tasks with the shell.
Further Reading
- For a more in-depth treatment of the topic, see Chapter 10 in The Linux Command Line.
- 1963 Timesharing: A Solution to Computer Bottlenecks, a fascinating YouTube video from the Computer History Museum describing the first timesharing operating system and how it works. It’s basically the same method used by all modern computers.
© 2000-2021, William E. Shotts, Jr. Verbatim copying and distribution of this entire article is permitted in any medium, provided this copyright notice is preserved.
Linux® is a registered trademark of Linus Torvalds.
Источник
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 :
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.
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 .
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
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
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
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
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 .
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
Let’s take an example and bring our gedit job to the 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 .
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
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
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.
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.
Источник
All You Need To Know About Processes in Linux [Comprehensive Guide]
In this article, we will walk through a basic understanding of processes and briefly look at how to manage processes in Linux using certain commands.
A process refers to a program in execution; it’s a running instance of a program. It is made up of the program instruction, data read from files, other programs or input from a system user.
Types of Processes
There are fundamentally two types of processes in Linux:
- Foreground processes (also referred to as interactive processes) – these are initialized and controlled through a terminal session. In other words, there has to be a user connected to the system to start such processes; they haven’t started automatically as part of the system functions/services.
- Background processes (also referred to as non-interactive/automatic processes) – are processes not connected to a terminal; they don’t expect any user input.
What is Daemons
These are special types of background processes that start at system startup and keep running forever as a service; they don’t die. They are started as system tasks (run as services), spontaneously. However, they can be controlled by a user via the init process.
Linux Process State
Creation of a Processes in Linux
A new process is normally created when an existing process makes an exact copy of itself in memory. The child process will have the same environment as its parent, but only the process ID number is different.
There are two conventional ways used for creating a new process in Linux:
- Using The System() Function – this method is relatively simple, however, it’s inefficient and has significantly certain security risks.
- Using fork() and exec() Function – this technique is a little advanced but offers greater flexibility, speed, together with security.
How Does Linux Identify Processes?
Because Linux is a multi-user system, meaning different users can be running various programs on the system, each running instance of a program must be identified uniquely by the kernel.
And a program is identified by its process ID (PID) as well as it’s parent processes ID (PPID), therefore processes can further be categorized into:
- Parent processes – these are processes that create other processes during run-time.
- Child processes – these processes are created by other processes during run-time.
The Init Process
Init process is the mother (parent) of all processes on the system, it’s the first program that is executed when the Linux system boots up; it manages all other processes on the system. It is started by the kernel itself, so in principle it does not have a parent process.
The init process always has process ID of 1. It functions as an adoptive parent for all orphaned processes.
You can use the pidof command to find the ID of a process:
Find Linux Process ID
To find the process ID and parent process ID of the current shell, run:
Find Linux Parent Process ID
Starting a Process in Linux
Once you run a command or program (for example cloudcmd – CloudCommander), it will start a process in the system. You can start a foreground (interactive) process as follows, it will be connected to the terminal and a user can send input it:
Start Linux Interactive Process
Linux Background Jobs
To start a process in the background (non-interactive), use the & symbol, here, the process doesn’t read input from a user until it’s moved to the foreground.
Start Linux Process in Background
You can also send a process to the background by suspending it using [Ctrl + Z] , this will send the SIGSTOP signal to the process, thus stopping its operations; it becomes idle:
To continue running the above-suspended command in the background, use the bg command:
To send a background process to the foreground, use the fg command together with the job ID like so:
Linux Background Process Jobs
States of a Process in Linux
During execution, a process changes from one state to another depending on its environment/circumstances. In Linux, a process has the following possible states:
- Running – here it’s either running (it is the current process in the system) or it’s ready to run (it’s waiting to be assigned to one of the CPUs).
- Waiting – in this state, a process is waiting for an event to occur or for a system resource. Additionally, the kernel also differentiates between two types of waiting processes; interruptible waiting processes – can be interrupted by signals and uninterruptible waiting processes – are waiting directly on hardware conditions and cannot be interrupted by any event/signal.
- Stopped – in this state, a process has been stopped, usually by receiving a signal. For instance, a process that is being debugged.
- Zombie – here, a process is dead, it has been halted but it’s still has an entry in the process table.
How to View Active Processes in Linux
There are several Linux tools for viewing/listing running processes on the system, the two traditional and well known are ps and top commands:
1. ps Command
It displays information about a selection of the active processes on the system as shown below:
List Linux Active Processes
2. top – System Monitoring Tool
List Linux Running Processes
Read this for more top usage examples: 12 TOP Command Examples in Linux
3. glances – System Monitoring Tool
glances is a relatively new system monitoring tool with advanced features:
Glances – Linux Process Monitoring
There are several other useful Linux system monitoring tools you can use to list active processes, open the link below to read more about them:
How to Control Processes in Linux
Linux also has some commands for controlling processes such as kill, pkill, pgrep and killall, below are a few basic examples of how to use them:
Control Linux Processes
To learn how to use these commands in-depth, to kill/terminate active processes in Linux, open the links below:
Note that you can use them to kill unresponsive applications in Linux when your system freezes.
Sending Signals To Processes
The fundamental way of controlling processes in Linux is by sending signals to them. There are multiple signals that you can send to a process, to view all the signals run:
List All Linux Signals
To send a signal to a process, use the kill, pkill or pgrep commands we mentioned earlier on. But programs can only respond to signals if they are programmed to recognize those signals.
And most signals are for internal use by the system, or for programmers when they write code. The following are signals which are useful to a system user:
- SIGHUP 1 – sent to a process when its controlling terminal is closed.
- SIGINT 2 – sent to a process by its controlling terminal when a user interrupts the process by pressing [Ctrl+C] .
- SIGQUIT 3 – sent to a process if the user sends a quit signal [Ctrl+D] .
- SIGKILL 9 – this signal immediately terminates (kills) a process and the process will not perform any clean-up operations.
- SIGTERM 15 – this a program termination signal (kill will send this by default).
- SIGTSTP 20 – sent to a process by its controlling terminal to request it to stop (terminal stop); initiated by the user pressing [Ctrl+Z] .
The following are kill commands examples to kill the Firefox application using its PID once it freezes:
To kill an application using its name, use pkill or killall like so:
Changing Linux Process Priority
On the Linux system, all active processes have a priority and certain nice value. Processes with higher priority will normally get more CPU time than lower priority processes.
However, a system user with root privileges can influence this with the nice and renice commands.
From the output of the top command, the NI shows the process nice value:
List Linux Running Processes
Use the nice command to set a nice value for a process. Keep in mind that normal users can attribute a nice value from zero to 20 to processes they own.
Only the root user can use negative nice values.
To renice the priority of a process, use the renice command as follows:
Check out our some useful articles on how to manage and control Linux processes.
That’s all for now! Do you have any questions or additional ideas, share them with us via the feedback form below.
If You Appreciate What We Do Here On TecMint, You Should Consider:
TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
We are thankful for your never ending support.
Источник