linux系统管理之进程管理(连载)

进程管理

积土为山,积水为海。

关于进程 process=======================================================

What is a process?  什么是进程
Process states  进程状态

1.1. 什么是进程?

进程是已启动的可执行程序的运行实例
程序: 二进制文件,静态 /bin/date, /usr/sbin/httpd,/usr/sbin/sshd, /usr/local/nginx/sbin/nginx
进程: 是程序运行的过程, 动态,有生命周期及运行状态。

进程状态

在多任务处理操作系统中,每个CPU(或核心)在一个时间点上只能处理一个进程。在进程运行时,它对CPU 时间和资源分配的要求会不断变化,从而为进程分配一个状态,它随着环境要求而改变。

1.2 .查看进程 process

静态查看进程

[root@linux-server ~]# ps aux | less
参数解释:
ps :process nsapashot
a 只能查看系统里面运行的所有终端进程
u 显示进程拥有者
x 显示系统内所有进程
f 显示进程之间的父子关系
o 指定显示的字段
-------------------
less: 可以上下翻页

USER:   #运行进程的用户
PID:    #进程ID
%CPU:  #CPU占用率
%MEM: #内存占用率
VSZ  进程占用的虚拟内存大小
RSS  占用的物理内存大小
STAT:   #进程状态    man ps (/STATE)
?    表示没有占用终端
R   运行
S   可中断睡眠 Sleep
D   不可中断睡眠 (usually IO)
T   停止的进程 
Z   僵尸进程
X    死掉的进程
START:  #进程的启动时间
TIME:   #进程占用CPU的总时间
COMMAND: #进程文件,进程名
查看tty的方法:
[root@linux-server ~]# tty
? 表示这个进程开启的时候没有占用终端

查看进程(二)

[root@linux-server ~]# ps -ef
参数解释:
-e 显示所有进程
-l 长格式显示
-f 完整格式

按指定字段排序

[root@linux-server ~]# ps aux --sort %cpu | less  #从小到大
[root@linux-server ~]# ps aux --sort -%cpu | less #从大到小
--sort:排序

查看单个PID

[root@linux-server ~]# yum install -y httpd  #安装apache软件
[root@linux-server ~]# systemctl start httpd #启动
[root@linux-server ~]# cat /var/run/sshd.pid 
104

查看指定PID

[root@linux-server ~]# ps aux | grep sshd
root       1043  0.0  0.2 105996  4120 ?        Ss   01:32   0:00 /usr/sbin/sshd -D
grep:过滤

查看端口

[root@linux-server ~]# yum install lsof  #安装软件包
[root@linux-server ~]# lsof -i:80   #端口号,这能查看带端口的进程
COMMAND   PID   USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
httpd   64249   root    4u  IPv6 1373628      0t0  TCP *:http (LISTEN)
#查网络进程和正在监听的端口
[root@linux-server ~]# netstat -lntp
参数详解:
-a  显示全部的进程
-u  显示udp
-n  以数字的新式显示协议名称
-t   tcp
-p:显示进程的名称和pid
-l :只显示正在被监听的端口
[root@linux-server ~]# w  #看已经登陆到终端的进程信息,远程登陆会有ip 地址

动态查看进程

成功的路上并不拥挤,因为坚持的人不多

top、htop

[root@linux-server ~]# top  #动态显示信息,三秒刷新一次。

在工作中必须监控的东西 load average(平均负载)等待cpu处理的队列长度 也是个数。
1分钟   第一个数字
5分钟   第二个数字
15分钟  第三个数字
======================
计算cpu负载:load average的三个值: 0.10, 0.16, 0.12,分别除cpu的个数,得出的值,如果值大于1那么那时候的负载高。

top操作

[root@linux-server ~]# top
h|? 帮助
> 往下翻页
< 往上翻页
M 按内存排序
P 按cpu排序
q 退出   
z 彩色显示
W 保存
=============================
PR   优先级
进程状态了解
Sl  以线程的方式运行
Ss  s进程的领导者,父进程
R+  +表示是前台的进程组
S< <优先级较高的进程    
SN  N优先级较低的进程

ni :nice值
id: cpu空闲率
wa:cpu等待,如果使用率过高,表示硬盘该换了

进程控制

按pid杀死进程

