Linux practical instruction (9) - process management

Process Management

Basic introduction of process

1) In LINUX, the program (codes) of each execution is called a process. Each process is assigned an ID number.

2) Each process will correspond to a parent, but the parent process can be replicated multiple sub-processes. For example, www server.

3) Each process may exist in two ways. Front and back, called the front desk is currently operating on the user's screen. This is the actual background process in operation, but the process can not be seen on the screen, usually performed using a background mode

4) general system services are the future there is a background process, and will be resident in the system, until the shutdown ended


2 shows the process performed by the system

2.1 Description:

See instructions using ps, the parameters used are generally ps-aux

ps information displayed options:

Field Explanation
PID Process identification number
TTY Terminal No.
TIME This elimination process of the CPU time
CMD Command being executed or process name
ps -a:显示当前终端的所有进程信息
ps -u:以用户的格式显示进程信息
ps -x:显示后台进程运行的参数


2.2 ps Detailed instructions

1) command: ps -aux | grep xxx, for example, I see no sshd service

2) Instruction Description

  • System V style show
  • USER: User Name
  • PID: Process ID
  • Process CPU percentage of:% CPU
  • Percentage process takes Logistics memory:% MEM
  • VSZ: size occupied by the process virtual memory (unit: KB)
  • RSS: Process occupied physical memory size (unit: KB)
  • TT: terminal name, the abbreviation
  • STAT: process state, in which S- sleep, s- indicates that the process is a session leader of the process, N- indicates that the process has a lower priority than normal priority level, R- running, D- short-term wait, Z- zombie processes , T- tracked or the like is stopped.
  • STARTED: process start-up time
  • TIME: CPU time, that is, the total CPU time used by the process
  • COMMAND: start the process used commands and parameters, if too long will be truncated display

2.3 Application examples

Requirements: First of all the processes that are currently in full format, see the parent process.

  • ps -ef is a full-format displays all the processes that are currently
  • -e show all processes. -f full format.
  • ps -ef | grep xxx
  • Is BSD-style
  • UID: User ID
  • PID: Process ID
  • PPID: parent process ID
  • C: CPU for calculating the execution priority factor. A higher value is a CPU intensive process operation, reduce the execution priority; Smaller values ​​indicates that the process is I / O intensive operations, execution priority level will increase.
  • STIME: process start time
  • TTY: Complete terminal name
  • TIME: CPU time
  • CMD: Command to start the process and parameters used

Application: If we want to see the parent process ID of the sshd process is how much, how should the query

ps -ef | grep sshd

3 to terminate the process kill and killall

3.1 Introduction

If a process executed when half needs to be stopped, or has consumed a lot of system resources, then you can consider stopping the process, use the kill command to complete this task.

3.2 The basic syntax

kill [options] process ID (Function Description: kill the process by process ID)

killall process name (Function Description: Kill processes by name, and also supports wildcards, which due to the load becomes too large to be useful when the system is very slow)

3.3 Common Options

-9: Indicates forced to stop the process immediately

3.4 Best Practices:

Case 1: kicked off an illegal user login

Case 2: terminate the Telnet service sshd, at the appropriate time to restart sshd service again

Case 3: terminate multiple gedit editor [killall, to terminate the process by process name]

Case 4: Forces kill a terminal

kill -9 4090


4 Review process tree pstree

4.1 The basic syntax

pstree [Options], you can process information more intuitive point of view

4.2 Common options:

PID display process: -p

-u: display belongs to the user process

4.3 Application examples

Case 1: Make your tree displayed in the form of process pid

pstree -p

Case 2: Make your tree in the form of user id of the process

pstree -u    即可

5 服务管理

5.1 介绍

服务本质就是进程,但是是运行在后台的,通常都会监听某个端口,等待其他程序的请求,比如(mysql,sshd 防火墙等),因此我们又称为守护进程,是Linux中非常重要的知识点。【原理图】

5.2 service 管理指令

service 服务名 [start|stop|restart|reload|status]

