Skip to content

6️⃣ PM2 and SystemD Integration

Step 1 — Install PM2 globally

sudo npm install pm2@latest -g

Step 2 — Start both bots under PM2

cd ~/gunbot-live
pm2 start ./gunthy-linux --name gunbot-live --time

cd ~/gunbot-sim
pm2 start ./gunthy-linux --name gunbot-sim --time

Check:

pm2 list


Step 3 — Enable PM2 persistence

pm2 save
pm2 startup systemd -u gunbot --hp /home/gunbot
sudo env PATH=$PATH:/usr/bin pm2 startup systemd -u gunbot --hp /home/gunbot

Note:
The second command registers PM2 for the gunbot user so it restarts automatically after reboots.


Step 4 — Create a systemd override for clean restarts

sudo systemctl edit pm2-gunbot.service

Insert:

[Service]
Type=simple
ExecStart=
ExecStart=/usr/bin/pm2 resurrect
Restart=on-failure
User=gunbot
Environment=PM2_HOME=/home/gunbot/.pm2

Reload systemd and enable persistence:

sudo systemctl daemon-reload
sudo systemctl enable pm2-gunbot
sudo systemctl restart pm2-gunbot

Check status:

sudo journalctl -u pm2-gunbot -n 20 --no-pager