PM2 installation not generating script - node.js

I need to deploy a NodeJs application following this tutorial https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-ubuntu-16-04.
When installing PM2 module and generating pm2 daemon script pm2 startup upstart, no command is shown, normally it should display a command like this:
sudo env PATH=$PATH:/usr/bin /usr/lib/node_modules/pm2/bin/pm2 startup systemd -u sammy --hp /home/sammy
NodeJs v0.10.48 (on Ubuntu-12)

Related

Start node server on machine startup

I was trying to start node.js server on startup of the machine (ubuntu 16.04) with upstart by using the the following code in nodeserv.conf file:
#!upstart
description "Node.js server"
author "Sushant Kumar"
start on started mountall
stop on shutdown
respawn
respawn limit 99 5
script
export APP_HOME = "/home/ubuntu/chatbot_server"
export HOME = "/home/ubuntu"
cd $APP_HOME
exec sudo -u ubuntu /usr/local/nodejs/bin/node $HOME/chatbot_server/server.js >> /var/log/chatbotserv.log 2>&1
end script
post-start script
echo "Node Started"
end script
, but I run the command
# start nodeserv
I get the followig error: >>start: Job failed to start.
Can anyone help me please where I am going wrong?
Edit: This server is hosted on AWS EC2 instance (if that helps, I don't think it's relevant, but just in case).
Have a look on PM2.
PM2 is a really powerful Node.js process manager.
After install your app, you can easily set it on startup with:
sudo systemctl start pm2-yourusername
You can do this by running your app as a service. You can use forever to ensure that a given script runs continuously. First of all you need to install forever. Then go to your project directory and install forever-monitor. Now you can start your app.
npm install forever -g
cd /path/to/your/project
npm install forever-monitor
forever start app.js
Now you need to use forever-service to build your node script as a service.Firstly, install forever-service and then install your app as a service.
npm install -g forever-service
forever-service install test
If you want to work on your script, you can replace this code in your script.
export HOME="/root"
exec /usr/local/nodejs/bin/node /home/ubuntu/chatbot_server/server.js >> /var/log/node.log 2>&1

How do you use pm2 startup with a non-root user?

According to the documentation here: http://pm2.keymetrics.io/docs/usage/startup/#startup-systems-support
You can use the command pm2 startup ubuntu -u nodeapps to resurrect all saved pm2 jobs on server startup.
I ran this command as the nodeapps user. Then I was given a sudo su command to run. I logged out of nodeapps, used sudo su to log into the system as root, and ran the command:
sudo su -c "env PATH=$PATH:/usr/bin pm2 startup ubuntu -u nodapps --hp /home/nodeapps"
The processes did not restart on server restart. I found this question on Stack Overflow: Ubuntu 14.04 - pm2 startup not starting after reboot.
In the script /etc/init.d/pm2-init.sh I found the line that question recommended addressing:
export PATH=/usr/bin:$PATH
export PM2_HOME="/home/nodeapps/.pm2"
But it looks correct to me so I didn't change anything.
I then found this question: pm2 Startup not starting up on Ubuntu
and in my boot logs I find the following line:
Starting pm2
/usr/bin/env: node: No such file or directory
I know that 'node' on Ubuntu is actually 'nodejs'. Could this be the reason?
If it is, what can I do to make the startup command look for nodejs instead of node.
Alternatively, could this be a $PATH problem? If it is, how can I add the correct path to root (at least I think it should be added to root)
I don't know if it will help you but I use in this way:
As a non-root user
pm2 startup -u <YOUR_NON_ROOT_USER>
Copy line showed like
env PATH=$PATH:/usr/bin pm2 startup systemd -u delivery --hp /home/delivery
As a root execute
env PATH=$PATH:/usr/bin pm2 startup systemd -u delivery --hp /home/delivery
Back to non root user and type:
pm2 start <YOUR /PATH/TO/INDEX.JS> --name <YOU_APPLICATION_NAME>
As a non-root type:
pm2 save
reboot
sudo reboot
As a non-root user type the commando bellow to check if it works
pm2 status
PS: Change as needed.
I hope it will be useful for you or someone.
(Posted on behalf of the OP).
In fact that was the problem. Fixed via creating a symlink (as root):
ln -s /usr/bin/nodejs /usr/sbin/node

PM2 Logrotate - install error

I am trying to install pm2 logrotate on ubuntu server. I run the command
pm2 install pm2-logrotate
I get the following error
[PM2][Module][ERROR] Unknown module
Any ideas on what could be wrong?

