How to use pm2

Comprehensive pm2 code examples:

How to use pm2.delete:

73
74
75
76
77
78
79
80
81
82
if (error) {
  console.log(`Raneto could not be stopped: ${error.msg}`);
  process.exit(1);
}
// (error, proc)
pm2.delete('raneto', (error) => {
  if (error) {
    console.log(`Raneto could not be stopped: ${error.msg}`);
    process.exit(1);
  } else {

How to use pm2.stop:

67
68
69
70
71
72
73
74
75
76
.command('stop')
.description('Stop Raneto Service')
.action(() => {
  pm2.connect(() => {
    // (error, proc)
    pm2.stop('raneto', (error) => {
      if (error) {
        console.log(`Raneto could not be stopped: ${error.msg}`);
        process.exit(1);
      }

How to use pm2.start:

36
37
38
39
40
41
42
43
44
45
program
  .command('start')
  .description('Start Raneto Service')
  .action(() => {
    pm2.connect(() => {
      pm2.start(
        {
          script: path.normalize(path.join(__dirname, '..', 'server.js')),
          exec_mode: 'fork',
          instances: 1,

How to use pm2.launchBus:

62
63
64
65
66
67
68
69
70
71
                currentPmID = item.pm_id;
            }
        }
    }
);
pm2.launchBus((err, bus) => {
    /** If a startup error occurs, an error is thrown directly */
    if (err) throw err;

    const DingTalkSender = new DingTalk(conf);

How to use pm2.list:

51
52
53
54
55
56
57
58
59
60
61
 */
function addWatchdogOfExistingProcesses() {
    console.trace('Getting list of processes.');


    // Gets list of process managed by pm2
    pm2.list(
        /**
         *
         * @param {Error|null} err
         * @param {ProcessDescription[]} apps

How to use pm2.disconnect:

61
62
63
64
65
66
67
68
69
70
 */
function(err, apps) {

    if (err) {
        console.error(`PM2 get process list failed. ${err.message || err}`);
        pm2.disconnect();
        return;
    }

    apps.forEach((/*ProcessDescription*/ processDescription) => {

How to use pm2.emit:

133
134
135
136
137
138
139
140
141
142
    };
}
portfolio[symbol].balance += data.executedQty;
portfolio[symbol].purchasePrice = (portfolio[symbol].purchasePrice * (portfolio[symbol].balance - data.executedQty) + data.price * data.executedQty) / portfolio[symbol].balance;
portfolio.USDT.balance -= data.executedQty * data.price;
pm2.emit('trade_log', {
    name: 'trade_log',
    tradeType: 'buy',
    symbol: symbol,
    price: data.price,

How to use pm2.describe:

109
110
111
112
113
114
115
116
117
118
program
  .command('logs')
  .description('Show Raneto Logs')
  .action(() => {
    pm2.connect(() => {
      pm2.describe('raneto', (error, list) => {
        if (error) {
          console.log(`Error getting info: ${error.msg}`);
          process.exit(1);
        } else if (list.length === 0) {

How to use pm2.default:

256
257
258
259
260
261
262
263
264
265
266


// server/servers/pm2Servers.ts
function pm2List(req, res) {
  return __async(this, null, function* () {
    try {
      import_pm2.default.list((error, list) => {
        defaultSucesshandler(res, list);
      });
    } catch (error) {
      defaultErrorHandler(res, error);

How to use pm2.connect:

35
36
37
38
39
40
41
42
43
44
45


program
  .command('start')
  .description('Start Raneto Service')
  .action(() => {
    pm2.connect(() => {
      pm2.start(
        {
          script: path.normalize(path.join(__dirname, '..', 'server.js')),
          exec_mode: 'fork',

How to use pm2.restart:

92
93
94
95
96
97
98
99
100
101
.command('restart')
.description('Restart Raneto Service')
.action(() => {
  pm2.connect(() => {
    // (error, proc)
    pm2.restart('raneto', (error) => {
      if (error) {
        console.log(`Raneto could not be restarted: ${error.msg}`);
        process.exit(1);
      } else {