I think that was the approach that I followed and it seemed to take some gymnastics to make work. I think I might have to revisit what I did as the configuration that Alan posted might fix some of the oddities I run into. I suspect my approach was not optimal. In either case I think I did something like the following. I have it all in an Ansible playbook and once I got it working I kind of left it.
My systemd unit file (/etc/systemd/system/pm2-dspace.service) looks like the following (running RHEL 8.9):
[Unit]
Description=PM2 process manager
Documentation=
https://pm2.keymetrics.io/After=network.target
[Service]
Type=forking
User=dspace
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
Environment=PATH=/home/dspace/.local/bin:/home/dspace/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/usr/bin:/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin
Environment=PM2_HOME=/home/dspace/.pm2
PIDFile=/home/dspace/.pm2/pm2.pid
Restart=on-failure
ExecStart=/usr/local/lib/node_modules/pm2/bin/pm2 resurrect
ExecReload=/usr/local/lib/node_modules/pm2/bin/pm2 reload all
ExecStop=/usr/local/lib/node_modules/pm2/bin/pm2 kill
[Install]
WantedBy=multi-user.target
To get this to work though I believe I needed to "sudo npm install --global pm2"
Then as the dspace user I created /opt/dspace-angular/dspace-ui.json with
{
"apps": [
{
"name": "dspace-ui",
"cwd": "{{ dspace_ui_deploy_dir }}",
"script": "dist/server/main.js",
"instances": "max",
"exec_mode": "cluster",
"env": {
"NODE_ENV": "production"
}
}
]
}
And then as the dspace user:
/usr/local/bin/pm2 start dspace-ui.json /
/usr/local/bin/pm2 save
/usr/local/bin/pm2 stop dspace-ui.json
/usr/local/bin/pm2 kill
At this point on a reboot systemd will start the pm2 dspace-ui but using systemd to stop pm2 leaves things in a odd state, possibly because PM2 God Daemon is running. As the dspace use I am able to pm2 stop dspace-ui and do some of the various other pm2 commands (I just added another cpu and just did a pm2 scale dspace-ui +1 and then a pm2 save and on a reboot systemd resurrected the saved config so I went from 2 workers to 3. I admit though that I do not and did not fully understand what pm2 was doing. I was just happy that I was able to make it work on a reboot.