Linux 进程管理(重点)

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

Linux 进程管理重点

Linux中每个执行程序都被称为一个进程每一个进程都被分配一个ID号pid进程号

每一个进程都可能以两种方式存在前台与后台。

  • 前台进程就是用户目前屏幕上可以进行操作
  • 后台进程则是实际在操作但由于屏幕上无法看到的进程通常使用后台方式执行

一般系统的服务都是以后台进程方式存在而且会常驻在系统中直到关机才会结束

程序运行起来就是一个进程

  • 程序不运行时就是一个代码
  • 当程序运行起来并且被加载到内存中就是一个进程
  • 程序静态概念进程动态概念

显示系统执行的进程 ps

ps 命令用来查看当前系统中有哪些正在执行以及它们执行的状况可以不加参数

字段说明
PID进程号
TTY终端识别号
TIME此进程所消耗CPU时间
CMD正在执行的命令或进程名

常用的选项

  • ps -a 显示当前终端的所有进程信息
  • ps -u 以用户的格式显示进程信息
  • ps -x 显示后台进程运行的参数
[root@rootylq ~]# ps -aux | more
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.2  51784  4064 ?        Ss   Jan10   0:58 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root         2  0.0  0.0      0     0 ?        S    Jan10   0:00 [kthreadd]
root         4  0.0  0.0      0     0 ?        S<   Jan10   0:00 [kworker/0:0H]
root         5  0.0  0.0      0     0 ?        S    Jan10   0:01 [kworker/u4:0]
root         6  0.0  0.0      0     0 ?        S    Jan10   0:05 [ksoftirqd/0]
root         7  0.0  0.0      0     0 ?        S    Jan10   0:00 [migration/0]
root         8  0.0  0.0      0     0 ?        S    Jan10   0:00 [rcu_bh]

选项说明
USER该进程执行的用户
PID进程号
CPU占用CPU的百分比
MEM占用物理内存的百分比
VSZ虚拟内存的大小包含进程所能访问的所有内存
RSS常驻内存大小表示进程使用了多少内存
TTY终端信息
STAT当前运行状态S休眠 R: 运行 D:短期等待 Z:僵尸进程 T:被跟踪或停止
START进程开始时间
TIME表示进程占用CPU总时间
COMMAND进程名执行该进程的指令

显示需要查找的进程

[root@rootylq ~]# ps -aux |grep sshd
root      1280  0.0  0.2 113000  4332 ?        Ss   Jan10   0:00 /usr/sbin/sshd -D
root     21589  0.0  0.0 112812   980 pts/0    R+   15:53   0:00 grep --color=auto sshd
root     31046  0.0  0.3 158012  6096 ?        Ss   Jan11   0:00 sshd: root@pts/0

查看sshd的父进程信息

查看进程的详细信息

[root@rootylq ~]# ps -ef |grep sshd
root      1280     1  0 Jan10 ?        00:00:00 /usr/sbin/sshd -D
root     27661  1280  0 17:28 ?        00:00:00 sshd: root@pts/1
root     27704 27664  0 17:29 pts/1    00:00:00 grep --color=auto sshd
root     31046  1280  0 Jan11 ?        00:00:00 sshd: root@pts/0

终止进程 kill和kill all

若是某个进程执行一半需要停止时或是已经消耗了大量的系统资源时可以考虑停止该进程。使用kill命令来完成此任务

常用选项

-9: 表示强迫进程立刻停止

kill [选项] 进程号 通过进程号杀死进程

killall 进程名 通过进程名称杀死进程也支持通配符这在系统过大而变得缓慢时将有用。灭门

踢掉某个非法用户登录

[root@rootylq ~]# ps -ef |grep sshd
root      1280     1  0 Jan10 ?        00:00:00 /usr/sbin/sshd -D
root     27661  1280  0 17:28 ?        00:00:00 sshd: root@pts/1
root     28443  1280  0 17:40 ?        00:00:00 sshd: ylq [priv]
ylq      28447 28443  0 17:40 ?        00:00:00 sshd: ylq@pts/2
root     28521 27664  0 17:41 pts/1    00:00:00 grep --color=auto sshd
root     31046  1280  0 Jan11 ?        00:00:00 sshd: root@pts/0
[root@rootylq ~]# kill 28443
[root@rootylq ~]# 

