Skip to content

Instantly share code, notes, and snippets.

@SrShark
Last active December 2, 2021 15:13
Show Gist options
  • Save SrShark/e92ad6c9984b8f96b6c24705a4c0d1db to your computer and use it in GitHub Desktop.
Save SrShark/e92ad6c9984b8f96b6c24705a4c0d1db to your computer and use it in GitHub Desktop.
Establecer PM2 como servicio con SystemD

Instalación del servicio de PM2 en ubuntu

pm2 startup systemd

output:

Platform systemd
Template
[Unit]
Description=PM2 process manager
Documentation=https://pm2.keymetrics.io/
After=network.target

[Service]
Type=forking
User=root
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/root/.pm2
PIDFile=/root/.pm2/pm2.pid
Restart=on-failure

ExecStart=/usr/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/lib/node_modules/pm2/bin/pm2 kill

[Install]
WantedBy=multi-user.target

Target path
/etc/systemd/system/pm2-root.service
Command list
[ 'systemctl enable pm2-root' ]
[PM2] Writing init configuration in /etc/systemd/system/pm2-root.service
[PM2] Making script booting at startup...
[PM2] [-] Executing: systemctl enable pm2-root...
Created symlink from /etc/systemd/system/multi-user.target.wants/pm2-root.service to /etc/systemd/system/pm2-root.service.
[PM2] [v] Command successfully executed.
+---------------------------------------+
[PM2] Freeze a process list on reboot via:
$ pm2 save

[PM2] Remove init script via:
$ pm2 unstartup systemd

Correr el suiguiente comando para guardar de forma efectiva los cambios en el sistema.

pm2 save

Iniciar el servicio en el sistema.

Al configurar el servicio PM2 le asigna el nombre de: pm2-root, siendo root el nombre del usuario quien lo configuró.

systemctl start pm2-root

Podemos verificar si el servicio esta activo con:

systemctl status pm2-root

Por último revisamos el estado de pm2 para estar seguros de que todo este corriendo.

pm2 status
@SrShark
Copy link
Author

SrShark commented Dec 2, 2021

@oropezaC Mira, yo lo tengo corriendo con la versión más reciente de pm2 sobre ubuntu 18.04. Pero cualquier distro con Systemd te debería de funcionar.

Te dejo este artículo de Digital Ocean de Cómo usar Systemctl para gestionar servicios y unidades de Systemd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment