How NodeJS server running for ever - node.js

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

Related

Is there a way to start a node application with pm2 running as root?

I have a node express app used as GUI for an embedded raspberry pi controller that manipulates a device via the gpio interface. The app runs fine with the command "sudo node app.js", but fails if I try to run it with the command "node app.js" with the error...
2022-08-28 21:48:03 initCheckPermitted:
+---------------------------------------------------------+
|Sorry, you don't have permission to run this program. |
|Try running as root, e.g. precede the command with sudo. |
+---------------------------------------------------------+
/home/ko2f/resonate/node_modules/pigpio/pigpio.js:17
pigpio.gpioInitialise();
I would like to use pm2 to automatically run my app each time the controller restarts.
Is there a way to start a node application with pm2 running as root?
resonate is the actual name of my app.
from my node application folder, I entered the command:
sudo pm2 start resonate.js
and got a [pm2] Process successfully started response. I entered the command:
sudo pm2 status
and pm2 returned a status of 'errored'. What did I miss?
Like your messages says.
Try using sudo before your command
sudo pm2 start server.js

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

Can PM2 run command line start scripts?

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

pm2 doesn't startup in ubuntu

I am trying to run pm2 at startup without success..
I am installing in ubuntu 16.04.
My npm directory is in : ~/.npm-global.
There, I can see pm2 -> ../lib/node_modules/pm2/bin/pm2
There is no dump file.
My pm2 directory is ~/.pm2:
There , I can see dump.pm2 and rpc.sock
If I do:
sudo $(which pm2) start app.js
sudo $(which pm2) startup ubuntu -u $USER
sudo $(which pm2) save
it shows me :
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"
[PM2] Done.
[PM2] Saving current process list...
[PM2] Successfully saved in /home/theUser/.pm2/dump.pm2
and I can see that the app is running fine.
If I do though, sudo ~/.npm-global/bin/pm2 start /etc/init.d/pm2-init.sh,it gives me:
events.js:160
throw er; // Unhandled 'error' event
^
Error: connect EACCES ~/.pm2/rpc.sock
at ....
The /etc/init.d/pm2-init.sh has :
NAME=pm2
PM2=/home/theUser/.pm2
USER=theUser
DEFAULT=/etc/default/$NAME
export PATH=/usr/bin:$PATH
export PM2_HOME="/home/theUser/.npm-global/lib/node_modules/pm2/bin/"
I tried also using : export PM2_HOME="/home/theUser/.pm2
and when I start the scipt from /etc/init.d/pm2-init.sh , it says that it cannot dump ( probably because dump exists only in ~/.pm2 directory )
If I try:
sudo $(which pm2) start app.js
sudo $(which pm2) startup systemd -u $USER
sudo $(which pm2) save
and execute manually ( because it can't find pm2 as normal user ) :
sudo ~/.npm-global/pm2 dump && ~/.npm-global/pm2 kill && systemctl daemon-reload && systemctl enable ~/.npm-global/pm2 && systemctl start ~/.npm-global/pm2
So,
1) I am not sure if I have to run sudo $(which pm2) startup systemd -u $USER or sudo $(which pm2) startup ubuntu -u $USER
2) What PM2 and PM2_HOME should I have?
3) How can I add ~/.npm-global/bin to sudo path ,or generally how to be able to use this path when I run pm2 as sudo.Note, that I don't want to alter /etc/sudoers.
I can't answer all your questions, but I had the same problem and this worked for me:
http://www.anicehumble.com/2016/05/pm2-must-not-be-ran-with-sudo.html

node startup script NOT working

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

Resources