终止远程服务并在恰当的时机进行重启

[root@rootylq ~]# kill 1280
[root@rootylq ~]# ps -ef |grep sshd
root     27661     1  0 17:28 ?        00:00:00 sshd: root@pts/1
root     28683 27664  0 17:43 pts/1    00:00:00 grep --color=auto sshd
root     31046     1  0 Jan11 ?        00:00:00 sshd: root@pts/0
[root@rootylq ~]# /bin/systemctl start sshd.service
[root@rootylq ~]# ps -ef |grep sshd
root     27661     1  0 17:28 ?        00:00:00 sshd: root@pts/1
root     28778     1  0 17:44 ?        00:00:00 /usr/sbin/sshd -D
root     28783 27664  0 17:44 pts/1    00:00:00 grep --color=auto sshd
root     31046     1  0 Jan11 ?        00:00:00 sshd: root@pts/0

终止多个gedit

killall gedit

强制杀死一个终端

kill -9 进程号

查看进程树 pstree

  • -p :显示进程的PID
  • -u :显示进程的所属用户
pstree -p

Linux 服务管理

服务本质就是进程是运行在后台通常都会监听某个端口等待其他程序的请求比如mysqlsshd 防火墙等因此我们又称其未守护进程是Linux中非常中的知识点

service 管理指令

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

centos 7.0 之后很多服务使用systemctl

service 指令管理的服务在/etc/init.d 中查看

[root@rootylq init.d]# ll
total 52
-rwxr-xr-x 1 root root 11776 Jan 10 14:35 bt
-rw-r--r-- 1 root root 18281 May 22  2020 functions
-rwxr-xr-x 1 root root  4569 May 22  2020 netconsole
-rwxr-xr-x 1 root root  7928 May 22  2020 network
-rw-r--r-- 1 root root  1160 Sep  1 22:57 README
[root@rootylq init.d]# 


[root@rootylq init.d]# service network status
Configured devices:
lo eth0
Currently active devices:
lo eth0
[root@rootylq init.d]# 

Linux 运行级别

运行级别说明

  • 0关机
  • 1单用户找回丢失密码
  • 2多用户状态但没有网络服务
  • 3多用户状态有网络服务
  • 4系统未使用保留给用户
  • 5图形界面
  • 6系统重启

Linux中常用的运行级别是3和5同时也可以指定默认运行级别

Linux系统开机的流程

开机->bios->/boot->systemd进程1->运行级别->运行级别对应的服务

[root@rootylq init.d]# systemctl get-default
multi-user.target

chkconfig指令

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

  • 查看服务 chkconfig --list [|grep xxx]
  • chkconfig 服务名 --list
  • chkconfig --level 5 服务名 on/off

chkconfig --level 5 network on

[root@rootylq init.d]# chkconfig --list

Note: This output shows SysV services only and does not include native
      systemd services. SysV configuration data might be overridden by native
      systemd configuration.

      If you want to list systemd services use 'systemctl list-unit-files'.
      To see services enabled on particular target use
      'systemctl list-dependencies [target]'.

bt             	0:off	1:off	2:on	3:on	4:on	5:on	6:off
netconsole     	0:off	1:off	2:off	3:off	4:off	5:off	6:off
network        	0:off	1:off	2:on	3:on	4:on	5:on	6:off
[root@rootylq init.d]# 

systemctl管理指令

基本语法 systemctl [start|stop|restart|status] 服务名

systemctl指令管理的服务在/usr/lib/systemd/system 查看

查看当前服务状态

[root@rootylq ~]# ll /usr/lib/systemd/system |grep fire
-rw-r--r--  1 root root  657 Apr 28  2021 firewalld.service
[root@rootylq ~]# 

[root@rootylq ~]# systemctl list-unit-files |grep firewalld
firewalld.service                             enabled 
[root@rootylq ~]# 