kill,pkill
语法: kill 信号 PID   #信号也是进程间通信的一种方式
[root@linux-server ~]# kill -l   #查看所有信号
-1   HUP  重新加载进程或者重新加载配置文件,PID不变
-9   KILL 强制杀死
-15  TERM 正常杀死(这个信号可以默认不写)
-18  CONT 激活进程
-19  STOP 挂起进程

案例一

给vsftpd进程发送信号1,15 vsftpd信号测试

[root@linux-server ~]# yum install -y vsftpd  #安装vsftpd
[root@linux-server ~]# systemctl start vsftpd  #启动
[root@linux-server ~]# ps aux | grep vsftpd
root      59363  0.0  0.0  53212   576 ?        Ss   16:47   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
​
[root@linux-server ~]# kill -1 59363  #发送重启信号,例如vsftpd的配置文件发生改变,希望重新加载
[root@linux-server ~]# ps aux | grep vsftpd
root      59363  0.0  0.0  53212   748 ?        Ss   16:47   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
[root@linux-server ~]# kill 59363 #正常杀死进程,信号为-15可以默认不写。我们可以使用systemctl stop vsftpd 停止服务。
[root@linux-server ~]# ps aux | grep vsftpd
root      62493  0.0  0.0 112660   968 pts/0    S+   16:51   0:00 grep --color=auto vsftpd
​
参数解释:
+:表示运行在前台的进程组
S+:休眠状态
T+:暂停,挂起状态
s:父进程

案例二

给vsftpd进程发送信号-9, vsftpd信号测试

[root@linux-server ~]# systemctl start vsftpd
[root@linux-server ~]# ps aux | grep vsftpd
root      67003  0.0  0.0  53212   572 ?        Ss   16:57   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
root      67089  0.0  0.0 112660   972 pts/0    S+   16:57   0:00 grep --color=auto vsftpd
[root@linux-server ~]# kill -9 67003  #强制杀死,一般用于不能正常停止的情况下
[root@linux-server ~]# ps aux | grep vsftpd
root      67190  0.0  0.0 112660   972 pts/0    S+   16:57   0:00 grep --color=auto vsftpd

案例三

使用pkill 杀死vsftpd进程

[root@linux-server ~]# systemctl start vsftpd
[root@linux-server ~]# ps -aux | grep vsftpd
root      73399  0.0  0.0  53212   572 ?        Ss   17:05   0:00 /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
root      73499  0.0  0.0 112660   968 pts/0    S+   17:05   0:00 grep --color=auto vsftpd
[root@linux-server ~]# pkill -9 vsftpd  #使用pkill可以指定进程名字
[root@linux-server ~]# ps -aux | grep vsftpd
root      73643  0.0  0.0 112660   968 pts/0    S+   17:05   0:00 grep --color=auto vsftpd
​

作业

信号测试18,19
[root@linux-server ~]# ps aux | grep sshd 
root       1043  0.0  0.2 105996  4120 ?        Ss   01:32   0:00 /usr/sbin/sshd -D
[root@linux-server ~]# kill -19 1043   #将进程挂起
[root@linux-server ~]# ps aux | grep sshd 
root       1043  0.0  0.2 105996  4120 ?        Ts   01:32   0:00 /usr/sbin/sshd -D
[root@linux-server ~]# kill -18 1043   #将进程激活
[root@linux-server ~]# ps aux | grep sshd 
root       1043  0.0  0.2 105996  4120 ?        Ss   01:32   0:00 /usr/sbin/sshd -D

进程优先级 nice

nice 值越高:表示优先级越低,例如+19,该进程容易将CPU 使用量让给其他进程。
nice 值越低:表示优先级越高,例如-20,该进程更不倾向于让出CPU。

查看进程优先级

[root@linux-server ~]# ps axo pid,command,nice  --sort=-nice | less
o:指定字段

或者

[root@linux-server ~]# top

NI: 实际nice级别
PR: 将nice级别显示为映射到更大优先级队列,-20映射到0,+19映射到39

实战案例

指定进程优先级

[root@linux-server ~]# nice --15 vim &

修改进程优先级

[root@linux-server ~]# ps -ef | grep vim 
root      92485  28819  0 17:30 pts/0    00:00:00 vim
root      94300  28819  0 17:32 pts/0    00:00:00 grep --color=auto vim
[root@linux-server ~]# renice 10 92485
92485 (process ID) old priority -15, new priority 10

