Running node.js pm2 remotely using pdsh - node.js

I have 4 servers running node.js with the help of pm2 (pm2 start app.js). I would like to restart pm2 on all 4 servers from another server using pdsh however when I run:
pdsh -a "/root/.nvm/v0.10.30/bin/pm2 restart 0"
it returns:
/root/.nvm/v0.10.30/bin/pm2: line 4: exec: : not found
I have also done similar to bellow:
pdsh -a "sh /etc/profile && sh ~/.bash_profile && /root/.nvm/v0.10.30/bin/pm2 restart 0"
and same error occurs.
Thanks

Try to run pm2 directly from the node path. It appears that pdsh can't load nvm
If you've installed it from nvm it should be something like that:
~/.nvm/v0.10.30/bin/node
Pm2 on the other hand should be in the node_modules directory:
~/.nvm/v0.10.30/lib/node_modules/pm2/bin/pm2
So if we combine all this it becomes:
/root/.nvm/v0.10.30/bin/node /root/.nvm/v0.10.30/lib/node_modules/pm2/bin/pm2 restart 0

Related

Nestjs with pm2, port still be used after killing the process with pm2 stop?

I deployed my Nest.js application on my VPS (Ubuntu 20.04 DO) using pm2 as process manager. Here is my step by step when I'm updating the app.
$ pm2 stop 1
$ kill -9 $(sudo lsof -t -i:4040)
$ npm run build
$ pm2 start 1
Notice that I must kill the port that the app use before I proceed to the build one, how to simplify it and become like these :
$ npm run build
$ pm2 reload 1
So I can deploy my Nest.js app gracefully and achieve at least only 1% downtime
Stop command keeps the app in the apps list while the delete command not.
I think you want something like
start.sh
#!/bin/bash
source .env
ENVIRONMENT="$NODE_ENV"
npm run build || exit
pm2 delete "$ENVIRONMENT"
pm2 start "npm run start:prod" --name "$ENVIRONMENT" --log-date-format 'DD-MM HH:mm:ss.SSS'
If you are not using different environments, an equivalent script would be
#!/bin/bash
npm run build || exit
pm2 delete my_application
pm2 start "npm run start:prod" --name my_application --log-date-format 'DD-MM HH:mm:ss.SSS'
Emitting the --log-date-format is perfectly fine. However, I have included it because I suspect it might become helpful when finding bugs in production down the line.

pm2 - How to start if not started, kill and start if started

I am trying to start pm2 if it is not running, or kill it and start if it is, how can I achieve this behavior in the WINDOWS command line interface?
There are plenty of solutions using grep in linux but nothing for windows, any idea on how to get this behaviour?
The documentation says that pm2 start -f app.js will kill and start the app but it actually just creates another instance.
Use this:
pm2 delete main.js 2> /dev/null && pm2 start main.js
This part: 2> /dev/null - will simply redirect the stderr to the /dev/null, meaning to nowhere.
It does not seem there is a "single command" way to do this, which is rather important in many development environments, so here are some options:
put soyuka's suggestion on one line.
pm2 stop myprocess; pm2 start myprocess.js
This will output errors, but it will work.
They also have this option built into their ecosystem tools. To use this, go into the folder you are working with and run
pm2 ecosystem
This will generate a file ecosystem.config.js which you will need to make sure your name and script are correct within.
You can then use the command:
pm2 startOrReload ecosystem.config.js
I, however also want to see my logging, so I use this command:
pm2 flush && pm2 startOrReload ecosystem.config.js && pm2 log
This will also flush the logs so you are not seeing old logs.
You can do something like this
pm2 delete your_app_name || : && pm2 start index.js -i 1 --name 'your_app_name'
The : is a null operator that returns 0 success exit code. So whatever happens, pm2 start command will execute (even if pm2 delete fails, for the case where the app does not exist yet).
I'd do this :
pm2 stop myprocess.js #this will just say process not found
pm2 start myprocess.js
Or if you want to clear everything :
pm2 kill
pm2 stop
If you want more advanced possibilities check out the pm2 api.

Keep meteor running on amazon EC2

