diff --git a/jsite.sh b/jsite.sh index f9b5720..4f9cd0d 100644 --- a/jsite.sh +++ b/jsite.sh @@ -614,14 +614,38 @@ install_pm2() { log_success "PM2安装完成" fi - # 设置PM2开机自启(避免交互式输出) + # 设置PM2开机自启 log_info "配置PM2开机自启..." - su - jingrow -c " + + # 获取PM2 startup命令的输出,避免交互式阻塞 + local startup_output=$(su - jingrow -c " export NVM_DIR=\"\$HOME/.nvm\" [ -s \"\$NVM_DIR/nvm.sh\" ] && \. \"\$NVM_DIR/nvm.sh\" - pm2 startup 2>/dev/null || true - " - log_success "PM2开机自启配置完成" + pm2 startup 2>&1 + " 2>/dev/null) + + # 检查是否成功生成systemd服务 + if echo "$startup_output" | grep -q "systemd"; then + log_success "PM2 systemd服务文件生成成功" + + # 提取systemd命令并执行(避免交互式提示) + local systemd_cmd=$(echo "$startup_output" | grep -E "sudo.*systemctl" | head -1) + if [ -n "$systemd_cmd" ]; then + log_info "执行systemd命令: $systemd_cmd" + # 移除sudo前缀,因为当前已经是root用户 + local clean_cmd=$(echo "$systemd_cmd" | sed 's/^sudo //') + if eval "$clean_cmd" 2>/dev/null; then + log_success "PM2 systemd服务启用成功" + else + log_warning "PM2 systemd服务启用失败,请手动执行: $systemd_cmd" + fi + fi + elif echo "$startup_output" | grep -q "init.d"; then + log_success "PM2 init.d脚本生成成功" + else + log_warning "PM2开机启动配置可能失败,输出: $startup_output" + log_info "请手动执行: su - jingrow -c 'pm2 startup'" + fi } # 2.6. 端口管理函数