东莞品牌型网站建设wordpress优化访问速度
2026/3/22 14:18:14 网站建设 项目流程
东莞品牌型网站建设,wordpress优化访问速度,利用ps做兼职的网站,都匀网站开发公司#x1f468;‍⚕️ 主页#xff1a; gis分享者 #x1f468;‍⚕️ 感谢各位大佬 点赞#x1f44d; 收藏⭐ 留言#x1f4dd; 加关注✅! #x1f468;‍⚕️ 收录于专栏#xff1a;Shell 面试 文章目录一、#x1f340;前言1.1 ☘️示例1.2 ☘️知识扩展一、#…‍⚕️主页 gis分享者‍⚕️感谢各位大佬 点赞 收藏⭐ 留言 加关注✅!‍⚕️收录于专栏Shell 面试文章目录一、前言1.1 ☘️示例1.2 ☘️知识扩展一、前言可以使用 systemctl 命令来列出和检查服务状态这是一个用于管理 Systemd 服务的命令行工具。1.1 ☘️示例基本示例#!/bin/bash# 列出所有服务并保存到变量services$(systemctl list-units--typeservice--all)# 打印标题echoService Name Load State Active State Sub State# 读取每个服务的状态whileread-r service;do# 检查服务名称和状态echo$service|awk{printf %-20s %-12s %-12s %-12s\n, $1, $2, $3, $4}done $services运行这个脚本它会列出所有的服务名称和它们的加载状态、激活状态及子状态。示例一#!/bin/bash# 系统服务状态检查脚本# 用于检查系统中所有服务的运行状态echoecho 系统服务状态检查报告echoecho# 检查系统类型和可用的命令check_system_type(){ifcommand-v systemctl /dev/null 21;thenecho检测到 systemd 系统return1 elif command-v service /dev/null 21;thenecho检测到 SysVinit 系统return2elseecho未检测到支持的服务管理工具return0 fi}# 使用systemctl检查服务状态 (systemd系统)check_systemd_services(){echo正在检查 systemd 服务状态...echo----------------------------------# 统计信息local total_services$(systemctl list-units--typeservice|grep-vLOAD\|listed|wc-l)local active_services$(systemctl list-units--typeservice--stateactive|grep-vLOAD\|listed|wc-l)local inactive_services$(systemctl list-units--typeservice--stateinactive|grep-vLOAD\|listed|wc-l)local failed_services$(systemctl list-units--typeservice--statefailed|grep-vLOAD\|listed|wc-l)echo总服务数:$total_servicesecho运行中:$active_servicesecho已停止:$inactive_servicesecho失败:$failed_servicesecho# 显示失败的服务if[$failed_services-gt0];thenecho失败的服务列表:systemctl list-units--typeservice--statefailed--no-pager|grep-vLOAD\|listedechofi# 显示所有服务状态摘要echo服务状态摘要:systemctl list-units--typeservice--no-pager|grep-vLOAD\|listed|awk{print $2: $4}|sort}# 使用service命令检查服务状态 (SysVinit系统)check_sysvinit_services(){echo正在检查 SysVinit 服务状态...echo----------------------------------# 检查 /etc/init.d/ 目录下的服务if[-d/etc/init.d/];then local service_count0 local running_count0 local stopped_count0forservice in/etc/init.d/*;doif[-f$service][-x$service];then service_name$(basename$service)service_count$((service_count1))# 检查服务状态if$servicestatus /dev/null 21;thenecho$service_name: 运行中running_count$((running_count1))elseecho$service_name: 已停止stopped_count$((stopped_count1))fi fi doneechoecho统计信息:echo总服务数:$service_countecho运行中:$running_countecho已停止:$stopped_countelseecho未找到服务目录 /etc/init.d/fi}# 检查特定服务check_specific_service(){local service_name$1ifsystemctl is-active--quiet$service_name2/dev/null;thenecho$service_name: 运行中elif systemctl is-active--quiet$service_name2/dev/null;thenecho$service_name: 已停止elseecho$service_name: 服务不存在或无法访问fi}# 主程序main(){# 获取系统类型check_system_type system_type$? case$system_typein 1)check_systemd_services;;2)check_sysvinit_services;;*)echo错误: 不支持的系统类型exit1;;esacechoechoecho 检查完成echo}# 检查是否提供了特定服务名称作为参数if[$# -eq 1 ]; thenecho检查特定服务:$1check_specific_service$1else# 执行完整检查main fi示例二#!/bin/bash# 服务状态检查函数check_service_status(){# 检测初始化系统类型if[[$(ps--no-headers-o comm 1)systemd]];thenecho-e\n[Systemd 系统服务状态检查]# 获取所有systemd服务单元systemctl list-units--typeservice--all--no-pager|awkNR1 !/loaded/ {print $1}|whileread-r service;do# 过滤掉无效条目if[[$service~ \.service$]];then active_status$(systemctl is-active$service)enabled_status$(systemctl is-enabled$service2/dev/null||echounknown)printf%-45s %-15s %-15s\n${service%.*}[Active:$active_status][Enabled:$enabled_status]fi doneelseecho-e\n[SysV Init 系统服务状态检查]# 检查传统init系统服务if[-d/etc/init.d];then find/etc/init.d/-executable-typef-printf%f\n|whileread-r service;dostatus_output$(service$servicestatus 21)if[[$status_output~is running]];then staterunningelsestatestoppedfi printf%-45s %-15s\n$service[Status:$state]doneelseecho未找到SysV初始化系统服务目录fi fi}# 执行主函数check_service_status|less-S脚本特点说明智能初始化系统检测通过检查PID 1进程判断实际使用的初始化系统支持systemd和传统SysV init系统systemd系统处理使用systemctl list-units获取完整服务列表显示服务激活状态(active/inactive)和启用状态(enabled/disabled)过滤无效条目和标题行SysV系统处理检查/etc/init.d目录中的可执行服务脚本解析基础运行状态(running/stopped)输出优化格式化列对齐输出通过管道连接less命令实现分页浏览支持宽字符显示(-S参数保持折行)1.2 ☘️知识扩展1systemctl 命令systemctl 是一个核心工具用于控制 Systemd 系统及服务管理器。你可以用它来启动、停止、重启、禁用和启用服务还可以用来检查服务状态。2awk这是一个非常强大的文本处理工具特别适合格式化和筛选文本内容。在这个脚本中我使用 awk 来格式化输出。3重定向操作符 和 : Bash 中有多种重定向技术。这里使用了 它将右侧的字符串作为输入提供给左侧命令。这是一种方便的方式来处理多行字符串。4脚本的改进为了使脚本更易读和更具扩展性我们可以考虑以下几点添加错误处理检查 systemctl 命令是否成功执行。提供选择特定服务进行检查的功能。输出结果到文件以方便后续查阅。改进版本的脚本#!/bin/bash# 检查 systemctl 命令是否可用if!command-v systemctl /dev/null thenechosystemctl 命令不可用请确保 Systemd 已安装。exit1 fi# 获取所有服务的状态services$(systemctl list-units--typeservice--all)# 打印标题printf%-20s %-12s %-12s %-12s\nService NameLoad StateActive StateSub State# 读取每个服务的状态并格式化输出whileread-r service;doecho$service|awk{printf %-20s %-12s %-12s %-12s\n, $1, $2, $3, $4}done $services# 将结果保存到文件echo$services service_status.txtecho服务状态检查完毕结果已保存到 service_status.txt 文件。

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询