pm2运行项目实践记录(通过ecosystem.config.js配置并自动运行)

三个步骤:
1.配置运行配置文件(推荐使用js格式配置)
2.配置项目重启脚本
用于版本项目更新、重启、部署
3.生成快速链接并运行
4.设置PM2开机自启动

一、PM2 配置文件(ecosystem.config.js)

创建ecosystem.config.js文件:

// ecosystem.config.js


const path = require('path');

// 固定不变的值直接写死
const APP_NAME = 'RentPay';  // 应用名通常不变
const APP_DIR = '/home/javaapp/RentPay';  // 部署路径通常固定
const JAR_FILE = 'RentPay.jar';  // jar文件名通常固定

// 可能经常调整的参数保留环境变量支持
const JAVA_OPTS = process.env.JAVA_OPTS || '-Xms2G -Xmx4G -XX:+UseG1GC';
const SPRING_PROFILE = process.env.SPRING_PROFILE || 'prod';
const MAX_MEMORY_RESTART = process.env.MAX_MEMORY_RESTART || '3G';


module.exports = {
  apps: [{
    name: APP_NAME,  // 固定值
    script: 'java',
    args: `-jar ${path.join(APP_DIR, JAR_FILE)}`,
//    args: `${JAVA_OPTS} -jar ${path.join(APP_DIR, JAR_FILE)} --spring.profiles.active=${SPRING_PROFILE}`,
    cwd: APP_DIR,
    interpreter: 'none', // 使用的解释器,没有的话默认使用node解释
    
    // 日志文件路径固定
    error_file: path.join(APP_DIR, 'logs', 'app-error.log'),
    out_file: path.join(APP_DIR, 'logs', 'app-out.log'),
//    log_date_format: 'YYYY-MM-DD HH:mm:ss',
    time: false
    
    // PM2监控参数可以灵活调整
//    max_memory_restart: MAX_MEMORY_RESTART,
//    autorestart: true,
//    watch: true
  }]
};

二、配置项目运行和重启的脚本

创建一个rp-restart.sh的文件:

#!/bin/bash

APP_DIR="/home/javaapp/RentPay"
APP_NAME="RentPay"
CONFIG_FILE="RentpayEco.config.js"


# 切换到应用目录
cd "$APP_DIR" || {
    echo "Error: Cannot change to $APP_DIR"
    exit 1
}

echo "Restarting $APP_NAME..."

# 停止应用
if pm2 stop "$APP_NAME"; then
    echo "Application stopped successfully"
else
    echo "Warning: Application may not have been running or failed to stop"
fi


# 启动应用
if pm2 start "$CONFIG_FILE"; then
    echo "$APP_NAME application restarted successfully"
    
    # 可选:保存pm2配置
    #pm2 save
    
else
    echo "Error: Failed to start application"
    exit 1
fi

windows版本(ps1文件)
创建 restart_app.ps1文件:

$APP_DIR = "C:\path\to\RentPay"
$APP_NAME = "RentPay"
$CONFIG_FILE = "RentpayEco.config.js"

# 切换到应用目录
try {
    Set-Location -Path $APP_DIR -ErrorAction Stop
} catch {
    Write-Error "Error: Cannot change to $APP_DIR"
    exit 1
}

Write-Host "Restarting $APP_NAME..."

# 停止应用
pm2 stop $APP_NAME
if ($LASTEXITCODE -eq 0) {
    Write-Host "Application stopped successfully"
} else {
    Write-Host "Warning: Application may not have been running or failed to stop"
}

# 启动应用
pm2 start $CONFIG_FILE
if ($LASTEXITCODE -eq 0) {
    Write-Host "$APP_NAME application restarted successfully"
    
    # 可选:保存pm2配置
    # pm2 save
    
} else {
    Write-Error "Error: Failed to start application"
    exit 1
}

三、 生成快速链接并运行

#生成全局链接命令:

# 1. 删除旧的符号链接
sudo rm /usr/local/bin/qt-restart

# 2. 重新创建符号链接
sudo ln -s /home/javaapp/RentPay/rp-restart.sh /usr/local/bin/rp-restart

# 3. 确保原始脚本有执行权限
chmod +x /home/javaapp/RentPay/rp-restart.sh

# 4. 测试是否生效
rp-restart

四、设置PM2开机自启动

1.查看命令pm2 ls 或pm2 status
1.保存当前进程状态 pm2 save
2. 生成开机自动启动脚本 pm2 startup

[root@localhost nodeldsdoc]# pm2 ls

[root@localhost nodeldsdoc]# pm2 save
[PM2] Saving current process list...
[PM2] Successfully saved in /root/.pm2/dump.pm2
[root@localhost nodeldsdoc]# pm2 startup
[PM2] Init System found: systemd
(中间省略。。。。)
$ pm2 unstartup systemd
[root@localhost nodeldsdoc]# 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值