Process Managers like PM2

Process Managers like PM2

Process Managers are tools that are designed to manage Node.js processes, which includes starting, stopping, monitoring, and restarting them as needed. They can automatically restart an application when changes are made to the source code, and this is called a “file watcher” or “file system watcher”. Process managers are important for two main reasons:

  1. In development environment, using them as a file watcher is a practice to speed up process and reduce the need for manual restarting of the application.
  2. In production environments they are very important as applications need to be running continuously.

PM2 is a process manager for Node.js that includes a file system watcher. In addition to monitoring changes to your code, it also provides features such as process clustering, load balancing, and zero-downtime deployments.

Nodemon is a tool that monitors for any changes in your source code and automatically restarts your Node.js application.

Forever is a simple tool that runs your Node.js script as a daemon and automatically restarts it if it crashes or if the code changes. It also supports clustering and logging.

Node Supervisor is a deprecated tool that provides a similar functionality as a file watcher that used to be popular

Supervisord is written in Python, configuration file is in a proprietary format and not JSON, does not include a built-in web interface and PM2 is much more active.


Assume we have working node.js.

It is possible to use SystemD as it is the default process manager on most Linux environments, but we have better ideas here as PM2 can be used also with ReactPHP for example.

How To Set Up a Node.js Application for Production on Debian 10 | DigitalOcean

Install PM2 globally:

npm install pm2 -g
# ensure pm2 starts on boot
pm2 startup
# service pm2-root status

You can also start pm2 at startup as other user with:

sudo env PATH=$PATH:/usr/local/bin pm2 startup -u safeuser

Let’s create our node.js application with the following lines of code:

cd ~/service

cat <<'EOF' > hello.js

const http = require('http');

const hostname = 'localhost';
const port = 4444;

http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World!\n');
}).listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

EOF

Test with:

pm2 start hello.js

This simple command will start your app and will ensure automatic restart if it crashes, and automatic start on server reboots.

For more command examples, this is perfect: https://pm2.keymetrics.io/docs/usage/pm2-doc-single-page/

pm2 start --name app_name app.js
pm2 start app_name --watch

pm2 list

pm2 stop app_name
pm2 reload app_name
pm2 restart app_name

pm2 logs app_name
pm2 monit
pm2 info app_name

Node in ISPConfig

How to Deploy Node JS to Ubuntu 20.04

nginx config:

location /service/hello {
  proxy_pass http://localhost:444;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection 'upgrade';
  proxy_set_header Host $host;
  proxy_cache_bypass $http_upgrade;
}

You can add additional location blocks to the same server block to access other applications on the same server.


shell - Linux : To monitor a Service and restart if stopped? - Super User

Eeasiest to use is some process manager. It’s also very well documented, works almost anywhere and is packaged in all the major distributions.

I can use ps-watcher or supervisor, monit but systemd should have that option included by default.

ps-watcher: Running HHVM with fallback to PHP-FPM

ps-watcher is a program that runs the ps command periodically and triggers commands on matches.


With monit:

Monit configurations for commonly used services

Codeable - Speed up WP admin with Redis and HHVM Restart HHVM with Monit

Poll: What do you use for Unix process management/monitoring? | Hacker News


Node restart on change

Node Supervisor

Node Supervisor is used to restart programs when they crash. It can also be used to restart programs when a *.js file changes.

https://github.com/isaacs/node-supervisor :: isaacs/node-supervisor

Alternatives to Node Supervisor are Run and Nodemon. Run is not as feature-rich as Supervisor. Nodemon is also feature rich, but I have found it to be a CPU hog, also it doesn’t restart after some runtime exceptions.

Nodemon

Simple node.js server restart. You can use nodemon with forever (restarting a node if server chrashes).

https://github.com/remy/nodemon :: remy/nodemon http://remysharp.com/2010/10/12/nodejs-rapid-development-nodemon/ :: node.js rapid development: nodemon


PHP process manager?

seregazhuk/php-watcher will automatically restart PHP application once the source code changes. Same as pm2/forever/nodemon but written in a pure PHP and available via Composer


Running command on any changes inside the folder

Watchman is a file watcher that monitors files, records changes, and triggers actions accordingly. It can be installed using scoop install watchman.

However, it seems that Watchman is no longer actively maintained or updated and therefore, it is recommended to use Watchexec instead. Watchexec, available on the watchexec/watchexec repository, is an excellent alternative for monitoring file modifications.

Use Watchexec on Linux

The installation process on Ubuntu is not straightforward and should not be done using the “cargo” command:

1
2
3
# don't do it like this
#   apt install cargo
#   cargo binstall watchexec-cli 

Although the method using the Rust apt repository is not elegant, it is still the best option for installation:

1
2
3
4
5
curl -fsSL https://apt.cli.rs/pubkey.asc | sudo tee -a /usr/share/keyrings/rust-tools.asc
curl -fsSL https://apt.cli.rs/rust-tools.list | sudo tee /etc/apt/sources.list.d/rust-tools.list
sudo apt update

apt install watchexec-cli lsd

Then, execute Watchexec and, for example, Rclone using the following commands:

1
watchexec -- 'p="_customize" ; rclone sync "cnc24-dev:/wp-content/plugins/$p" "cnc24-stage:/wp-content/plugins/$p" --progress --exclude="node_modules/**" --exclude=".git/**"'
date 31. Oct 2020 | modified 13. May 2024
filename: Linux » Process Managers