- 在Linux中每个执行的程序(代码)都称为一个进程,每一个进程都分配一个ID号
- 每一个进程,都会对应一个父进程,而这个父进程科一复制多个子进程。例如www服务器
- 每个进程都可能以两种方式存在的,前台后台,所谓前台进程就是用户目前屏幕上可以进行操作的,后台进程则是实际在操作,但由于屏幕上无法看到的进程,通常用后台方式执行
- 一般系统的服务都是以后台进程的方式存在,而且都会常驻在系统中,知道关机才结束
基本介绍
ps
是用来查看目前系统,有哪些正在执行,以及他们执行的情况。可以不加任何参数
ps -a
:显示当前终端所有进程信息
ps -u
:以用户格式显示进程信息
ps -x
:显示后台进程运行的参数
ps -aux
:一起使用
ps -aux | more
:分页展示
ps -aux | grep xxxx
:筛选进程
ps -f
:全格式显示
ps -e
:显示所有进程
ps -ef
:查询父进程
pstree
:以树状结构显示进程
-p
:显示pid-u
:显示进程的所属用户
[root@localhost /]# ps -aux | more
USER(1) PID(2) %CPU(3) %MEM(4) VSZ(5) RSS(6) TTY(7) STAT(8) START(9) TIME(10) COMMAND(11)
root 1 0.0 0.3 193940 7072 ? Ss 22:17 0:04 /usr/lib/systemd/systemd --switched-root --system --deserialize 22
root 2 0.0 0.0 0 0 ? S 22:17 0:00 [kthreadd]
- 用户名
- 进程ID
- 占用CPU
- 占用内存
- 使用的虚拟内存(单位:kb)
- 使用的物理内存(单位:kb)
- 使用的终端
- 进程的状态:S(休眠)、R(运行)、N(表示进程拥有比普通优先级更低的优先级)、D(短期等待)、Z(僵死进程)、T(被跟踪或者被停止)
- 启动时间
- 占用CPU的总时间
- 进程启动时的命令和参数(过长会被截断显示)
终止进程kill和killall
基本语法
kill [选项] 进程号
:通过进程号杀死进程
killall 进程名称
:通过进程名杀死进程(可以使用通配符,在系统因负载过大而变得很慢时很有用)
选项
-9
:表示强迫进程立刻停止
最佳实例
-
踢掉某个非法登陆用户
[root@localhost ~]# ps -aux | grep sshd root 1292 0.0 0.2 112900 4320 ? Ss 21:51 0:00 /usr/sbin/sshd -D root 3609 0.2 0.3 160988 5640 ? Ss 22:05 0:00 sshd: root@pts/1 root 3720 1.9 0.2 160988 5576 ? Ss 22:06 0:00 sshd: jack [priv] jack 3724 0.0 0.1 160988 2380 ? S 22:07 0:00 sshd: jack@pts/2 root 3776 0.0 0.0 112828 976 pts/0 S+ 22:07 0:00 grep --color=auto sshd [root@localhost ~]# kill -9 3724 [root@localhost ~]# ps -aux | grep sshd root 1292 0.0 0.2 112900 4320 ? Ss 21:51 0:00 /usr/sbin/sshd -D root 3609 0.1 0.3 160988 5640 ? Ss 22:05 0:00 sshd: root@pts/1 root 3800 0.0 0.0 112828 976 pts/0 S+ 22:07 0:00 grep --color=auto sshd
-
终止远程登陆服务sshd,在适当时候再重启sshd服务
[root@localhost ~]# ps -aux | grep sshd root 1292 0.0 0.2 112900 4320 ? Ss 21:51 0:00 /usr/sbin/sshd -D root 3609 0.0 0.3 160988 5640 ? Ss 22:05 0:00 sshd: root@pts/1 root 3876 0.7 0.2 160988 5572 ? Ss 22:10 0:00 sshd: jack [priv] jack 3880 0.0 0.1 160988 2380 ? S 22:10 0:00 sshd: jack@pts/2 root 3959 0.0 0.0 112828 976 pts/0 S+ 22:11 0:00 grep --color=auto sshd [root@localhost ~]# kill 1292 [root@localhost ~]# ps -aux | grep sshd root 3609 0.0 0.3 160988 5640 ? Ss 22:05 0:00 sshd: root@pts/1 root 3876 0.5 0.2 160988 5572 ? Ss 22:10 0:00 sshd: jack [priv] jack 3880 0.0 0.1 160988 2380 ? S 22:10 0:00 sshd: jack@pts/2 root 3974 0.0 0.0 112824 976 pts/0 S+ 22:11 0:00 grep --color=auto sshd # 重启服务 [root@localhost ~]# service sshd restart Redirecting to /bin/systemctl restart sshd.service
-
终止一个终端
[root@localhost ~]# ps -aux | grep bash root 809 0.0 0.0 115408 944 ? S 21:51 0:00 /bin/bash /usr/sbin/ksmtuned root 2891 0.0 0.0 72472 776 ? Ss 22:03 0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "env GNOME_SHELL_SESSION_MODE=classic gnome-session --session gnome-classic" root 4454 0.0 0.1 116576 2976 pts/0 Ss 22:18 0:00 bash root 4507 0.0 0.1 116576 2984 pts/1 Ss+ 22:19 0:00 bash root 4554 0.0 0.1 116580 2980 pts/2 Ss+ 22:19 0:00 bash root 4608 0.0 0.1 116576 2988 pts/3 Ss 22:19 0:00 bash root 4701 0.0 0.0 112828 976 pts/3 R+ 22:20 0:00 grep --color=auto bash [root@localhost ~]# kill -9 4507 [root@localhost ~]# ps -aux | grep bash root 809 0.0 0.0 115408 944 ? S 21:51 0:00 /bin/bash /usr/sbin/ksmtuned root 2891 0.0 0.0 72472 776 ? Ss 22:03 0:00 /usr/bin/ssh-agent /bin/sh -c exec -l /bin/bash -c "env GNOME_SHELL_SESSION_MODE=classic gnome-session --session gnome-classic" root 4454 0.0 0.1 116576 2976 pts/0 Ss 22:18 0:00 bash root 4554 0.0 0.1 116580 2980 pts/2 Ss+ 22:19 0:00 bash root 4608 0.0 0.1 116576 2988 pts/3 Ss 22:19 0:00 bash root 4736 0.0 0.0 112828 976 pts/3 R+ 22:21 0:00 grep --color=auto bash
服务(service)管理
服务(service)的本质就是进程,但是时运行再后台的,通常都会监听某个端口,等待其他程序的请求,比如mysql、sshd、防火墙等,因此我们又成为守护进程
service 服务名 [start|stop|restart|reload|status]
:立即生效,重启后无效
在centos7.0之后使用systemctl
systemctl [start|stop|restart|reload|status] 服务名称
-
systemctl list-units
:列出当前系统服务的状态 -
systemctl list-unit-files
:列出服务的开机状态 -
systemctl enable sshd
:设定指定服务开机开启 -
systemctl disable sshd
:设定指定服务开机关闭 -
systemctl list-dependencies sshd
:查看指定服务的倚赖关系 -
systemctl mask xxx
:冻结指定服务 -
systemctl unmask xxx
:启用服务
实例
- 查看当前防火墙状态,关闭防火墙和重启防火墙
# 查看防火墙状态
service firewalld status
Redirecting to /bin/systemctl status firewalld.service
● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since 三 2021-08-18 21:51:15 CST; 1h 11min ago
Docs: man:firewalld(1)
Main PID: 854 (firewalld)
Tasks: 2
CGroup: /system.slice/firewalld.service
└─854 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
8月 18 21:51:14 localhost.localdomain systemd[1]: Starting firewalld - dynamic firewall daemon...
8月 18 21:51:15 localhost.localdomain systemd[1]: Started firewalld - dynamic firewall daemon.
8月 18 21:51:15 localhost.localdomain firewalld[854]: WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future release. Please ...bling it now.
Hint: Some lines were ellipsized, use -l to show in full.
# 关闭防火墙
[root@localhost ~]# service firewalld stop
Redirecting to /bin/systemctl stop firewalld.service
[root@localhost ~]# service firewalld status
# 重启防火墙
[root@localhost ~]# service firewalld status
Redirecting to /bin/systemctl status firewalld.service
- 通过telnet指令检查端口是否开放
telnet ip 端口
查询服务
- 使用setup --> 系统服务可以看到
- /etc/init.d/服务名称
[root@localhost ~]# ll /etc/init.d/
动态查看进程
top与ps命令很相似,他们都用来显示这个在执行的进程。top与ps最大的区别在于top执行一段时间可以更新正在运行的进程
top [选项]
选项说明
-d 秒数
:指定top命令每隔几秒更新,默认是3秒
P
:以CPU使用率排序M
:以内存使用率排序N
:以PID排序q
:退出top
-i
:使top不显示任何闲置或者僵死进程
-p
:通过指定监控进程ID来仅仅监控某个进程
案例
- 监视特定用户
top
:输入此命令,按回车键,查看执行的进程u
:然后输入u
回车,在输入用户名,即可
- 终止指定的进程
top
:输入此命令,按回车键,查看执行的进程k
:然后输入k
回车,在输入想要结束的进程ID号
- 指定系统状态更新的时间(每隔10秒自动更新)
top -d 10
监控网络服务状态
netstat [选项]
选项
-an
:按一定顺序排列输出
-p
:显示哪个进程在调用
案例
- 查看系统所有的网络服务
netstat -anp | more
- 查看服务名是sshd的服务信息
netstat -anp | grep sshd