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
Related
I am using pm2 for managing node processes on one of the servers.
The package is here: https://pm2.keymetrics.io/
It is open source and available both on npmjs and GitHub.
I can easily install it every time using: npm i pm2 -g
I love pm2, and not just node processes, I write bash scripts and run them as cron under pm2 and I can easily check the logs.
Some commands:
pm2 --name "process-name" start "bash script.sh"
pm2 --name "node-process" start "node main.js"
pm2 logs node-process
pm2 stop node-process
pm2 restart node-process
There are 2 more commands which are very useful to start pm2 on startup with all the processes automatically.
pm2 startup Will generate startup script.
pm2 save Will update start script with current processes.
Everything is good. But, today I got into a problem.
I am running all pm2 node processes from a folder /mnt/node.
What I want is that I have synced that /mnt/node folder to another server and I am trying to find a way to move all pm2 processes automatically to another server without writing each process once again.
May be someone can help.
You can do this.
On the source server:
pm2 save
copy file saved on ~/.pm2/dump.pm2 to destination server, then:
pm2 resurrect
Haven't try this between two differents server yet but i think it will be ok.
I'm using a library, say I want the script that starts my app to be cli-lib start how can i go about that? I don't want to just run node app.js or any js file for that matter, the cli lib does that for me but i cant figure out a way to get this to work.
Yes you can run any process type with pm2.
For scripts in other languages which has assigned an interpreter by default,
pm2 start echo.coffee
pm2 start echo.php
pm2 start echo.py
pm2 start echo.sh
pm2 start echo.rb
or with interpreter
pm2 start echo.pl --interpreter=perl
read more from docs
I think you can create a bash script then run it with pm2, for examples:
bash.sh
#!/usr/bin/bash
node /home/user/test.js
then you can run file "bash.sh" with pm2
pm2 start bash.sh
I did not test it yet but you can try.
Yes we can use & operate pm2 related command with script in Linux.
create script file for run.
$ sudo nano runpm2.sh
add your relevant command for run pm2. Also you can visit for more command on this site
#!/usr/bin/bash
pm2 restart "a"
pm2 restart "b"
pm2 restart "c"
pm2 start "app.js"
pm2 restart "app_name"
pm2 reload "app_name"
pm2 stop "app_name"
pm2 delete "app_name"
Assign access for run script.
sudo chmod +x runpm2.sh
run .sh file with this command.
sudo ./runpm2.sh
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.
I have a node app that I would like to run at startup but I cannot get it to work. I am running Ubuntu 14.04. The first command starts the process and works as intended but when I run pm2 startup and pm2 save from below and reboot the box I have no luck with the app launching at boot.
# Start the process
NODE_ENV="production" pm2 start /home/expressjs/app_name/app.js -u expressjs --watch --name "app_name"
# Create a startup script and save the process.
sudo env PATH=$PATH:/usr/local/bin pm2 startup ubuntu -u expressjs
pm2 save
I use Putty.
When I start server with "node X.js", putty start server running.
If I exit from putty the server stop.
How I can keep it running and make it default running after restart or reboot the server (computer)?
I have centos 5.10.
Thank you!
I use pm2 to do it
To install pm2
sudo npm install -g pm2
To generate startup script
pm2 startup ubuntu(centos in your case)
Then pm2 will prompt the command for you to run, in my case, it is like
PM2 You have to run this command as root
PM2 Execute the following command :
PM2 sudo env PATH=$PATH:/usr/bin pm2 startup ubuntu -u USERNAME
Then you could run
sudo env PATH=$PATH:/usr/bin pm2 startup ubuntu -u USERNAME
Then you could see
PM2 Generating system init script in /etc/init.d/pm2-init.sh
PM2 Making script booting at startup...
PM2 -ubuntu- Using the command su -c "chmod +x /etc/init.d/pm2-init.sh && update-rc.d pm2-init.sh defaults"
Adding system startup for /etc/init.d/pm2-init.sh ...
/etc/rc0.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc1.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc6.d/K20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc2.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc3.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc4.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
/etc/rc5.d/S20pm2-init.sh -> ../init.d/pm2-init.sh
PM2 Done.
Once you have started the apps and want to keep them on server reboot do:
pm2 save
You can refer to Startup script section in https://github.com/Unitech/pm2#startup-script
There are several ways, I personally like forever.
sudo npm install -g forever
forever start app.js &
note that ending with & will fork the process to background.
You can later check the process with
forever list
To run it when your system restarts you can add to cron
#reboot forever start app.js &> /dev/null
Remember to point to the absolute location of app.js