lx_systemd_list
Linux Systemd 单元状态 美化列表
一键脚本
bash <(curl -sL gitee.com/meimolihan/cmdbox/raw/master/sh/lx_systemd_list.sh)
bash <(curl -sL ../sh/lx_systemd_list.sh)
效果预览
补充说明
该脚本用于美化显示 Linux Systemd 单元状态列表,基于 systemctl list-units --type=service --all 命令实现,适合快速查看系统服务的运行状态。
功能特点
- 彩色输出:服务名称(青色)、状态(绿色/红色)、描述(蓝色)使用不同颜色区分
- 完整信息:显示单元名称、加载状态、激活状态、子状态、描述等信息
- 表格格式化:自动使用
column命令对齐输出 - 状态高亮:运行中的服务显示绿色,失败的服务显示红色
输出说明
脚本输出包含以下字段:
| 字段 | 说明 |
|---|---|
| 单元 | Systemd 单元名称 |
| 加载 | 单元是否已加载 |
| 激活 | 激活状态(active/inactive) |
| 子状态 | 详细子状态(running/exited/failed 等) |
| 描述 | 服务的简要描述 |
注意事项
- 需要系统已安装
systemctl命令(通常 systemd 系统自带) - 需要
column命令(通常系统已自带) - 显示的是所有 systemd 服务单元,包括已停止的
- 失败的服务会显示红色警告
- 部分服务可能没有描述信息
脚本源码
#!/bin/bash
set -uo pipefail
list_color_init() {
export gl_hui=$'\033[38;5;59m'
export gl_hong=$'\033[38;5;9m'
export gl_lv=$'\033[38;5;10m'
export gl_huang=$'\033[38;5;11m'
export gl_lan=$'\033[38;5;32m'
export gl_bai=$'\033[38;5;15m'
export gl_zi=$'\033[38;5;13m'
export gl_bufan=$'\033[38;5;14m'
export reset=$'\033[0m'
}
list_color_init
break_end() {
echo -e "${gl_lv}操作完成${gl_bai}"
echo -e "${gl_bai}按任意键继续 ${gl_hong}.${gl_huang}.${gl_lv}.${gl_bai}\c"
read -r -n 1 -s -r -p ""
echo ""
clear
}
column_if_available() {
if command -v column &> /dev/null; then
column -t -s $'\t'
else
cat
fi
}
list_beautify_linux_systemd() {
{
printf "%s%s\t%s\t%s\t%s\t%s%s\n" "$gl_hui" "单元" "加载" "激活" "子状态" "描述" "$reset"
printf "%s%s\t%s\t%s\t%s\t%s%s\n" "$gl_hui" "--------" "--------" "--------" "--------" "--------" "$reset"
data=$(systemctl list-units --type=service --all --no-legend 2>/dev/null | head -n 50)
if [ -z "$data" ]; then
printf "%s%s\t%s\t%s\t%s\t%s%s\n" "$gl_huang" "(无数据)" "(无数据)" "(无数据)" "(无数据)" "(无数据)" "$reset"
else
echo "$data" | awk -v unit_color="$gl_lan" \
-v load_color="$gl_bufan" \
-v active_color="$gl_lv" \
-v substate_color="$gl_huang" \
-v desc_color="$gl_hui" \
-v reset="$reset" '
BEGIN {
FS="[[:space:]]+";
OFS="\t"
}
{
unit = $1
load = $2
active = $3
substate = $4
desc = ""
for (i=5; i<=NF; i++) {
if (desc == "") desc = $i; else desc = desc " " $i
}
act_color = active_color
if (active == "active" && substate == "running") {
act_color = active_color
} else if (active == "failed") {
act_color = "\033[38;5;9m"
} else if (active == "inactive") {
act_color = "\033[38;5;59m"
}
sub_color = substate_color
if (substate == "running") {
sub_color = "\033[38;5;10m"
} else if (substate == "failed") {
sub_color = "\033[38;5;9m"
} else if (substate == "exited") {
sub_color = "\033[38;5;11m"
}
if (load == "loaded") load = "已加载"
else if (load == "not-found") load = "未找到"
else if (load == "bad") load = "错误"
if (active == "active") active = "已激活"
else if (active == "inactive") active = "未激活"
else if (active == "failed") active = "失败"
if (substate == "running") substate = "运行中"
else if (substate == "exited") substate = "已退出"
else if (substate == "dead") substate = "已停止"
else if (substate == "waiting") substate = "等待中"
else if (substate == "activating") substate = "激活中"
else if (substate == "deactivating") substate = "停用中"
print unit_color unit reset,
load_color load reset,
act_color active reset,
sub_color substate reset,
desc_color desc reset
}'
fi
} | column_if_available
}
list_beautify_all() {
clear
echo -e "${gl_zi}>>> Linux Systemd服务列表${gl_bai}"
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
list_beautify_linux_systemd
echo -e "${gl_bufan}————————————————————————————————————————————————${gl_bai}"
break_end
}
list_beautify_all
创建本地脚本
new_script="new_test.sh"
cat > "$new_script" << 'EOF'
#!/bin/bash
# 粘贴脚本源码
EOF
# 保留本地脚本,去掉 rm -f "$new_script"
chmod +x "$new_script" && ./"$new_script" && rm -f "$new_script"
相关命令
- lx_block_list
- lx_cert_list
- lx_cron_list
- lx_disk_list
- lx_env_list
- lx_firewall_list
- lx_fstab_list
- lx_gpu_list
- lx_hardware_list
- lx_inode_list
- lx_io_list
- lx_ip_list
- lx_journal_log_list
- lx_load_list
- lx_locale_list
- lx_login_log_list
- lx_ls_list
- lx_lscpu_list
- lx_lspci_list
- lx_lvm_list
- lx_modules_list
- lx_mounts_list
- lx_nic_list
- lx_package_list
- lx_port_list
- lx_process_list
- lx_raid_list
- lx_services_list
- lx_sshd_conf_list
- lx_swap_list
- lx_systemd_list 👈 当前所在位置
- lx_tcp_list
- lx_timer_list
- lx_timezone_list
- lx_uptime_list
- lx_usb_list
- lx_user_list
- lx_zombie_list