宝塔面板中可一键关闭闲置服务的方法有四种:一、用bt命令行工具停止面板及Web服务;二、执行自定义Shell脚本批量关停常见服务;三、通过宝塔任务管理器可视化筛选并手动停止;四、用systemctl精准关停非核心服务。

如果您在宝塔面板中发现大量后台服务处于运行状态但实际未被使用,可能造成资源占用过高或安全风险。以下是多种可执行的一键关闭闲置系统服务的方法:
一、使用宝塔内置命令行工具停止面板及关联服务
该方法通过宝塔官方提供的命令行接口统一控制核心服务启停,操作安全且兼容性强,适用于大多数Linux发行版。
1、通过SSH连接服务器,执行以下命令进入宝塔命令行工具:
bt
2、在交互式菜单中选择编号2(停止面板服务)
3、确认执行后,面板自身服务(btPanel)将立即停止
4、若需同时停止Web服务,再依次执行:
/etc/init.d/nginx stop
/etc/init.d/mysqld stop
/etc/init.d/php-fpm-74 stop(请根据您实际安装的PHP版本替换数字)
二、执行自定义Shell脚本批量关停服务
该方法依据宝塔官方停服逻辑封装为可复用脚本,能识别并关停Nginx、Apache、MySQL、Pure-FTPd、Redis、Memcached、Tomcat及各PHP-FPM实例等常见服务,避免遗漏。
1、复制以下完整脚本内容:
/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH stop_service(){ /etc/init.d/bt stop if [ -f "/etc/init.d/nginx" ]; then /etc/init.d/nginx stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/httpd" ]; then /etc/init.d/httpd stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/mysqld" ]; then /etc/init.d/mysqld stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/pure-ftpd" ]; then /etc/init.d/pure-ftpd stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/tomcat" ]; then /etc/init.d/tomcat stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/redis" ]; then /etc/init.d/redis stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/memcached" ]; then /etc/init.d/memcached stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-52" ]; then /etc/init.d/php-fpm-52 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-53" ]; then /etc/init.d/php-fpm-53 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-54" ]; then /etc/init.d/php-fpm-54 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-55" ]; then /etc/init.d/php-fpm-55 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-56" ]; then /etc/init.d/php-fpm-56 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-70" ]; then /etc/init.d/php-fpm-70 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-71" ]; then /etc/init.d/php-fpm-71 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-72" ]; then /etc/init.d/php-fpm-72 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-73" ]; then /etc/init.d/php-fpm-73 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-74" ]; then /etc/init.d/php-fpm-74 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-80" ]; then /etc/init.d/php-fpm-80 stop > /dev/null 2>&1 fi if [ -f "/etc/init.d/php-fpm-81" ]; then /etc/init.d/php-fpm-81 stop > /dev/null 2>&1 fi swapoff /www/swap } stop_service
2、将脚本保存为stop-all-services.sh文件
3、赋予执行权限:
chmod +x stop-all-services.sh
4、运行脚本:
./stop-all-services.sh
三、通过宝塔任务管理器手动筛选并关停服务
该方法提供可视化界面操作,适合不熟悉命令行的用户,可逐项确认服务状态与用途后再决定是否关闭。
1、登录宝塔面板,在左侧菜单栏点击【宝塔任务管理器】
2、切换至【服务】标签页
3、浏览服务列表,识别名称中含“backup”、“test”、“demo”、“sample”、“dev”等标识的服务
4、对确认闲置的服务,点击其右侧的【停止】按钮
5、对已停止但仍在进程列表中残留的服务,切换至【会话】页,执行ps aux | grep 定位PID,再使用kill -9
四、使用systemctl命令精准关停非宝塔核心服务
该方法基于systemd服务管理机制,可列出所有启用状态的服务并筛选出非必要项,避免误停宝塔依赖服务。
1、列出所有正在运行的服务:
systemctl list-units --type=service --state=running
2、过滤出非宝塔相关服务:
systemctl list-units --type=service --state=running | grep -E -v "(bt|nginx|httpd|mysql|mysqld|php|redis|memcached|pure-ftpd|tomcat)"
3、对输出结果中的每个服务名(如cups.service、avahi-daemon.service),执行:
systemctl stop
systemctl disable
4、特别注意保留以下服务:bt、nginx、mysqld、php-fpm-*、pure-ftpd










