ubuntu - unable to set pm2 as service on boot - node.js

I'm launching an application and am able to use PM2 to run it however I'm looking at making sure PM2 launches on reboot of the server.
I have two users, root and a user for running the application. The user has sudo privileges.
Currently I am typing this command:
sudo env PATH=$PATH:/usr/local/bin /usr/local/lib/node_modules/pm2/bin/pm2 startup systemd -u USER --hp /home/USER
and the result:
[ERROR] Exit code : 1
[PM2][ERROR] systemctl start pm2-paul failed, see error above.
When I check jorunalctl for details, I see this:
pm2-user.service: PID file /home/user/.pm2/pm2.pid not readable (yet?) after
start: No such file or directory
Failed to start PM2 process manager.
Any help would be greatly appreciated, I've been struggling for a few hours now with this.

Related

Crontab failing to restart systemctl process

Hi im trying to setup a cron job to restart a game server daily but its failing to work
i have used crontab -e to install a job
* 3 * * * systemctl --user restart <servicename>
this failed to restart my process at 3 am as expected.
i also tried moving the command to a bash file and running the bash file through cron (as i have seen alot of people use .sh files to run commands)
additionally i have tried installing to job as root with zero success
this had similar results
my syslog did show a command execute as expected yesterday though the service didnt restart, since then cron isnt showing any new attempts.
i found some info on troubleshooting and following the guidance shows that my cron is working, i managed write the date/time to a file every minute and the service is clearly running and this showed in the logs.
is there a problem with cron running systemctl commands or is there further steps i can use to see why its failing?
Edit:
i tried running my command as root also and nothing happened
Jan 31 09:00:01 ns509515 CRON[108307]: (root) CMD (<user> systemctl --user restart EcoServer)
Jan 31 09:01:01 ns509515 CRON[108330]: (root) CMD (<user> systemctl --user restart EcoServer)
Jan 31 09:02:01 ns509515 CRON[108356]: (root) CMD (<user> systemctl --user restart EcoServer)

GitHub action self-host runner as service fails

I have installed GitHub self-hosted runner on my Ubuntu system. Getting below error when i try to configure it as service.
$ sudo ./svc.sh start
Failed to start actions.runner._services.Linux-Host01.service: Unit actions.runner._services.Linux-Host01.service is not loaded properly: Exec format error.
See system logs and 'systemctl status actions.runner._services.Linux-Host01.service' for details.
Failed: failed to start actions.runner._services.Linux-Host01.service
$ systemctl status actions.runner._services.Linux-Host01.service
● actions.runner._services.Linux-Host01.service - GitHub Actions Runner (_services.Linux-Host01)
Loaded: error (Reason: Exec format error)
Active: inactive (dead)
$ cat /etc/systemd/system/actions.runner._services.Linux-Host01.service
[Unit]
Description=GitHub Actions Runner (_services.Linux-Host01)
After=network.target
[Service]
ExecStart=/home/admin.user/actions-runner/runsvc.sh
User=admin.user
WorkingDirectory=/home/admin.user/actions-runner
KillMode=process
KillSignal=SIGTERM
TimeoutStopSec=5min
[Install]
WantedBy=multi-user.target
$ sudo journalctl -u actions.runner._services.Linux-Host01.service -f
Aug 04 08:40:47 Linux-Host01 systemd[1]: /etc/systemd/system/actions.runner._services.Linux-Host01.service:7: Invalid user/group name or numeric ID: admin.user
Additionally have provided executable permission to actions.runner._services.Linux-Host01.service but still it results same error.
What is wrong here?
I have changed User=admin.user to User=uid in the /etc/systemd/system/actions.runner._services.Linux-Host01.service file and then executed
systemctl daemon-reload
Now action service started and it is running fine.
Good morning,
First of all stop the service:
sudo ./svc.sh stop
Then make sure you have given permissions to the user:
sudo usermod -a -G <USER>
Now try to start the service:
sudo ./svc.sh start
And tell me if this works when check the status.
If not works please do the same but sudo permissions:
sudo su
And then try again all without sudo command because you are actually on root.
Have a great day!
This simply means you've not installed the runner
sudo ./svc.sh install // install the runner
sudo ./svc.sh start // then start it
Remember, this is the best way to go about it than using the ./run.sh script, as the runner will always be running in the background.
Use sudo ./svc.sh status to confirm that the runner is up and running.