作业控制

作业控制之jobs:

实战案例

[root@linux-server ~]# vim a.txt &   #放后台运行
[1] 101839
[root@linux-server ~]# jobs  #查看工作号
[1]-  Stopped                 vim a.txt
[root@linux-server ~]# fg %1  #把程序调到前台,%是用来修饰job number,1就是job number。(程序的工作号)
# ctrl+z  #把程序放到后台(这方法会让程序在后台暂停)
[root@linux-server ~]# bg %1  #让暂停的程序在后台运行
[1]+  Stopped                 vim a.txt
[root@linux-server ~]# kill -9 %1 #杀死程序
[1]+  Stopped                 vim a.txt
[root@linux-server ~]# jobs  #再次查看为空

tcpwrapper---访问控制工具

1.tcp wrapper是一种访问控制工具是操作系统自带的,类似于防火墙(iptables)可以作访问控制。
2.针对系统进程来做限制的
============================
#TCPwrapper配置
TCPwrapper有两个配置文件。
1./etc/hosts.allow   --允许
2./etc/hosts.deny    --拒绝
TCPwrappers先查找/etc/hosts.allow,再查找/etc/hosts.deny,如果两个配置中有冲突,先匹配中的优先,也就是hosts.allow中的配置优先,如果两个配置都没命中,默认放行。

TCPwrapper作用范围

tcpwarpper要看该应用是否依赖libwrap.so这个库文件。
例如tcpwrapper可以控制ssh服务,因为实现ssh协议的sshd程序依赖了libwarp.so库文件,

实战环境

准备两台机器:
wrap-server----192.168.246.188
test-1----192.168.246.158
#所有机器关闭防火墙和selinux
[root@localhost ~]# systemctl stop firewalld  #关闭防火墙
[root@localhost ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@localhost ~]# setenforce 0 #临时关闭selinux
1.查看某一个程序是否支持tcpwrapper
[root@wrap-server ~]# ldd `which sshd` | grep wrap
libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f9ec5c26000)
[root@wrap-server ~]# ldd `which httpd` | grep wrap  #httpd就不用,所以不支持
[root@wrap-server ~]# 

实战案例:

1.允许192.168.246.158这台机器访问我的服务器,其他禁止掉

[root@wrap-server ~]# vim /etc/hosts.allow  #在文件后面新增一行
sshd:192.168.246.158
[root@wrap-server ~]# vim /etc/hosts.deny  #在文件后面新增一行
sshd:ALL       #拒绝所有
===========================
sshd:192.168.246.159 #拒绝某个IP

测试:

1.首先我们用192.168.246.158的机器去ssh连接
[root@web-1 ~]# ssh 192.168.246.188
[root@wrap-server ~]#
2.在用其他机器连接
1.通过finalshell连接。  ---连接拒绝
2.或者通过第三台机器去连接
[root@test-2 ~]# ssh 192.168.246.188
ssh_exchange_identification: read: Connection reset by peer  #拒绝连接

常用命令

1.查看当前CPU负载

[root@linux-server ~]# uptime 
 17:35:01 up 16:02,  3 users,  load average: 0.00, 0.02, 0.05

2.查看内存使用

[root@linux-server ~]# free -m 
              total        used        free      shared  buff/cache   available
Mem:           1984         154        1508           8         321        1632
Swap:          2047           0        2047
​
更多帮助
[root@linux-server ~]# free --help

3.linux启动过程

1加电,2加载bios设置,3加载grub,4加载内核系统到内存当中,
5加载配置文件,6加载内核模块,7完成相应的初始化工作和启动相应的服务,
8启动系统进程,9出现登录界面,10开机启动完成

4.查看系统的版本和内核

[root@linux-server ~]# cat /etc/redhat-release  #查看版本
CentOS Linux release 7.4.1708 (Core)
[root@linux-server ~]# uname -a #看查正在运行的内核版本
Linux linux-server 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
[root@linux-server ~]# uname -r  #查看内核版本
3.10.0-693.el7.x86_64

5.修改主机名

[root@linux-server ~]# hostnamectl set-hostname  xxxx   #主机名,修改完之后断开与终端连接,然后在重新连接即可。
查看主机名
[root@linux-server ~]# hostname

猜你喜欢

转载自blog.csdn.net/qfxulei/article/details/108448520