自动判断和启动相关服务脚本
选择一个能含有环境变量路径的文件夹或新增一个,然后在该文件夹下新建文本
btinit
然后将如下代码复制进去,然后sudo chmod 755 btinit
#!/bin/bash
btStatus=$(sudo bt status 2>/dev/null)
# 检查输出中是否包含表示运行状态的关键字,这里假设包含 "running" 表示正在运行
if echo "$btStatus" | grep -q "already running"; then
echo "bt 服务正在运行。"
else
echo "bt 服务未运行,正在尝试启动..."
# 执行 sudo bt start 命令来启动相应服务
sudo bt start
# 检查启动命令的退出状态
if [ $? -eq 0 ]; then
echo "bt 服务已成功启动。"
else
echo "启动 bt 服务时出现错误。"
fi
fi
# 判断nginx是否存在
if [ -f "/etc/init.d/nginx" ]; then
nginxStatus=$(sudo /etc/init.d/nginx status 2>/dev/null)
if echo "$nginxStatus" | grep -q "already running"; then
echo "nginx 服务正在运行。"
else
echo "nginx 服务未运行,正在尝试启动..."
# 执行 sudo bt start 命令来启动相应服务
sudo /etc/init.d/nginx start
# 检查启动命令的退出状态
if [ $? -eq 0 ]; then
echo "nginx 服务已成功启动。"
else
echo "启动 nginx 服务时出现错误。"
fi
fi
fi
# 判断apache是否存在
if [ -f "/etc/init.d/httpd" ]; then
httpdStatus=$(sudo /etc/init.d/httpd status 2>/dev/null)
if echo "$httpdStatus" | grep -q "is running"; then
echo "apache 服务正在运行。"
else
echo "apache 服务未运行,正在尝试启动..."
# 执行 sudo bt start 命令来启动相应服务
sudo /etc/init.d/httpd start
# 检查启动命令的退出状态
if [ $? -eq 0 ]; then
echo "apache 服务已成功启动。"
else
echo "启动 apache 服务时出现错误。"
fi
fi
fi
# 判断php-fpm-74是否存在
if [ -f "/etc/init.d/php-fpm-74" ]; then
phpStatus=$(sudo /etc/init.d/php-fpm-74 status 2>/dev/null)
if echo "$phpStatus" | grep -q "is running"; then
echo "php 服务正在运行。"
else
echo "bt服务未运行,正在尝试启动..."
# 执行 sudo bt start 命令来启动相应服务
sudo /etc/init.d/php-fpm-74 start
# 检查启动命令的退出状态
if [ $? -eq 0 ]; then
echo "php 服务已成功启动。"
else
echo "启动 php 服务时出现错误。"
fi
fi
fi