1.打开 Cygwin 终端,执行:
pip3 install supervisor
安装完成后,验证是否可用:
supervisord --version # 若输出版本号,说明安装成功
2.生成配置文件并配置
生成默认配置文件:
echo_supervisord_conf > /etc/supervisord.conf # 若 /etc 目录不存在,先创建:mkdir -p /etc
编辑配置文件:
[program:php_script1]
command=/usr/bin/php /home/server/ceshi.php # 监听的脚本地址
autostart=true # 随 supervisord 启动
autorestart=true # 脚本退出后自动重启
startretries=3 # 启动失败重试次数
# ---------------------- 日志轮转核心配置 ----------------------
stdout_logfile=/var/log/php_script/php_script1.log # 1. 恢复日志文件路径(必须指定文件,而非NONE)
stdout_logfile_maxbytes=10MB # 2. 单个日志文件最大大小(如10MB,可改50MB/100MB)
stdout_logfile_backups=5 # 3. 保留的历史日志数量(如5个,即最多保留5个切割后的旧日志)
stdout_logfile_append=true # 4. 日志追加模式(默认true,无需修改)
# (可选)错误日志也配置轮转(与stdout同理)
stderr_logfile=/var/log/php_script/php_script1_err.log
stderr_logfile_maxbytes=10MB
stderr_logfile_backups=5
# --------------------------------------------------------------
3.启动 supervisord 服务
supervisord -c /etc/supervisord.conf
4.管理 PHP 脚本进程
supervisorctl -c /etc/supervisord.conf status # 查看状态
supervisorctl -c /etc/supervisord.conf start my_php_script # 启动
supervisorctl -c /etc/supervisord.conf restart my_php_script # 重启
5.启动cygwin时自动启动
# 启动服务
#!/bin/bash
# 定义日志目录(根据实际配置修改)
MYSQL_LOG_DIR="/var/log/mysql"
NGINX_LOG_DIR="/var/log/nginx"
PHP_LOG_DIR="/var/log/php"
SUPERVISOR_LOG_DIR="/var/log/supervisor"
# 确保日志目录存在
mkdir -p "$MYSQL_LOG_DIR" "$NGINX_LOG_DIR" "$PHP_LOG_DIR" "$SUPERVISOR_LOG_DIR"
# 函数:检查进程是否是否存在(通过进程名关键字)
is_running() {
local process_name="$1"
# pgrep -f:匹配整个命令行,避免误判名
if pgrep -f "$process_name" > /dev/null; then
return 0 # 进程存在(0表示成功)
else
return 1 # 进程不存在
fi
}
# 1. 启动 Nginx(检测关键字:nginx)
if is_running "nginx"; then
echo "Nginx 已启动,跳过..."
else
echo "启动 Nginx..."
nginx
if [ $? -ne 0 ]; then
echo "Error: Nginx 启动失败!"
exit 1
fi
sleep 1
fi
# 2. 启动 PHP-FPM(检测关键字:php-fpm,根据实际进程名调整)
if is_running "php-fpm"; then
echo "PHP-FPM 已启动,跳过..."
else
echo "启动 PHP-FPM..."
php-fpm
if [ $? -ne 0 ]; then
echo "Error: PHP-FPM 启动失败!"
exit 1
fi
sleep 1
fi
# 3. 启动 MySQL(检测关键字:mysqld_safe 或 mysqld)
if is_running "mysqld_safe" || is_running "mysqld"; then
echo "MySQL 已启动,跳过..."
else
echo "启动 MySQL..."
mysqld_safe --log-error="$MYSQL_LOG_DIR/error.log" > "$MYSQL_LOG_DIR/mysqld_stdout.log" 2>&1 &
# 等待 MySQL 初始化(最长等待10秒,每2秒检查一次)
local wait_time=0
while [ $wait_time -lt 10 ]; do
if is_running "mysqld_safe" || is_running "mysqld"; then
echo "MySQL 启动成功"
break
fi
sleep 2
wait_time=$((wait_time + 2))
done
if [ $wait_time -ge 10 ]; then
echo "Error: MySQL 启动超时!查看日志:$MYSQL_LOG_DIR/error.log"
exit 1
fi
fi
# 4. 启动 Supervisor(检测关键字:supervisord)
if is_running "supervisord"; then
echo "Supervisor 已启动,跳过..."
else
echo "启动 Supervisor..."
supervisord -c /etc/supervisord.conf
if [ $? -ne 0 ]; then
echo "Error: Supervisor 启动失败!"
exit 1
fi
# 等待 Supervisor 初始化
sleep 2
fi
# 查看 Supervisor 管理的进程状态
echo -e "\nSupervisor 进程状态:"
supervisorctl -c /etc/supervisord.conf status
echo -e "\n所有服务检查/启动完成!"