I have a simple meteor app that I'm running on an Amazon EC2 server. Everything is working great. I start it manually with my user via meteor in the project directory.
However, what I would like is for this app to
Run on boot
Be immune to hangups
I try running it via nohup meteor &, but when I try to log out of the EC2 instance, I get the "You have running jobs" message. Continuing to log out stops the app.
How can I get the app to start on startup and stay up (unless it crashes for some reason)?
Install forever and use a start script.
$ npm install -g forever
I have several scripts for managing my production environment - the start script looks something like:
#!/bin/bash
forever stopall
export MAIL_URL=...
export MONGO_URL=...
export MONGO_OPLOG_URL=...
export PORT=3000
export ROOT_URL=...
forever start /home/ubuntu/apps/myapp/bundle/main.js
exit 0
Conveniently, it will also append to a log file in ~/.forever which will show any errors encountered while running your app. You can get the location of the log file and other stats about your app with:
$ forever list
To get your app to start on startup, you'd need to do something appropriate for your flavor of linux. You can maybe just put the start script in /etc/rc.local. For ubuntu see this question.
Also note you really should be bundling your app if using it in production. See this comparison for more details on the differences.
I am using upstart on Ubuntu server which you should be able to easily install on Amazon linux.
This is roughly my /etc/init/myapp.conf:
start on (local-filesystems and net-device-up IFACE=eth0)
stop on shutdown
respawn
respawn limit 99 5
script
export HOME="/home/deploy"
export NODE_ENV="production"
export MONGO_URL="mongodb://localhost:27017/myappdb"
export ROOT_URL=http://localhost
export MAIL_URL=smtp://localhost:25
export METEOR_SETTINGS='{"somesetting":true}'
cd /var/www/myapp/bundle/
exec sudo -u deploy PORT=3000 /usr/bin/node main.js >> /var/log/node.log 2>&1
end script
I can then manually start and stop myapp like this:
sudo start myapp
sudo stop myapp
I believe this package solves your problem: https://github.com/arunoda/meteor-up
which seems to use forever: https://github.com/nodejitsu/forever

Bash script for Ghost blog not starting up on server reboot

