2023-04-10 12:08:09 +00:00
|
|
|
const pm2 = require('pm2');
|
2023-04-10 14:29:16 +00:00
|
|
|
const { execSync } = require("child_process");
|
2023-04-10 12:08:09 +00:00
|
|
|
|
|
|
|
function startPm2Process(process_name, file, path, port) {
|
|
|
|
|
|
|
|
pm2.connect(function (err) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
// process.exit(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
pm2.start({
|
|
|
|
name: process_name,
|
|
|
|
script: file,
|
|
|
|
cwd: path,
|
|
|
|
env: {
|
|
|
|
PORT: port
|
2023-04-10 14:29:16 +00:00
|
|
|
}
|
2023-04-10 12:08:09 +00:00
|
|
|
// additional options here if needed
|
|
|
|
}, function (err, apps) {
|
|
|
|
if (err) {
|
|
|
|
console.error(err);
|
|
|
|
// process.exit(2);
|
|
|
|
}
|
2023-04-10 14:29:16 +00:00
|
|
|
else {
|
|
|
|
execSync(`pm2 save --force`, { cwd: path }, (error, stdout, stderr) => {
|
|
|
|
if (error) {
|
|
|
|
console.log(`error: ${error.message}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (stderr) {
|
|
|
|
console.log(`stderr: ${stderr}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log(`stdout: ${stdout}`);
|
|
|
|
});
|
|
|
|
}
|
2023-04-10 12:08:09 +00:00
|
|
|
|
|
|
|
pm2.disconnect();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = startPm2Process
|