设置是在linux运行级别中的3和5都能通用

设置服务开机自启动和关闭

[root@rootylq ~]# systemctl is-enabled firewalld
enabled

查看防火墙状态和关闭启动防火墙

[root@rootylq ~]# systemctl stop firewalld
[root@rootylq ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since Tue 2023-01-17 16:48:24 CST; 3s ago
     Docs: man:firewalld(1)
  Process: 5817 ExecStart=/usr/sbin/firewalld --nofork --nopid $FIREWALLD_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 5817 (code=exited, status=0/SUCCESS)

Jan 10 15:41:17 rootylq systemd[1]: Stopped firewalld - dynamic firewall daemon.
Jan 10 15:41:17 rootylq systemd[1]: Starting firewalld - dynamic firewall daemon...
Jan 10 15:41:17 rootylq systemd[1]: Started firewalld - dynamic firewall daemon.
Jan 10 15:41:17 rootylq firewalld[5817]: WARNING: AllowZoneDrifting is enabled. This is consi...ow.
Jan 17 16:48:24 rootylq systemd[1]: Stopping firewalld - dynamic firewall daemon...
Jan 17 16:48:24 rootylq systemd[1]: Stopped firewalld - dynamic firewall daemon.
Hint: Some lines were ellipsized, use -l to show in full.
[root@rootylq ~]# systemctl start firewalld
[root@rootylq ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2023-01-17 16:50:11 CST; 2s ago
     Docs: man:firewalld(1)
 Main PID: 1002 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─1002 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid

Jan 17 16:50:11 rootylq systemd[1]: Starting firewalld - dynamic firewall daemon...
Jan 17 16:50:11 rootylq systemd[1]: Started firewalld - dynamic firewall daemon.
Jan 17 16:50:12 rootylq firewalld[1002]: WARNING: AllowZoneDrifting is enabled. This is consi...ow.
Hint: Some lines were 

关闭或启动防火墙指令会立即生效

这种方式主要是临时生效的当重启系统后还将回归之前对服务的设置

若希望设置永久有效需要使用systemctl [enable|disable] 服务名

firewall打开或关闭指定端口

[root@rootylq ~]# firewall-cmd --permanent --add-port=111/tcp
success
[root@rootylq ~]# firewall-cmd --reload
success
[root@rootylq ~]# firewall-cmd --query-port=111/tcp
yes
[root@rootylq ~]# firewall-cmd --permanent --remove-port=111/tcp
success
[root@rootylq ~]# firewall-cmd --reload
success
[root@rootylq ~]# firewall-cmd --query-port=111/tcp
no
[root@rootylq ~]# 

动态监控进程

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

top [选项]

  • -d 秒数 指定top命令每隔几秒更新默认3秒
  • -i 使top不显示任何限制或则僵尸进程
  • -p 通过指定监控进程PID来仅仅监控某个进程的状态

交互操作说明

  • P 以CPU使用率进行排序默认即为此项
  • M 以内存使用率排序
  • N 以pid排序
  • q 退出top

监控网络

查看系统网络状况netstat

netstat [选项]

  • -an 按一定顺序排列输出
  • -p 显示那个进程在调用
[root@rootylq ~]# netstat -anp |grep sshd
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      28778/sshd          
tcp        0      0 172.22.69.202:22        115.236.56.78:63460     ESTABLISHED 8950/sshd: ylq [pri 
tcp        0     36 172.22.69.202:22        115.236.56.78:52490     ESTABLISHED 27661/sshd: root@pt 
unix  3      [ ]         STREAM     CONNECTED     819367   28778/sshd           
unix  2      [ ]         DGRAM                    1742232  8950/sshd: ylq [pri  
unix  2      [ ]         DGRAM                    815681   27661/sshd: root@pt  
unix  3      [ ]         STREAM     CONNECTED     1742235  8953/sshd: ylq@pts/  
unix  3      [ ]         STREAM     CONNECTED     1742236  8950/sshd: ylq [pri  

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: linux