Does pm2 auto restart application after reboot by default?

I have an issue with my linux server and need to reboot, before that I run my node application with pm2 start server.js without any other config. Can it auto restart my app after reboot server?
Not by default, but PM2 can do so using a startup script:
PM2 can generate startup scripts and configure them in order to keep
your process list intact across expected or unexpected machine
restarts.
After generating your startup script (read also this comment), take a look at pm2 save:
Once you started all the applications you want to manage, you have to
save the list you wanna respawn at machine reboot with:
pm2 save
You can use this script before run pm2 save:
pm2 startup
[PM2] You have to run this command as root. Execute the following command:
sudo su -c "env PATH=$PATH:/home/unitech/.nvm/versions/node/v14.3/bin pm2 startup <distribution> -u <user> --hp <home-path>
https://pm2.keymetrics.io/docs/usage/startup/
First of all I run below command
pm2 save
Then edit crontab
nano /etc/crontab
Then Add
* * * * * pm2 resurrect

How to start Jenkins with a different user?

I am working with Jenkins and I want to start a script with forever using Jenkins with a different user named aroot.
So my build configuration I write this command:
sudo -u aroot forever start -a --uid 'server' bin/www
It works fine but forever still tries to access the jenkins user and when I try to see the scripts running under forever by using the command:
forever list
I see nothing. Why does this happen? I have changed the user to aroot but it still tries to start the script under jenkins user. What should I do here?
I even tried changing the default jenkins user to aroot but after this, the Jenkins just does not restart.
you should not need the forever thing going. just enable it from systemctl
# systemctl enable jenkins.service
how you get it to run as different users is by editing this file.
# vi /etc/sysconfig/jenkins
and changing this line to whatever user you want.
JENKINS_USER="aroot"
When you restart it will be running as that user but you will need to chown all files in /var/lib/jenkins so it has the correct ownership.

How to use jenkins to restart PM2?

i'm running my node.js app on the linux server using PM2, with a config file, like this:
PM2 start mywebsite.config.js
all is good. but now i want to add jenkins to the picture.
i'm running a pipeline project in jenkins, using Jenkinsfile.
All working fine except for the last command, that should restart the app, to make the new version live:
stage('Restart PM2') {
steps {
sh 'pm2 restart all' }
}
}
and this command fails. here is the log output:
+ pm2 restart all
Use --update-env to update environment variables
[PM2][WARN] No process found
< empty pm2 log table here>
Use `pm2 show <id|name>` to get more details about an app
I understand that PM2 is working per user. means, that the user who ran the first command (start) is the one that should run the restart as well.
but how to do this?
To run pm2 restart all from Jenkins you need to:
Configure your system to run sudo from jenkins
(https://sgoyal.net/2016/11/18/run-a-shell-from-jenkins-using-sudo-ubuntu/)
Make a symbolic link to the .pm2/ folder, in my case(Ubuntu) it was at /root/.pm2 so i run
sudo ln -s /root/.pm2/ /var/lib/jenkins/
NOTE: /var/lib/jenkins if the default jenkins root directory, you can check yours on Jenkins configuration
after that you can go to jenkins and setup a shell command, in my case i did:
#!/bin/sh
echo "RESTARTING ALL"
sudo pm2 restart all
echo "ALL RESTARTED"
NOTE: if you have a .pm2 folder already in your jenkins root directory rename it so you can do the symbolic link
Hope this helps
Instead of restarting PM2 through you jenkins code, let PM2 do it by itself, using the watch flag. in your config file, set watch to be true.
You may want to add a relatively new flag called watch-ignore. that's an array, with files to be ignored by the watch. add your log file and error file to this list. otherwise, any logged information will cause your node app to restart endlessly.
after doing these changes to the config file, run pm2 again with the config. remove the restarting code from Jenkinsfile, you don't need that anymore, pm2 will detect the new version and will reload the app!
BUILD_ID=dontKillMe PM2 start mywebsite.config.js
Jenkins kills the pm2 daemon to be created by the build.
You should put the keyword to prevent killing daemon by Jenkins.

Resources