在CentOS7.0后,不在使用service,而是systemctl

5.3 使用案例:

1)查看当前防火墙的状况,关闭防火墙和重启防火墙。

5.4 细节讨论:

1)关闭或者启用防火墙后,立即生效。[telent 测试 某个端口即可]

2) 这种方式只是临时生效,当重启系统后,还是回归以前对服务器的设置

如果希望设置某个服务自启动或者关闭永久生效,需要使用chkconfig指令。在5.8会详细讲解。

5.5 查看服务名

方式1:使用setp -> 系统服务 就可以看到。

方式2: /etc/init.d/服务名称

5.6 服务的运行级别(runlevel)

查看或者修改默认级别:

vi etc/inittab

Linux 系统有7种运行级别(runlevel):常用的是级别3和5

  • 运行级别0:系统停机状态,系统默认运行级别不能设为0,否则不能正常启动
  • 运行级别1:单用户工作状态,root权限,用于系统维护,禁止远程登录
  • 运行级别2:多用户状态(没有NFS),不支持网络
  • 运行级别3:完全的多用户状态(有NFS),登录后进入控制台命令模式
  • 运行级别4:系统未使用,保留
  • 运行级别5:X11控制台,登录后进入图形GUI模式
  • 运行级别6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动

5.7 开机的流程说明

5.8 chkconfig 指令

通过 chkconfig 命令可以给每个服务的各个运行级别设置自启动/关闭

基本语法

1)查看服务

chkconfig --list|grep xxx

2) chkconfig 服务名 --list

3)chkconfig --level 5 服务名 on/off

chkconfig --level 5 服务名 on/off

请将sshd服务在运行级别为5的情况下,不要自启动。


5.9 应用实例:

1) 案例1: 请显示当前系统所有服务的各个运行级别的运行状态

bash> chkconfig --list

2) 案例2 :请查看sshd 服务的运行状态

bash>service sshd status

3) 案例3 :将sshd服务在运行级别5下设置为不自动启动

bash> chkconfig --level 5 sshd off

4) 案例4 : 当运行级别为5时,关闭防火墙

bash > chkconfig --level 5 iptables off

5)案例5 :在所有运行级别下,关闭防火墙

bash > chkconfig iptables off

6) 案例6 : 在所有运行级别下,开启防火墙

bash > chkconfig iptables on


5.10 使用细节

1) chkconfig 重置设置服务后自启动或关闭,需要重启机器reboot才能生效


6 动态监控进程

6.1 介绍

top与ps命令很相似,它们都用来显示正在执行的进程。Top与ps最大的不同之处,在于Top在执行一段时间可以更新正在运行的进程。

6.2 基本语法

Top [选项]

6.3 选项说明

选项 功能
-d 秒数 指定top命令每隔几秒更新,默认是3秒在top命令的交互模式当中可以执行的命令
-i 使top不显示任何闲置或者僵死进程
-p 通过指定监控进程ID来仅仅监控某个进程的状态

交互操作说明:

操作 功能
P 以CPU使用率排序,默认就是此项
M 以内存的使用率排序
N 以PID排序
q 退出top

6.4 应用实例:

案例1 : 监视特定用户

top:输入此命令,按回车键,查看执行的进程

u:然后输入“u” 回车,在输入用户名,即可

案例2 : 终止指定的进程

top: 输入此命令, 按回车键,查看执行的进程。

k:然后输入“k”回车,在输入要结束的进程ID号

案例3: 指定系统状态更新的时间(每隔10秒自动更新,默认为3秒)

bash> top -d 10

6.5 查看系统网络情况netstat (重要)

  • 基本语法
netstat [选项]
netstat -anp
  • 选项说明

-an 按移动顺序排列输出

-p 显示哪个进程在调用

  • 应用案例

查看系统所有的网络服务

netstat -anp | more

请查看服务名为sshd 的服务的信息

netstat anp | grep sshd

Guess you like

Origin www.cnblogs.com/jianshuai520/p/11450553.html