Nodejs forever, run on boot with --watch not working

I am new to linux and have just set up a server over at linode using ubuntu 12.04.
I have created a "myconfig.conf" file in /etc/init/ containing the following
start on startup
stop on shutdown
respawn
exec sudo -u myUser /usr/local/bin/forever start -w /home/myUser/myProject/server.js
When I reboot the linux server node has not been started (in some cases I can access the node server for 1 second before it dies). If I run this without the watch option ("-w") it works fine. I can also use the watch option when running manually without a problem, only happens when using the conf file. Is there some path I have to specify so that forever knows what files to watch?
Thanks in advance!
I use forever-service and nodemon and then use the chkconfig utility to have it start on reboot.
For example:
This forever-service command does the following: everytime a json or raml file in the applications dist/assets folder is modified, wait 10 seconds and then restart the node app (server.js script):
$ forever-service install raml --script server.js -f " -c nodemon" -o " --delay 10 --watch dist/assets -e json,raml --exitcrash" -e "PATH=/usr/local/bin:$PATH"
Then I can set the service to start on server reboot:
$ chkconfig --add raml
$ chkconfig raml on

How to make a node.js application run permanently?

On a Debian server, I installed Node.js. I understand how to launch an app from putty with this command line:
node /srv/www/MyUserAccount/server/server.js
and get to it on the address 50.51.52.53:8080 (IP and port).
But as soon as I close putty, then I cannot reach the address 50.51.52.53:8080 anymore.
How to make a Node.js application run permanently?
As you can guess, I am a beginner with Linux and Node.js.
You could install forever using npm like this:
sudo npm install -g forever
And then start your application with:
forever server.js
Or as a service:
forever start server.js
Forever restarts your app when it crashes or stops for some reason. To restrict restarts to 5 you could use:
forever -m5 server.js
To list all running processes:
forever list
Note the integer in the brackets and use it as following to stop a process:
forever stop 0
Restarting a running process goes:
forever restart 0
If you're working on your application file, you can use the -w parameter to restart automatically whenever your server.js file changes:
forever -w server.js
Although the other answers solve the OP's problem, they are all overkill and do not explain why he or she is experiencing this issue.
The key is this line, "I close putty, then I cannot reach the address"
When you are logged into your remote host on Putty you have started an SSH linux process and all commands typed from that SSH session will be executed as children of said process.
Your problem is that when you close Putty you are exiting the SSH session which kills that process and any active child processes. When you close putty you inadvertently kill your server because you ran it in the foreground. To avoid this behavior run the server in the background by appending & to your command:
node /srv/www/MyUserAccount/server/server.js &
The problem here is a lack of linux knowledge and not a question about node. For some more info check out: http://linuxconfig.org/understanding-foreground-and-background-linux-processes
UPDATE:
As others have mentioned, the node server may still die when exiting the terminal. A common gotcha I have come across is that even though the node process is running in bg, it's stdout and stderr is still pointed at the terminal. This means that if the node server writes to console.log or console.error it will receive a broken pipe error and crash. This can be avoided by piping the output of your process:
node /srv/www/MyUserAccount/server/server.js > stdout.txt 2> stderr.txt &
If the problem persists then you should look into things like tmux or nohup, which are still more robust than node specific solutions, because they can be used to run all types of processes (databases, logging services, other languages).
A common mistake that could cause the server to exit is that after running the nohup node your_path/server.js & you simply close the Putty terminal by a simple click. You should use exit command instead, then your node server will be up and running.
You can use PM2, it's a production process manager for Node.js applications with a built-in load balancer.
Install PM2
$ npm install pm2 -g
Start an application
$ pm2 start app.js
If you using express then you can start your app like
pm2 start ./bin/www --name="app"
Listing all running processes:
$ pm2 list
It will list all process. You can then stop / restart your service by using ID or Name of the app with following command.
$ pm2 stop all
$ pm2 stop 0
$ pm2 restart all
To display logs
$ pm2 logs ['all'|app_name|app_id]
I'd recommend looking for something such as Forever to restart Node in the event of a crash, and handle daemonizing this for you.
If you just want to run your node app in the terminal always, just use screen.
Install on ubuntu/ debian:
sudo apt-get install screen
Usage:
$ screen
$ node /path/to/app.js
ctrl + a and then ctrl + d to dismiss
To get is back:
One screen: screen -r
If there's more than one you can list all the screens with: screen -ls
And then: screen -r pid_number
You could simply use this
nohup node /srv/www/MyUserAccount/server/server.js &
This will keep the application running and to shut it down you will have to kill it.
For that you could install htop and then search for node and then kill it
Forever is a very good NodeJs module to do exactly that.
Install forever by typing in the command line
$ npm install forever -g
Then use the following command to run a node.js script
$ forever start /path/to/script.js
You are good to go. Additionally you can run
$ forever list
to see all the running scripts. You can terminate any specific script by typing
$ forever stop [pid]
where [pid] is the process ID of the script you will obtain from the list command. To stop all scripts, you may type
$ forever stopall
Installation
$ [sudo] npm install forever -g
You can use forever to run scripts continuously
forever start server.js
forever list
for stop service
forever stop server.js
During development, I recommend using nodemon. It will restart your server whenever a file changes. As others have pointed out, Forever is an option but in production, it all depends on the platform you are using.
You will typically want to use the operating system's recommended way of keeping services up (e.g. http://www.freedesktop.org/wiki/Software/systemd/).
nohup working i checked in AWS Ubunto vm follow the correct syntax
ubuntu#ip-172-00-00-00:~/ms$ nohup node server.js &
then press enter you will see this line
ubuntu#ip-172-00-00-00:~/ms$ nohup: ignoring input and appending output to ‘nohup.out’
then type this
rm nohup.out
Here's an upstart solution I've been using for my personal projects:
Place it in /etc/init/node_app_daemon.conf:
description "Node.js Daemon"
author "Adam Eberlin"
stop on shutdown
respawn
respawn limit 3 15
script
export APP_HOME="/srv/www/MyUserAccount/server"
cd $APP_HOME
exec sudo -u user /usr/bin/node server.js
end script
This will also handle respawning your application in the event that it crashes. It will give up attempts to respawn your application if it crashes 3 or more times in less than 15 seconds.
First install pm2 globally
npm install -g pm2
then start
pm2 start bin/www
I’ve found forever to do the job perfectly fine.
Assuming you already have npm installed, if not, just do
sudo apt-get install npm
Then install forever
npm install forever --global
Now you can run it like this
forever start app.js
https://codingweb.io/run-nodejs-application-background/
No need to install any other package.
Run this command
node server.js > stdout.txt 2> stderr.txt &
server.js is your server file or it can be api.js
After that hit "exit" to close terminal
exit
Another way is creating a system unit for your app. create a "XXX.service" file in "/etc/systemd/system" folder, similar to this:
[Unit]
Description=swagger
After=network.target
[Service]
ExecStart=/usr/bin/http-server /home/swagger/swagger-editor &
WorkingDirectory=/home/swagger
Restart=always
RestartSec=30
[Install]
WantedBy=multi-user.target
A benefit is the app will run as a service, it automatically restarts if it crashed.
You can also use sytemctl to manage it:
systemctl start XXX to start the service, systemctl stop XXX to stop it and systemctl enable XXX to automatically start the app when system boots.
Try pm2 to make your application run forever.
npm install -g pm2
and then use
pm2 start server.js
to list and stop apps, use commnds
pm2 list
pm2 stop 0
I hope this will help you.
At the command line, install forever:
npm install forever -g
Create an example file:
sudo nano server.js
You can edit the file and get results directly in your browser.
You can use filezilla or any editor to edit the file.
Run this command to run the file:
forever start --minUptime 1 --spinSleepTime 1000 -w server.js
forever package worked for me, just one thing, it depends on deep-equal, so if you had issue with installing it like:
npm -g install forever
Try:
npm -g install forever deep-equal#1.1.1
instead.
As we know that there are many options to do this. Here is a pure Shell solution, with no need for extra programs / packages.
This solution will restart server.js if it crashes for some reason / errors.
Let's say this is a run.sh:
#!/usr/bin/env sh
while :; do
node server.js
echo "Restarting..."
sleep 1
done
Make sure to make the run.sh file executable:
chmod +x run.sh
And to run it:
./run.sh
If you want to run it in the background:
./run.sh &
Run in the background super-silently (detached, without any output):
( ./run.sh > /dev/null 2>&1 & )
I recommend use PM2, which is a process manager for Node.js applications. PM2 provides an easy way to manage and daemonize applications (run them as a service).
refer this link - https://www.digitalocean.com/community/tutorials/how-to-set-up-a-node-js-application-for-production-on-centos-7

Resources