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
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 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 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.
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