I have a very simple bash script which should launch my ghost blog. I am using crontab to launch the script on startup, here is the crontab command I am running:
#reboot /var/www/ghost/launch.sh
The script has the following code:
#!/bin/sh
ps auxw | grep apache2 | grep -v grep > /dev/null
if [ $? != 0 ]
then
NODE_ENV=production forever start --sourceDir /var/www/ghost index.js
fi
When I sudo reboot the server, and use forever list to find the processes running, I see the following:
data: [0] sHyo /usr/bin/nodejs index.js 1299 1314 /home/webadmin/.forever/sHyo.log 0:0:1:25.957
When I nano to that log file, the log says the following:
^[[31m
ERROR:^[[39m ^[[31mCould not locate a configuration file.^[[39m
^[[37m/home/webadmin^[[39m
^[[32mPlease check your deployment for config.js or config.example.js.^[[39m
Error: Could not locate a configuration file.
at checkTemplate (/var/www/ghost/core/config-loader.js:16:36)
at Object.cb [as oncomplete] (fs.js:168:19)
error: Forever detected script was killed by signal: null
It appears to be looking in /home/webadmin/, but ghost is installed at /var/www/ghost????
When I run the exact same script in the terminal manually after the sever has started up by ssh-ing into the server, the script works fine. I run: cd /var/www/ghost/ and then ./launch.sh and the ghost blog appears and is working fine. The log for that forever process says the following:
^[[32mGhost is running...^[[39m
Your blog is now available on http://blog.example.com ^[[90m
Ctrl+C to shut down^[[39m
What is wrong with my script or crontab that it cannot launch the script properly?
I run: cd /var/www/ghost/ and then ./launch.sh and the ghost blog appears and is working fine.
That's the thing, your cron job is not doing the same:
#reboot /var/www/ghost/launch.sh
This script is executed from your home directory. One way to fix is to change your crontab:
#reboot cd /var/www/ghost; ./launch.sh
Another way is to add this line near the top of launch.sh, anywhere before launching forever:
# change to the directory of this script
cd $(dirname "$0")
Just an FYI for anybody that runs across this I would highly suggest looking into pm2 to start Ghost and to monitor Ghost. It will monitor Ghost like Forever and has a built in feature to generate a init script to start pm2 when your server restarts. Also has better features to monitor Ghost while it is running. Check out my how to here.

Node.js - Unable to run node server using forever

I am trying to run node server using forever command.
I installed forever globally using:
npm install forever -g
After installing forever I try to run my node script by using below command:
node_modules\.bin\forever start app.js
Below is my console:
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up f
or at least 1000ms
info: Forever processing file: app.js
Please help me to resolve this issue!
There is no problem here other than warnings for configs forever recommends you declare. If you see the final message there it tells you it has processed your script. Just run forever list and you should see your script running.
I ran into this same thing when installing npm via yum repository ( yum install npm ) and then installing forever whereas when I install node and npm via shell scripts and then install forever it doesn't occur. It must have something to do with the formulas for the package installer or potentially missing alias with flags with installer to set those values behind the scene.
Those don't mean it's not working. See below I created a js file using sample code from node's site and ran it manually (I flushed firewall to open port for app temporarily but you don't need that):
[root#app1 ~]# vi example.js
[root#app1 ~]# apf -f
apf(23924): {glob} flushing & zeroing chain policies
apf(23924): {glob} firewall offline
[root#app1 ~]# node example.js
Server running at http://127.0.0.1:1337/
I then start app using forever:
^C[root#app1 ~]# forever start example.js
warn: --minUptime not set. Defaulting to: 1000ms
warn: --spinSleepTime not set. Your script will exit if it does not stay up for at least 1000ms
info: Forever processing file: example.js
Now I check to see if my app is running:
[root#app1 ~]# forever list
info: Forever processes running
data: uid command script forever pid logfile uptime
data: [0] dan1 /usr/bin/node example.js 23976 23978 /root/.forever/dan1.log 0:0:0:27.320
[root#app1 ~]#
This solved my issue:
forever start -c node [path/to/app]
"-c" means - Run commnad; and then just run via nodejs
This way - you get the Respawn by default of min. 1000ms uptime
Taken from: https://github.com/nodejitsu/forever/issues/422, by "Basarat"
If you are using node js with express framework then script will not start using :
forever start app.js
First stop all running apps:
forever stopall
When this Express framework used it must be started with:
forever start ./bin/www
First stop all running apps:
forever stopall
then use this command. It works for me and solved my issue:
forever -w ./bin/www
and you should find this in package.json file:
"scripts": {
"start": "node ./bin/www"
}
I hope it helps you.
One thing that also produces same kind of output, but doesn't start the application is if forever is unable to write to the specified log file. I had a case where the log file had become too big and this prevented the process from starting.
Firstly change your package.json scripts like
"scripts": {
"start": "forever ./bin/www.js"
}
than start this command on linux console:sudo npm start
for windows just :npm start
I just ran into this today on an AWS Lightsail server, and NONE of the answers here or elsewhere had any effect. Everything worked fine until upgrading from NodeJS 10.x to 13.x. I tried removing and reinstalling forever, changing the permissions on the files and directories, etc, and I kept getting the EACCES error. The issue seemed to be that forever could not create directories within its .forever directory. The only thing that worked was to do the following:
1) Remove the .forever folder and all its subfolders and contents. For me, this was accomplished as follows:
sudo rm -rf /home/bitnami/.forever
2) Manually recreate the .forever folder:
sudo mkdir /home/bitnami/.forever
3) Manually set the permissions on the .forever folder:
sudo chmod -R o+rwx /home/bitnami/.forever
4) Manually recreate the .forever/pids folder:
sudo mkdir /home/bitnami/.forever/pids
5) Manually set the permissions on the .forever/pids folder:
sudo chmod -R o+rwx /home/bitnami/.forever/pids
6) Manually recreate the .forever/sock folder:
sudo mkdir /home/bitnami/.forever/sock
7) Manually set the permissions on the .forever/sock folder:
sudo chmod -R o+rwx /home/bitnami/.forever/sock
8) Run my NodeJS app via forever again with the sudo command.
9) List the processes forever is running, and verify that my app was there.
I'm not sure why I had to go through all of this, as setting the permissions recursively should have done the same thing, but after doing this, forever started running perfectly as it did before.
Hope this helps someone.
forever stopall
cd /<app-folder>
forever -w ./bin/www
This just worked for me in a Google Cloud Bitnami VM
If you setup a module globaly (-g option) "forever" is in the $path
forever start app.js
should work.

Resources