最近在释放某些废弃的服务器,快速查看有哪些服务。
bash
#!/bin/bash
# 服务器重要服务检查脚本
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# 重要服务列表
SYSTEM_SERVICES=("sshd" "systemd" "cron" "rsyslog" "network" "firewalld" "iptables")
WEB_SERVICES=("nginx" "apache2" "httpd" "tomcat" "php-fpm")
DB_SERVICES=("mysql" "mariadb" "postgresql" "redis" "mongodb" "elasticsearch")
MIDDLEWARE_SERVICES=("docker" "containerd" "kubelet" "rabbitmq" "kafka" "zookeeper")
MONITOR_SERVICES=("prometheus" "grafana" "node_exporter" "zabbix-agent")
OTHER_SERVICES=("ntpd" "chronyd" "supervisord" "keepalived" "haproxy")
# 计数器
TOTAL_RUNNING=0
# 检查systemd服务状态
check_service() {
local service=$1
local category=$2
if systemctl list-unit-files | grep -q "^${service}.service"; then
if systemctl is-active --quiet "$service" 2>/dev/null; then
local enabled_status=""
if systemctl is-enabled --quiet "$service" 2>/dev/null; then
enabled_status="[开机自启]"
else
enabled_status="[手动启动]"
fi
echo -e " ${GREEN}✓${NC} ${service} ${GREEN}(运行中)${NC} $enabled_status"
((TOTAL_RUNNING++))
return 0
else
echo -e " ${RED}✗${NC} ${service} ${RED}(已停止)${NC}"
return 1
fi
fi
return 2
}
# 通过进程检查服务
check_process() {
local service=$1
if pgrep -f "$service" > /dev/null 2>&1; then
local pids=$(pgrep -f "$service" | tr '\n' ' ')
echo -e " ${GREEN}✓${NC} ${service} ${GREEN}(运行中)${NC} [PIDs: $pids]"
((TOTAL_RUNNING++))
return 0
fi
return 1
}
# 检查服务类别
check_category() {
local category_name=$1
shift
local services=("$@")
local found=0
echo ""
echo -e "${BLUE}【${category_name}】${NC}"
echo "----------------------------------------"
for service in "${services[@]}"; do
check_service "$service" "$category_name"
local result=$?
# 如果systemd没找到,尝试进程检查
if [ $result -eq 2 ]; then
check_process "$service"
if [ $? -eq 0 ]; then
found=1
fi
elif [ $result -eq 0 ]; then
found=1
fi
done
if [ $found -eq 0 ]; then
echo " (未发现相关服务)"
fi
}
# 显示监听端口
show_listening_ports() {
echo ""
echo -e "${BLUE}【监听端口】${NC}"
echo "----------------------------------------"
if command -v ss &> /dev/null; then
ss -tulnp 2>/dev/null | tail -n +2 | while read line; do
local port=$(echo "$line" | awk '{print $5}' | awk -F: '{print $NF}')
local process=$(echo "$line" | awk '{print $NF}')
if [ -n "$port" ]; then
printf " 端口 %-6s -> %s\n" "$port" "$process"
fi
done
elif command -v netstat &> /dev/null; then
netstat -tulnp 2>/dev/null | grep LISTEN | while read line; do
local port=$(echo "$line" | awk '{print $4}' | awk -F: '{print $NF}')
local process=$(echo "$line" | awk '{print $NF}')
if [ -n "$port" ]; then
printf " 端口 %-6s -> %s\n" "$port" "$process"
fi
done
fi
}
# 主函数
main() {
echo "========================================"
echo " 服务器服务检查报告"
echo "========================================"
echo "主机名: $(hostname)"
echo "系统: $(uname -s) $(uname -r)"
echo "检查时间: $(date '+%Y-%m-%d %H:%M:%S')"
echo "========================================"
# 检查是否需要root权限
if [ "$EUID" -ne 0 ]; then
echo -e "${YELLOW}提示: 建议使用root权限运行以获取完整信息${NC}"
echo -e "${YELLOW} sudo $0${NC}"
echo ""
fi
# 检查各类服务
check_category "系统服务" "${SYSTEM_SERVICES[@]}"
check_category "Web服务" "${WEB_SERVICES[@]}"
check_category "数据库服务" "${DB_SERVICES[@]}"
check_category "中间件服务" "${MIDDLEWARE_SERVICES[@]}"
check_category "监控服务" "${MONITOR_SERVICES[@]}"
check_category "其他服务" "${OTHER_SERVICES[@]}"
# 显示监听端口
show_listening_ports
# 总结
echo ""
echo "========================================"
echo -e "总计运行中的重要服务: ${GREEN}${TOTAL_RUNNING}${NC} 个"
echo "========================================"
}
# 运行主函数
main