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
Related
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
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
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 have Node.js app on AWS EC2 instance. This app consists out of 4 modules. To start my app from opened SSH terminal I use PM2and I run the following commands one by one:
pm2 start /opt/backend/app1/app1.js
pm2 start /opt/backend/app2/app2.js
pm2 start /opt/backend/app3/app3.js
pm2 start /opt/backend/app4/app4.js
It works perfect when I run the commands above from opened SSH terminal. However, when I make an image from the EC2 instance and I configure launch configuration to launch new instances from that image and I add the following bash script that should be run when new instance is created, new instances are being made and auto scaling works but Node.js app does not start:
#!/bin/bash
pm2 start /opt/backend/app1/app1.js
pm2 start /opt/backend/app2/app2.js
pm2 start /opt/backend/app3/app3.js
pm2 start /opt/backend/app4/app4.js
I tried the following as well, but it still does not work:
#!/bin/bash
pm2 start /home/username/opt/backend/app1/app1.js
pm2 start /home/username/opt/backend/app2/app2.js
pm2 start /home/username/opt/backend/app3/app3.js
pm2 start /home/username/opt/backend/app4/app4.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