30人参与 • 2026-07-28 • Linux
系统负载平均值:linux内核以活动请求数的指数移动平均值来表示。
linux 中的 stress 工具用于对系统进行压力测试,可模拟 cpu、内存、i/o 和磁盘等资源的高负载状态。通过指定参数(如 -c 压 cpu、-m 压内存)可创建负载,帮助发现系统在压力下的稳定性问题,常用于性能调优或硬件验证。
#启动两个终端,一个终端进行消耗另一个终端监控 [root@centos7 ~ 17:00:03]# stress -c 2 stress: info: [2178] dispatching hogs: 2 cpu, 0 io, 0 vm, 0 hdd #stress -c 2 stress -c 是针对cpu的后面的数字是几个,这里的2会让电脑2个cpu跑满,另一台监控终端是我们通过top监控 top - 17:03:21 up 5 min, 2 users, load average: 0.76, 0.26, 0.10 tasks: 203 total, 3 running, 200 sleeping, 0 stopped, 0 zombie %cpu0 : 100.0/0.0 100[|||||||||||||||||||||||||||||||||||||||||||||||||||| %cpu1 : 100.0/0.0 100[|||||||||||||||||||||||||||||||||||||||||||||||||||| %cpu2 : 0.0/0.0 0[ %cpu3 : 0.0/0.0 0[ #4个cpu2个已经跑满,回测试终端ctrl+c结束
#先查看消耗前的内存,通过free命令去查看-m 以mb为单位
[root@centos7 ~ 17:05:01]# free -m
total used free shared buff/cache available
mem: 3931 479 3113 14 338 3220
swap: 3967 0 3967
#消耗1gb的内存
[root@centos7 ~ 17:07:12]# stress -m 1 --vm-bytes 1g
stress: info: [2527] dispatching hogs: 0 cpu, 0 io, 1 vm, 0 hdd
#查看消耗后的内存
[root@centos7 ~ 17:03:23]# free -m
total used free shared buff/cache available
mem: 3931 1378 2214 14 338 2321
swap: 3967 0 3967
**注意!!!**mem表示物理区域也就是内存,total是共计多少内存,used表示使用了多少,free是空闲,shared是共享,buff/cache是缓存,available当前可用。 swap是交互区域,是磁盘的存储,当物理区域不够时会像交互区域借用内存。
[root@centos7 ~ 17:11:54]# stress -m 1 --vm-bytes 4g #创建一个消耗磁盘的进程,一个进程消耗4gb [root@centos7 ~ 17:24:20]# sar -dp 1 linux 3.10.0-1160.71.1.el7.x86_64 (centos7.xiaoliu.cloud) 2026年07月22日 _x86_64_ (4 cpu) 17时24分23秒 dev tps rd_sec/s wr_sec/s avgrq-sz avgqu-sz await svctm %util 17时24分25秒 sda 209.00 0.00 214016.00 1024.00 115.94 684.98 4.79 100.20 17时24分25秒 sr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 17时24分25秒 centos-root 98.00 0.00 100352.00 1024.00 116.51 1470.18 10.22 100.20 # 监控磁盘读写速度,重点关注rd_sec/s和wr_sec/s,单位是0.5k/每秒,每个sec(sector)是512byte。
# 传送一个大size的文件 # 监控带宽 [root@centos7 ~ 17:27:37]# sar -n dev 1 linux 3.10.0-1160.71.1.el7.x86_64 (centos7.xiaoliu.cloud) 2026年07月22日 _x86_64_ (4 cpu) 17时28分04秒 iface rxpck/s txpck/s rxkb/s txkb/s rxcmp/s txcmp/s rxmcst/s 17时28分05秒 lo 0.00 0.00 0.00 0.00 0.00 0.00 0.00 17时28分05秒 virbr0-nic 0.00 0.00 0.00 0.00 0.00 0.00 0.00 17时28分05秒 virbr0 0.00 0.00 0.00 0.00 0.00 0.00 0.00 17时28分05秒 ens33 45963.00 6858.00 67666.94 402.50 0.00 0.00 0.00
以下是针对 linux 系统监控的几条实用建议,涵盖关键监控维度和最佳实践:
top/htop(实时)、vmstat/iostat(系统级)等工具快速查看。systemctl status 或自定义脚本实现。对异常退出的进程,结合日志排查崩溃原因。/var/log/messages)、应用日志集中存储(如使用 elk 栈),关注错误信息(error 级别)、登录异常(/var/log/auth.log)和磁盘错误(dmesg | grep error),必要时配置日志告警规则。smartctl 工具检测 s.m.a.r.t 信息)和文件系统完整性(定期运行 fsck,非挂载状态下),避免硬件故障导致数据丢失。netstat/ss)、异常连接(尤其是外部 ip 的高频访问)和防火墙规则生效情况。可结合 tcpdump 抓包分析可疑流量。通过分层监控(基础指标→服务状态→业务性能)和主动预警,可显著提升系统稳定性和故障响应效率。
centos 7 使用 systemd 引导系统启动,速度最快,所有进程无论有无依赖关系则都是并行启动(很多时候进程没有真正启动而是只有一个信号或者说是标记而已,在真正利用的时候才会真正启动)。systemd为了解决上文的问题而诞生。它的目标是,为系统的启动和管理提供一套完整的解决方案。
系统引导程序:
服务:从业务角度来称呼,例如 web 服务,数据库服务。
守护进程(daemon):web 服务器对外提供 web 服务,由 web 相关的进程提供支持。
以web服务为例:
重点:一个服务的正常运行是有非常多大进程在维持,这个功能可能对应部分进程,比如我们电脑开机,还没有打开任何软件,打开后台管理器,有非常多的进程是启动的,这些进程维持着服务正常的 运行,所以称之为守护进程。
例如:
#我们在虚拟机上安装软件包httpd #启动这个服务 [root@centos7 ~ 18:40:18]# systemctl start httpd #启动httpd #查看进程 [root@centos7 ~ 18:41:07]# ps -c httpd f pid tty stat time command 5745 ? ss 0:00 /usr/sbin/httpd -dforeground 5751 ? s 0:00 \_ /usr/sbin/httpd -dforeground 5752 ? s 0:00 \_ /usr/sbin/httpd -dforeground 5753 ? s 0:00 \_ /usr/sbin/httpd -dforeground 5754 ? s 0:00 \_ /usr/sbin/httpd -dforeground 5755 ? s 0:00 \_ /usr/sbin/httpd -dforeground #f我们查看树形结构可以看出进程之间的关系,这六个进程就是httpd的守护进程
守护进程:systemd,由守护进程干活。
工具:systemctl,提供给用户要、用来根机器交互的工具。
systemctl 命令用于管理不同类型的系统对象,这些对象称之为 units。
#列出状态为loaded units [root@centos7 ~ 18:42:58]# systemctl list-units #数量非常多 [root@centos7 ~ 18:48:28]# systemctl list-units | wc -l 169
systemctl list-units命令输出说明:
#通过-t选定特定类型unit查看,我们选有关定时器类型 [root@centos7 ~ 18:49:47]# systemctl list-units -t timer unit load active sub description systemd-tmpfiles-clean.timer loaded active waiting daily cleanup of temporary directories unbound-anchor.timer loaded active waiting daily update of the root trust anchor for d load = reflects whether the unit definition was properly loaded. active = the high-level unit activation state, i.e. generalization of sub. sub = the low-level unit activation state, values depend on unit type. 2 loaded units listed. pass --all to see loaded but inactive units, too. to show all installed unit files use 'systemctl list-unit-files'. #查看系统中类型为service,状态为active和inactive的unit [root@centos7 ~ 18:56:38]# systemctl list-units --type service --all # 列出系统中所有unit,包括未loaded的unit [root@centos7 ~ 18:59:56]# systemctl list-unit-files #查看失败的服务
命令解释
[root@centos7 ~ 19:02:11]# systemctl status sshd.service
● sshd.service - openssh server daemon
loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
active: active (running) since 三 2026-07-22 16:58:03 cst; 2h 11min ago
docs: man:sshd(8)
man:sshd_config(5)
main pid: 1252 (sshd)
tasks: 1
cgroup: /system.slice/sshd.service
└─1252 /usr/sbin/sshd -d
7月 22 16:58:03 centos7.xiaoliu.cloud systemd[1]: starting openssh server daemon...
7月 22 16:58:03 centos7.xiaoliu.cloud sshd[1252]: server listening on 0.0.0.0 port 22.
...
| 关键字 | 概述 |
|---|---|
| loaded | 单元配置文件已处理 |
| active(running) | 正在运行 |
| active(exited) | 已成功完成一次性配置 |
| active(waiting) | 运行中,正在等待事件 |
| inactive | 不再运行 |
| enabled | 系统引导时启动 |
| disabled | 系统引导时不启动 |
| static | 无法启动,依赖其他单元启动 |
| 命令 | 任务 |
|---|---|
| systemctl status unit | 查看单元状态的详细信息。 |
| systemctl stop unit | 在运行中的系统上停止一项服务。 |
| systemctl start unit | 在运行中的系统上启动一项服务。 |
| systemctl restart unit | 在运行中的系统上重新启动一项服务。 |
| systemctl reload unit | 重新加载运行中服务的配置文件。 |
| systemctl mask unit | 禁用服务,使其无法手动启动或在系统引导时启动。 |
| systemctl unmask unit | 使屏蔽的服务变为可用。 |
| systemctl enable unit | 将服务配置为在系统引导时启动。使用 --now 选项也会启动该服务。 |
| systemctl disable unit | 禁止服务在系统引导时启动。使用 --now选项也会停止该服务。 |
# 停止服务 [root@centos7 ~ 19:10:40]# systemctl stop sshd.service #此时我如果断开连接在想通过ssh登录到虚拟机是行不通的了,需要在主机上重新开启ssh服务 # 重新启动服务(主机) [root@centos7 ~ 19:16:44]# systemctl start sshd.service #可以再次通过ssh登录 # 重启服务,相当于stop再start [root@centos7 ~ 19:18:58]# systemctl restart sshd.service #此时断开连接再通过ssh连接不受影响 # 重新加载,一般是配置文件发生改变的时候配置,配置文件发生改变并不会立刻告知给服务进程,修改配置需要重新加载一次配置文件,重新加载服务 [root@centos7 ~ 19:20:24]# systemctl reload sshd.service # 重新加载服务,服务对应的主进程不会重启,只会重新加载一次配置文件。 # 禁止服务开机自启 [root@centos7 ~ 19:22:53]# systemctl disable sshd.service [root@centos7 ~ 19:24:46]# systemctl is-enabled sshd disabled # 设置服务开机自启 [root@centos7 ~ 19:25:03]# systemctl enable sshd.service --now [root@centos7 ~ 19:26:03]# systemctl is-enabled sshd enabled # 禁用服务:服务被禁用后,将无法start,因为服务的配置文件指向/dev/null [root@centos7 ~ 19:26:15]# systemctl mask sshd.service # 取消禁用 [root@centos7 ~ 19:27:26]# systemctl unmask sshd.service #null文件像黑洞类似于丢弃站
脚本说明:这是一个无限循环的脚本,每 5 秒会向 /var/log/study.log 文件中追加一行包含当前时间的日志,内容为 [时间]: i'm studying [ linux ]。
[root@centos7 ~ 19:27:46]# vim /usr/local/bin/xdc #!/bin/bash # 第一行内容是脚本的 "解释器声明"(shebang),指定该脚本使用 /bin/bash 作为解释器执行。系统会根据这一行找到对应的 shell 程序来解析后续命令。 # 启动一个无限循环:while 是循环关键字,true 是一个永远为真的条件,因此这个循环会一直执行下去,直到被外部终止(如 ctrl+c)。 while true # 循环体的开始标记,do 和后面的 done 之间的内容是循环中重复执行的命令。 do # 执行 date 命令(获取当前系统时间),并通过 $(...) 捕获其输出,将结果赋值给变量 date。 date=$(date) # echo 命令输出字符串,其中 $date 会被替换为变量的值 # >> 是追加重定向符号,将输出内容追加到 /var/log/study.log 文件中 # 最终输出内容类似 fri oct 31 10:00:00 cst 2025: i'm studying [ linux ]。 echo "$date: i'm studying [ linux ]" >> /var/log/study.log # 让脚本暂停执行 5 秒(sleep 命令用于延迟,单位默认为秒),避免循环执行过快。 sleep 5 # 循环体的结束标记,与前面的 while 和 do 配合,标志着一次循环的结束。 done [root@centos7 ~ 19:33:09]# chmod +x /usr/local/bin/xdc
2.创建 studyd 服务单元文件
#参考sshd
[root@centos7 ~ 19:35:03]# cp /usr/lib/systemd/system/sshd.service /etc/systemd/system/studyd.service
[root@centos7 ~ 19:37:58]# vim /etc/systemd/system/studyd.service
[unit]
description=study server daemon
[service]
execstart=/usr/local/bin/study
[install]
wantedby=multi-user.target
#配置文件发生变化通知systemd读取unit的变化
[root@centos7 ~ 19:38:25]# systemctl daemon-reload
# 启用并启动服务
[root@centos7 ~ 19:38:55]# systemctl enable studyd --now
# 查看服务状态
[root@centos7 ~ 19:39:26]# systemctl status studyd
● studyd.service - study server daemon
loaded: loaded (/etc/systemd/system/studyd.service; enabled; vendor preset: disabled)
active: active (running) since 三 2026-07-22 19:25:46 cst; 13min ago
main pid: 763 (study)
cgroup: /system.slice/studyd.service
├─ 763 /bin/bash /usr/local/bin/study
└─2748 sleep 5
7月 22 19:25:46 centos7.xiaoliu.cloud systemd[1]: started study server daemon.
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论