mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-03-12 01:40:10 +08:00
Merge pull request #5491 from ictlyh/master
Translated tech/20170331 All You Need To Know About Processes in Linu…
This commit is contained in:
commit
7aae11add8
@ -1,345 +0,0 @@
|
||||
ictlyh Translating
|
||||
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][9] 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.
|
||||
|
||||
[
|
||||

|
||||
][10]
|
||||
|
||||
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][11]; 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:
|
||||
|
||||
```
|
||||
# pidof systemd
|
||||
# pidof top
|
||||
# pidof httpd
|
||||
```
|
||||
[
|
||||

|
||||
][12]
|
||||
|
||||
Find Linux Process ID
|
||||
|
||||
To find the process ID and parent process ID of the current shell, run:
|
||||
|
||||
```
|
||||
$ echo $$
|
||||
$ echo $PPID
|
||||
```
|
||||
[
|
||||

|
||||
][13]
|
||||
|
||||
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:
|
||||
|
||||
```
|
||||
# cloudcmd
|
||||
```
|
||||
[
|
||||

|
||||
][14]
|
||||
|
||||
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.
|
||||
|
||||
```
|
||||
# cloudcmd &
|
||||
# jobs
|
||||
```
|
||||
[
|
||||

|
||||
][15]
|
||||
|
||||
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:
|
||||
|
||||
```
|
||||
# tar -cf backup.tar /backups/* #press Ctrl+Z
|
||||
# jobs
|
||||
```
|
||||
|
||||
To continue running the above-suspended command in the background, use the bg command:
|
||||
|
||||
```
|
||||
# bg
|
||||
```
|
||||
|
||||
To send a background process to the foreground, use the fg command together with the job ID like so:
|
||||
|
||||
```
|
||||
# jobs
|
||||
# fg %1
|
||||
```
|
||||
[
|
||||

|
||||
][16]
|
||||
|
||||
Linux Background Process Jobs
|
||||
|
||||
You may also like: [How to Start Linux Command in Background and Detach Process in Terminal][17]
|
||||
|
||||
#### 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][18] and [top][19] commands:
|
||||
|
||||
#### 1\. ps Command
|
||||
|
||||
It displays information about a selection of the active processes on the system as shown below:
|
||||
|
||||
```
|
||||
# ps
|
||||
# ps -e | head
|
||||
```
|
||||
[
|
||||

|
||||
][20]
|
||||
|
||||
List Linux Active Processes
|
||||
|
||||
#### 2\. top – System Monitoring Tool
|
||||
|
||||
[top is a powerful tool][21] that offers you a [dynamic real-time view of a running system][22] as shown in the screenshot below:
|
||||
|
||||
```
|
||||
# top
|
||||
```
|
||||
[
|
||||

|
||||
][23]
|
||||
|
||||
List Linux Running Processes
|
||||
|
||||
Read this for more top usage examples: [12 TOP Command Examples in Linux][24]
|
||||
|
||||
#### 3\. glances – System Monitoring Tool
|
||||
|
||||
glances is a relatively new system monitoring tool with advanced features:
|
||||
|
||||
```
|
||||
# glances
|
||||
```
|
||||
[
|
||||

|
||||
][25]
|
||||
|
||||
Glances – Linux Process Monitoring
|
||||
|
||||
For a comprehensive usage guide, read through: [Glances – An Advanced Real Time System Monitoring Tool for Linux][26]
|
||||
|
||||
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:
|
||||
|
||||
1. [20 Command Line Tools to Monitor Linux Performance][1]
|
||||
2. [13 More Useful Linux Monitoring Tools][2]
|
||||
|
||||
### 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:
|
||||
|
||||
```
|
||||
$ pgrep -u tecmint top
|
||||
$ kill 2308
|
||||
$ pgrep -u tecmint top
|
||||
$ pgrep -u tecmint glances
|
||||
$ pkill glances
|
||||
$ pgrep -u tecmint glances
|
||||
```
|
||||
[
|
||||

|
||||
][27]
|
||||
|
||||
Control Linux Processes
|
||||
|
||||
To learn how to use these commands in-depth, to kill/terminate active processes in Linux, open the links below:
|
||||
|
||||
1. [A Guide to Kill, Pkill and Killall Commands to Terminate Linux Processess][3]
|
||||
2. [How to Find and Kill Running Processes in Linux][4]
|
||||
|
||||
Note that you can use them to kill [unresponsive applications in Linux][28] 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:
|
||||
|
||||
```
|
||||
$ kill -l
|
||||
```
|
||||
[
|
||||

|
||||
][29]
|
||||
|
||||
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:
|
||||
|
||||
```
|
||||
$ pidof firefox
|
||||
$ kill 9 2687
|
||||
OR
|
||||
$ kill -KILL 2687
|
||||
OR
|
||||
$ kill -SIGKILL 2687
|
||||
```
|
||||
|
||||
To kill an application using its name, use pkill or killall like so:
|
||||
|
||||
```
|
||||
$ pkill firefox
|
||||
$ killall firefox
|
||||
```
|
||||
|
||||
#### 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:
|
||||
|
||||
```
|
||||
$ top
|
||||
```
|
||||
[
|
||||

|
||||
][30]
|
||||
|
||||
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:
|
||||
|
||||
```
|
||||
$ renice +8 2687
|
||||
$ renice +8 2103
|
||||
```
|
||||
|
||||
Check out our some useful articles on how to manage and control Linux processes.
|
||||
|
||||
1. [Linux Process Management: Boot, Shutdown, and Everything in Between][5]
|
||||
2. [Find Top 15 Processes by Memory Usage with ‘top’ in Batch Mode][6]
|
||||
3. [Find Top Running Processes by Highest Memory and CPU Usage in Linux][7]
|
||||
4. [How to Find a Process Name Using PID Number in Linux][8]
|
||||
|
||||
That’s all for now! Do you have any questions or additional ideas, share them with us via the feedback form below.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
|
||||
作者简介:
|
||||
|
||||
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/linux-process-management/
|
||||
|
||||
作者:[Aaron Kili][a]
|
||||
译者:[译者ID](https://github.com/译者ID)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/aaronkili/
|
||||
|
||||
[1]:http://www.tecmint.com/command-line-tools-to-monitor-linux-performance/
|
||||
[2]:http://www.tecmint.com/linux-performance-monitoring-tools/
|
||||
[3]:http://www.tecmint.com/how-to-kill-a-process-in-linux/
|
||||
[4]:http://www.tecmint.com/find-and-kill-running-processes-pid-in-linux/
|
||||
[5]:http://www.tecmint.com/rhcsa-exam-boot-process-and-process-management/
|
||||
[6]:http://www.tecmint.com/find-processes-by-memory-usage-top-batch-mode/
|
||||
[7]:http://www.tecmint.com/find-linux-processes-memory-ram-cpu-usage/
|
||||
[8]:http://www.tecmint.com/find-process-name-pid-number-linux/
|
||||
[9]:http://www.tecmint.com/dstat-monitor-linux-server-performance-process-memory-network/
|
||||
[10]:http://www.tecmint.com/wp-content/uploads/2017/03/ProcessState.png
|
||||
[11]:http://www.tecmint.com/linux-boot-process/
|
||||
[12]:http://www.tecmint.com/wp-content/uploads/2017/03/Find-Linux-Process-ID.png
|
||||
[13]:http://www.tecmint.com/wp-content/uploads/2017/03/Find-Linux-Parent-Process-ID.png
|
||||
[14]:http://www.tecmint.com/wp-content/uploads/2017/03/Start-Linux-Interactive-Process.png
|
||||
[15]:http://www.tecmint.com/wp-content/uploads/2017/03/Start-Linux-Process-in-Background.png
|
||||
[16]:http://www.tecmint.com/wp-content/uploads/2017/03/Linux-Background-Process-Jobs.png
|
||||
[17]:http://www.tecmint.com/run-linux-command-process-in-background-detach-process/
|
||||
[18]:http://www.tecmint.com/linux-boot-process-and-manage-services/
|
||||
[19]:http://www.tecmint.com/12-top-command-examples-in-linux/
|
||||
[20]:http://www.tecmint.com/wp-content/uploads/2017/03/ps-command.png
|
||||
[21]:http://www.tecmint.com/12-top-command-examples-in-linux/
|
||||
[22]:http://www.tecmint.com/bcc-best-linux-performance-monitoring-tools/
|
||||
[23]:http://www.tecmint.com/wp-content/uploads/2017/03/top-command.png
|
||||
[24]:http://www.tecmint.com/12-top-command-examples-in-linux/
|
||||
[25]:http://www.tecmint.com/wp-content/uploads/2017/03/glances.png
|
||||
[26]:http://www.tecmint.com/glances-an-advanced-real-time-system-monitoring-tool-for-linux/
|
||||
[27]:http://www.tecmint.com/wp-content/uploads/2017/03/Control-Linux-Processes.png
|
||||
[28]:http://www.tecmint.com/kill-processes-unresponsive-programs-in-ubuntu/
|
||||
[29]:http://www.tecmint.com/wp-content/uploads/2017/03/list-all-signals.png
|
||||
[30]:http://www.tecmint.com/wp-content/uploads/2017/03/top-command.png
|
||||
[31]:http://www.tecmint.com/author/aaronkili/
|
||||
[32]:http://www.tecmint.com/10-useful-free-linux-ebooks-for-newbies-and-administrators/
|
||||
[33]:http://www.tecmint.com/free-linux-shell-scripting-books/
|
@ -0,0 +1,343 @@
|
||||
关于 Linux 进程你所需要知道的一切【完全指南】
|
||||
============================================================
|
||||
|
||||
在这篇指南中,我们会逐步对进程做基本的了解,然后简要看看如何用特定命令[管理 Linux 进程][9]。
|
||||
|
||||
进程是指正在执行的程序;是程序正在运行的一个实例。它由程序指令,从文件、其它程序中读取的数据或系统用户的输入组成。
|
||||
|
||||
#### 进程的类型
|
||||
|
||||
在 Linux 中主要有两种类型的进程:
|
||||
|
||||
* 前台进程(也称为交互式进程) - 这些进程由终端会话初始化和控制。换句话说,需要有一个连接到系统中的用户来启动这样的进程;它们不是作为系统功能/服务的一部分自动启动。
|
||||
* 后台进程(也称为非交互式/自动进程) - 这些进程没有连接到终端;它们不需要任何用户输入。
|
||||
|
||||
#### 什么是守护进程
|
||||
|
||||
这是后台进程的特殊类型,它们在系统启动时启动,并作为服务一直运行;它们不会死亡。它们自发地作为系统任务启动(作为服务运行)。但是,它们能被用户通过 init 进程控制。
|
||||
|
||||
[
|
||||

|
||||
][10]
|
||||
|
||||
Linux 进程状态
|
||||
|
||||
### 在 Linux 中创建进程
|
||||
|
||||
当现有的进程在内存中完全拷贝一份自身的时候就会创建出一个新的进程。子进程会有和父进程一样的环境,只有进程 ID 不同。
|
||||
|
||||
在 Linux 中有两种常规方式创建进程:
|
||||
|
||||
* 使用 System() 函数 - 这个方法相对简单,但是比较低效而且具有明显的安全隐患。
|
||||
* 使用 fork() 和 exec() 函数 - 这个技巧比较高级但提供更好的灵活性、速度以及安全性。
|
||||
|
||||
### Linux 如何识别进程?
|
||||
|
||||
由于 Linux 是一个多用户系统,意味着不同的用户可以在系统上运行各种各样的程序,内核必须唯一标识程序运行的每个实例。
|
||||
|
||||
程序由它的进程 ID(PID)和它父进程的进程 ID(PPID)识别,因此进程可以被分类为:
|
||||
|
||||
* 父进程- 这些是在运行时创建其它进程的进程。
|
||||
* 子进程- 这些是在运行时由其它进程创建的进程。
|
||||
|
||||
#### init 进程
|
||||
|
||||
init 进程是系统中所有进程的父进程,它是[启动 Linux 系统][11]后第一个运行的程序;它管理着系统上的所有其它进程。它由内核自身启动,因此理论上说它没有父进程。
|
||||
|
||||
init 进程的进程 ID 总是为 1。它是所有孤儿进程的收养父母。(它会收养所有孤儿进程)。
|
||||
|
||||
你可以用 pidof 命令查找某个进程的进程 ID:
|
||||
|
||||
# pidof systemd
|
||||
# pidof top
|
||||
# pidof httpd
|
||||
|
||||
[
|
||||

|
||||
][12]
|
||||
|
||||
查找 Linux 进程 ID
|
||||
|
||||
要查找当前 shell 的进程 ID 以及它父进程的进程 ID,可以运行:
|
||||
|
||||
|
||||
$ echo $$
|
||||
$ echo $PPID
|
||||
|
||||
[
|
||||

|
||||
][13]
|
||||
|
||||
查找 Linux 父进程 ID
|
||||
|
||||
#### 在 Linux 中启动进程
|
||||
|
||||
每次你运行一个命令或程序(例如 cloudcmd - CloudCommander),它就会在系统中启动一个进程。你可以按照下面的方式启动一个前台(交互式)进程,它会被连接到终端,用户可以发送输入给它:
|
||||
|
||||
# cloudcmd
|
||||
|
||||
[
|
||||

|
||||
][14]
|
||||
|
||||
启动 Linux 交互进程
|
||||
|
||||
#### Linux 后台任务
|
||||
|
||||
要在后台(非交互式)启动一个进程,使用 `&` 符号,此时,该进程不会从用户中读取输入,直到它被移到前台。
|
||||
|
||||
|
||||
# cloudcmd &
|
||||
# jobs
|
||||
|
||||
[
|
||||

|
||||
][15]
|
||||
|
||||
在后台启动 Linux 进程
|
||||
|
||||
你也可以使用 `[Ctrl + Z]` 暂停执行一个程序并把它发送到后台,它会给进程发送 SIGSTOP 信号,从而暂停它的执行;它就会变为空闲:
|
||||
|
||||
|
||||
# tar -cf backup.tar /backups/* #press Ctrl+Z
|
||||
# jobs
|
||||
|
||||
|
||||
要在后台继续运行上面被暂停的命令,使用 bg 命令:
|
||||
|
||||
|
||||
# bg
|
||||
|
||||
|
||||
要把后台进程发送到前台,使用 fg 命令以及任务的 ID,类似:
|
||||
|
||||
|
||||
# jobs
|
||||
# fg %1
|
||||
|
||||
[
|
||||

|
||||
][16]
|
||||
|
||||
Linux 后台进程任务
|
||||
|
||||
你也可能想要阅读:[如何在后台启动 Linux 命令以及在终端分离(Detach)进程][17]
|
||||
|
||||
#### Linux 中进程的状态
|
||||
|
||||
在执行过程中,取决于它的环境一个进程会从一个状态转变到另一个状态。在 Linux 中,一个进程有下面的可能状态:
|
||||
|
||||
* Running - 此时它正在运行(它是系统中的当前进程)或准备运行(它正在等待分配 CPUs 单元)。
|
||||
* Waiting - 在这个状态,进程正在等待某个事件的发生或者系统资源。另外,内核也会区分两种不同类型的等待进程;可中断等待进程(interruptible waiting processes) - 可以被信号中断以及不可中断等待进程(uninterruptible waiting processes)- 正在等待硬件条件,不能被任何事件/信号中断。
|
||||
* Stopped - 在这个状态,进程已经被停止了,通常是由于收到了一个信号。例如,正在被调试的进程。
|
||||
* Zombie - 该进程已经死亡,它已经停止了但是进程表(process table)中仍然有它的条目。
|
||||
|
||||
#### 如何在 Linux 中查看活跃进程
|
||||
|
||||
有很多 Linux 工具可以用于查看/列出系统中正在运行的进程,两个传统众所周知的是 [ps][18] 和 [top][19] 命令:
|
||||
|
||||
#### 1\. ps 命令
|
||||
|
||||
它显示被选中的系统中活跃进程的信息,如下图所示:
|
||||
|
||||
|
||||
# ps
|
||||
# ps -e | head
|
||||
|
||||
[
|
||||

|
||||
][20]
|
||||
|
||||
列出 Linux 活跃进程
|
||||
|
||||
#### 2\. top - 系统监控工具
|
||||
|
||||
[top 是一个强大的工具][21],它能给你提供 [运行系统的动态实时视图][22],如下面截图所示:
|
||||
|
||||
|
||||
# top
|
||||
|
||||
[
|
||||

|
||||
][23]
|
||||
|
||||
列出 Linux 正在运行的程序
|
||||
|
||||
阅读这篇文章获取更多 top 使用事例:[Linux 中 12 个 top 命令事例][24]
|
||||
|
||||
#### 3\. glances - 系统监控工具
|
||||
|
||||
glances 是一个相对比较新的系统监控工具,它有一些比较高级的功能:
|
||||
|
||||
|
||||
# glances
|
||||
|
||||
[
|
||||

|
||||
][25]
|
||||
|
||||
Glances – Linux 进程监控
|
||||
|
||||
要获取完整使用指南,请阅读:[Glances - Linux 的一个高级实时系统监控工具][26]
|
||||
|
||||
还有很多你可以用来列出活跃进程的其它有用的 Linux 系统监视工具,打开下面的链接了解更多关于它们的信息:
|
||||
|
||||
1. [监控 Linux 性能的 20 个命令行工具][1]
|
||||
2. [13 个有用的 Linux 监控工具][2]
|
||||
|
||||
### 如何在 Linux 中控制进程
|
||||
|
||||
Linux 也有一些命令用于控制进程,例如 kill、pkill、pgrep 和 killall,下面是一些如何使用它们的基本事例:
|
||||
|
||||
|
||||
$ pgrep -u tecmint top
|
||||
$ kill 2308
|
||||
$ pgrep -u tecmint top
|
||||
$ pgrep -u tecmint glances
|
||||
$ pkill glances
|
||||
$ pgrep -u tecmint glances
|
||||
|
||||
[
|
||||

|
||||
][27]
|
||||
|
||||
控制 Linux 进程
|
||||
|
||||
想要深入了解如何使用这些命令,在 Linux 中杀死/终止活跃进程,可以点击下面的链接:
|
||||
|
||||
1. [终止 Linux 进程的 Kill、Pkill 和 Killall 命令指南][3]
|
||||
2. [如何在 Linux 中查找并杀死进程][4]
|
||||
|
||||
注意当你系统僵死(freeze)时你可以使用它们杀死 [Linux 中的不响应程序][28]。
|
||||
|
||||
#### 给进程发送信号
|
||||
|
||||
Linux 中控制进程的基本方法是给它们发送信号。你可以发送很多信号给一个进程,运行下面的命令可以查看所有信号:
|
||||
|
||||
```
|
||||
$ kill -l
|
||||
```
|
||||
[
|
||||

|
||||
][29]
|
||||
|
||||
列出所有 Linux 信号
|
||||
|
||||
要给一个进程发送信号,可以使用我们之前提到的 kill、pkill 或 pgrep 命令。但只有被编程为能识别这些信号时程序才能响应这些信号。
|
||||
|
||||
大部分信号都是系统内部使用,或者给程序员编写代码时使用。下面是一些对系统用户非常有用的信号:
|
||||
|
||||
* SIGHUP 1 - 当控制它的终端被被关闭时给进程发送该信号。
|
||||
* SIGINT 2 - 当用户使用 `[Ctrl+C]` 中断进程时控制它的终端给进程发送这个信号。
|
||||
* SIGQUIT 3 - 当用户发送退出信号 `[Ctrl+D]` 时给进程发送该信号。
|
||||
* SIGKILL 9 - 这个信号会马上中断(杀死)进程,进程不会进行清理操作。
|
||||
* SIGTERM 15 - 这是一个程序终止信号(kill 默认发送这个信号)。
|
||||
* SIGTSTP 20 - 它的控制终端发送这个信号给进程要求它停止(终端停止);通过用户按 `[Ctrl+Z]` 触发。
|
||||
|
||||
下面是当 Firefox 应用程序僵死时通过它的 PID 杀死它的 kill 命令事例:
|
||||
|
||||
|
||||
$ pidof firefox
|
||||
$ kill 9 2687
|
||||
OR
|
||||
$ kill -KILL 2687
|
||||
OR
|
||||
$ kill -SIGKILL 2687
|
||||
|
||||
|
||||
使用它的名称杀死应用,可以像下面这样使用 pkill 或 killall:
|
||||
|
||||
|
||||
$ pkill firefox
|
||||
$ killall firefox
|
||||
|
||||
|
||||
#### 更改 Linux 进程优先级
|
||||
|
||||
在 Linux 系统中,所有活跃进程都有一个优先级以及 nice 值。有比点优先级进程有更高优先级的进程一般会获得更多的 CPU 时间。
|
||||
|
||||
但是,有 root 权限的系统用户可以使用 nice 好 renice 命令影响(更改)优先级。
|
||||
|
||||
在 top 命令的输出中, NI 显示了进程的 nice 值:
|
||||
|
||||
|
||||
$ top
|
||||
|
||||
[
|
||||

|
||||
][30]
|
||||
|
||||
列出 Linux 正在运行的进程
|
||||
|
||||
使用 nice 命令为一个进程设置 nice 值。记住一个普通用户可以给他拥有的进程设置 0 到 20 的 nice 值。
|
||||
|
||||
只有 root 用户可以使用负的 nice 值。
|
||||
|
||||
要 renice 一个进程的优先级,像下面这样使用 renice 命令:
|
||||
|
||||
|
||||
$ renice +8 2687
|
||||
$ renice +8 2103
|
||||
|
||||
|
||||
阅读我们其它如何管理和控制 Linux 进程的有用文章。
|
||||
|
||||
1. [Linux 进程管理:启动、停止以及中间过程][5]
|
||||
2. [使用 ‘top’ 命令 Batch 模式查找内存使用最高的 15 个进程][6]
|
||||
3. [在 Linux 中查找内存和 CPU 使用率最高的进程][7]
|
||||
4. [在 Linux 中如何使用进程 ID 查找进程名称][8]
|
||||
|
||||
就是这些!如果你有任何问题或者想法,通过下面的反馈框和我们分享吧。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
|
||||
作者简介:
|
||||
|
||||
Aaron Kili 是一个 Linux 和 F.O.S.S(Free and Open-Source Software) 爱好者,一个 Linux 系统管理员、web 开发员,现在也是 TecMint 的内容创建者,他喜欢和电脑一起工作,他相信知识共享。
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
via: http://www.tecmint.com/linux-process-management/
|
||||
|
||||
作者:[Aaron Kili][a]
|
||||
译者:[ictlyh](https://github.com/ictlyh)
|
||||
校对:[校对者ID](https://github.com/校对者ID)
|
||||
|
||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||
|
||||
[a]:http://www.tecmint.com/author/aaronkili/
|
||||
|
||||
[1]:http://www.tecmint.com/command-line-tools-to-monitor-linux-performance/
|
||||
[2]:http://www.tecmint.com/linux-performance-monitoring-tools/
|
||||
[3]:http://www.tecmint.com/how-to-kill-a-process-in-linux/
|
||||
[4]:http://www.tecmint.com/find-and-kill-running-processes-pid-in-linux/
|
||||
[5]:http://www.tecmint.com/rhcsa-exam-boot-process-and-process-management/
|
||||
[6]:http://www.tecmint.com/find-processes-by-memory-usage-top-batch-mode/
|
||||
[7]:http://www.tecmint.com/find-linux-processes-memory-ram-cpu-usage/
|
||||
[8]:http://www.tecmint.com/find-process-name-pid-number-linux/
|
||||
[9]:http://www.tecmint.com/dstat-monitor-linux-server-performance-process-memory-network/
|
||||
[10]:http://www.tecmint.com/wp-content/uploads/2017/03/ProcessState.png
|
||||
[11]:http://www.tecmint.com/linux-boot-process/
|
||||
[12]:http://www.tecmint.com/wp-content/uploads/2017/03/Find-Linux-Process-ID.png
|
||||
[13]:http://www.tecmint.com/wp-content/uploads/2017/03/Find-Linux-Parent-Process-ID.png
|
||||
[14]:http://www.tecmint.com/wp-content/uploads/2017/03/Start-Linux-Interactive-Process.png
|
||||
[15]:http://www.tecmint.com/wp-content/uploads/2017/03/Start-Linux-Process-in-Background.png
|
||||
[16]:http://www.tecmint.com/wp-content/uploads/2017/03/Linux-Background-Process-Jobs.png
|
||||
[17]:http://www.tecmint.com/run-linux-command-process-in-background-detach-process/
|
||||
[18]:http://www.tecmint.com/linux-boot-process-and-manage-services/
|
||||
[19]:http://www.tecmint.com/12-top-command-examples-in-linux/
|
||||
[20]:http://www.tecmint.com/wp-content/uploads/2017/03/ps-command.png
|
||||
[21]:http://www.tecmint.com/12-top-command-examples-in-linux/
|
||||
[22]:http://www.tecmint.com/bcc-best-linux-performance-monitoring-tools/
|
||||
[23]:http://www.tecmint.com/wp-content/uploads/2017/03/top-command.png
|
||||
[24]:http://www.tecmint.com/12-top-command-examples-in-linux/
|
||||
[25]:http://www.tecmint.com/wp-content/uploads/2017/03/glances.png
|
||||
[26]:http://www.tecmint.com/glances-an-advanced-real-time-system-monitoring-tool-for-linux/
|
||||
[27]:http://www.tecmint.com/wp-content/uploads/2017/03/Control-Linux-Processes.png
|
||||
[28]:http://www.tecmint.com/kill-processes-unresponsive-programs-in-ubuntu/
|
||||
[29]:http://www.tecmint.com/wp-content/uploads/2017/03/list-all-signals.png
|
||||
[30]:http://www.tecmint.com/wp-content/uploads/2017/03/top-command.png
|
||||
[31]:http://www.tecmint.com/author/aaronkili/
|
||||
[32]:http://www.tecmint.com/10-useful-free-linux-ebooks-for-newbies-and-administrators/
|
||||
[33]:http://www.tecmint.com/free-linux-shell-scripting-books/
|
Loading…
Reference in New Issue
Block a user