In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to manage the Linux process with specific commands". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to manage the Linux process with specific commands".
A process is a program that is executing; it is an instance of a program that is running. It consists of program instructions, and data read from files or other programs, or input from system users.
Type of process
There are two main types of processes in Linux:
Foreground processes (also known as interactive processes)-these processes are initialized and controlled by terminal sessions. In other words, a user connected to the system is required to start such processes; they are not started automatically as part of the system function / service.
Background processes (also known as non-interactive / automatic processes)-these processes are not connected to the terminal; they do not require any user input.
What is a daemon (daemon)
This is a special type of background process that starts when the system starts and runs as a service; they do not die. They are started spontaneously as system tasks (running as services). However, they can be controlled by the user through the init process.
Linux process status
Create a process in Linux
(LCTT translation note: the original text of this section is inaccurate and will be re-provided according to the understanding of the translator)
There are three ways to create a process in Linux:
Fork () mode
Use the fork () function to copy a process based on the parent process, whose PID number is different from the parent process PID number. In the Linux environment, fork () is implemented by write replication, and the environment of the new child process is the same as the parent process, only the memory is different from the parent process, the others are shared with the parent process, and a copy is regenerated only after the parent process or child process has been modified.
System () mode
The system () function calls / bin/sh-c command to execute a specific command and blocks the execution of the current process until the command command finishes. The new child process will have a new PID.
Exec () mode
The exec () method has several different functions. Unlike the previous fork () and system () functions, exec () will replace the original process with a new process, the system will run from the new process, and the PID value of the new process will be the same as the PID value of the original process.
How does Linux identify the process?
Because Linux is a multi-user system, which means that different users can run a variety of programs on the system, the kernel must identify each instance of the program running.
A program is recognized by its process ID (PID) and its parent process ID (PPID), so processes can be classified as:
Parent processes-these are processes that create other processes at run time.
Child processes-these are processes created by other processes at run time.
Init process
The init process is the parent process of all processes in the system. It is the program that runs after starting the Linux system; it manages all other processes on the system. It is started by the kernel itself, so it theoretically has no parent process.
The process ID of the init process is always 1. It is the adoptive parent of all orphans. It will adopt all orphans in the process.
Find process ID
You can use the pidof command to find the process ID of a process:
# pidof systemd # pidof top # pidof httpd
Find Linux process ID
To find the process ID of the current shell and the process ID of its parent process, run:
$echo $echo $PPID
Find Linux parent process ID
Start a process in Linux
Every time you run a command or program (such as cloudcmd-CloudCommander), it starts a process on the system. You can start a foreground (interactive) process as follows, it will be connected to the terminal, and the user can send input to it:
# cloudcmd
Start the Linux interactive process
Linux background tasks
To start a process in the background (non-interactive), use the & symbol, where the process does not read input from the user until it is moved to the foreground.
# cloudcmd & # jobs
Start the Linux process in the background
You can also use Ctrl + Z to suspend the execution of a program and send it to the background. It will send a SIGSTOP signal to the process, thus suspending its execution; it will become idle:
# tar-cf backup.tar / backups/* # Press Ctrl+Z # jobs
To continue running the paused command above in the background, use the bg command:
# bg
To send the background process to the foreground, use the fg command and the ID of the task, similar to:
# jobs # fg 1
Linux background process task
Status of processes in Linux
During execution, depending on its environment, a process transitions from one state to another. In Linux, a process has the following possible states:
Running-at this time it is running (it is the current process in the system) or ready to run (it is waiting to allocate CPU units).
Waiting-in this state, the process is waiting for an event or system resources. In addition, the kernel distinguishes between two different types of waiting processes; interruptible waiting processes (interruptible waiting processes)-which can be interrupted by signals, and non-interruptible waiting processes (uninterruptible waiting processes)-are waiting for hardware conditions and cannot be interrupted by any event / signal.
Stopped-in this state, the process has been stopped, usually due to a signal received. For example, the process being debugged.
Zombie-the process is dead, it has stopped, but the process table still has its entry.
How to view active processes in Linux
There are many Linux tools that can be used to view / list processes running on a system, and two traditions are known as the ps and top commands:
1. Ps command
It displays information about the active processes in the selected system, as shown in the following figure:
# ps # ps-e | head
List Linux active processes
2. Top-system monitoring tool
Top is a powerful tool that provides you with a dynamic real-time view of the running system, as shown in the screenshot below:
# top
List the programs that Linux is running
3. Glances-system monitoring tool
Glances is a relatively new system monitoring tool with some advanced features:
# glances
Glances-Linux process monitoring
There are many other useful Linux system monitoring tools you can use to list active processes. Open the links below to learn more about them:
20 command-line tools for monitoring Linux performance
13 useful Linux monitoring tools
How to control the process in Linux
Linux also has some commands to control processes, such as kill, pkill, pgrep, and killall. Here are some basic examples of how to use them:
$pgrep-u tecmint top $kill 2308$ pgrep-u tecmint top $pgrep-u tecmint glances $pkill glances $pgrep-u tecmint glances
Control the Linux process
To learn more about how to use these commands to kill / terminate an active process in Linux, click the link below:
Guide to Kill, Pkill, and Killall commands for terminating Linux processes
How to find and kill processes in Linux
Note that you can use them to kill unresponsive programs in Linux when your system is freeze.
Send signals to the process
The basic way to control processes in Linux is to send signals to them. You can send a lot of signals to a process. Run the following command to see all the signals:
$kill-l
List all Linux signals
To signal a process, you can use the kill, pkill, or pgrep commands we mentioned earlier. But only if it is programmed to recognize these signals can the program respond to them.
Most of the signals are used within the system or when writing code for programmers. Here are some useful signals for system users:
SIGHUP 1-sends this signal to the process when the terminal that controls it is turned off.
SIGINT 2-when the user interrupts the process with Ctrl+C, the terminal that controls it sends this signal to the process.
SIGQUIT 3-sends the exit signal Ctrl+D to the process when the user sends it.
SIGKILL 9-this signal immediately interrupts (kills) the process, and the process does not clean up.
SIGTERM 15-this is a program termination signal (kill sends this signal by default).
SIGTSTP 20-its control terminal sends this signal to the process asking it to stop (terminal stops); triggered by the user pressing Ctrl+Z.
Here is an example of a kill command that kills a Firefox application through its PID when it freezes:
$pidof firefox $kill 9 2687 or $kill-KILL 2687 or $kill-SIGKILL 2687
To kill the application with its name, you can use pkill or killall as follows:
$pkill firefox $killall firefox
Change Linux process priority
In Linux systems, all active processes have a priority and a nice value. Processes that have higher priorities than point priority processes generally get more CPU time.
However, system users with root privileges can use the nice and renice commands to influence (change) the priority.
In the output of the top command, NI displays the nice value of the process:
$top
List the processes that Linux is running
Use the nice command to set the nice value for a process. Remember that an average user can set a nice value of 0 to 20 for the process he owns.
Only root users can use negative nice values.
To reset the priority of a process, use the renice command as follows:
$renice + 8 2687$ renice + 8 2103 Thank you for reading, the above is the content of "how to manage the Linux process with specific commands". After the study of this article, I believe you have a deeper understanding of how to manage the Linux process with specific commands, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.