Is it 2am or 2pm? No it's pm2.

dos2unix

Well-Known Member
Joined
May 3, 2019
Messages
3,517
Reaction score
3,266
Credits
31,472
PM2 is a production process manager for Node.js applications with a built-in load balancer. It helps keep applications alive forever, reload them without downtime, and facilitate common system admin tasks. This is similar to running a command as a process daemon. A lot like running...

Code:
nohup mycommand.sh &

Think of it as a process manager like sysVinit or systemd. Except you don't have to know how to write rc.init files or systemd.service files.

It requires something called npm in order to install it. For those who aren't familiar with npm, it's a module installer for nodejs. I feel like I need to get into a series of explaining.. "what is..". Nodejs is a jascipt server. Typically used as a javascript backend for other web servers (nginx, httpd, apache, etc..) but can be used a standalone web server as well. However we don't need it to be a javascript or webserver for the sake of this article.

Depending on your distro, npm sometimes comes as part of nodejs. For other distros you can install it separately. Think of npm like pip for python. At a higher level, it's a little like apt, dnf or pacman except it's only for nodejs modules.

OK, so now that I have npm installed, how do I install pm2?

Code:
npm install pm2 -g

The "-g" means make it globally available for all users. This is how you can check the status of pm2.
Code:
pm2 status

You wouldn't want to use this for a "run once" script. This is for something you want to be running all the time in the background.
OK, so how do we add a job to pm2?

Code:
pm2 start app.js

Change "app.js" to the name of your bash shell script, or javascript, or python file. Pretty much any language will work.
It also works with process watching.

Code:
pm2 start app.js --name myApp --watch

How can I check the status on my job?
Code:
pm2 show myApp

How can I remove a job in pm2 if I don't need it anymore?
Code:
pm2 delete myApp

Here are a few examples of different ways to install jobs.
Code:
pm2 start app.js

With python.
Code:
pm2 start script.py --interpreter python

With bash.
Code:
pm2 start script.sh --interpreter bash

With an environmental variable.
Code:
pm2 start app.js --name myApp --env production

For commands that have option flags.
Code:
pm2 start npm --name "awx-run" -- run awx

Hopefully this is enough to get you started with a new way to run applications in the background.
Happy Linux'ing! :cool:
 


Well OK, that was fun. But I don't want to use pm2 anymore. How do I delete it?

Stop all jobs.
Code:
pm2 stop all

Kill the pm2 daemon.
Code:
pm2 kill

Remove the daemon.
Code:
npm remove pm2 -g

If you want to be extra thorough and delete your command files.
Code:
rm -rf ~/.pm2

That's all I have for now. Feel free to add comments and suggestions.
 
Here is a silly example of a background script you might run using pm2.
Code:
#!/bin/bash

# Log file location
LOGFILE="/var/log/system_monitor.log"

# Function to log system usage and check thresholds
log_usage() {
    CPU_USAGE=$(top -bn1 | grep "Cpu(s)" | \
    sed "s/.*, *\([0-9.]*\)%* id.*/\1/" | \
    awk '{print 100 - $1}')
    MEM_USAGE=$(free -m | awk 'NR==2{printf "%.2f", $3*100/$2 }')

    echo "$(date): CPU: ${CPU_USAGE}% MEM: ${MEM_USAGE}%" >> $LOGFILE

# Take drastic action here!
    if (( $(echo "$CPU_USAGE > 85" | bc -l) )) || (( $(echo "$MEM_USAGE > 90" | bc -l) )); then
        cowsay "Help! I'm being overloaded!"
    fi
}

# Infinite loop to keep the script running
while true
do
    log_usage
    sleep 60 # Log every 60 seconds
done
 


Staff online

Members online


Latest posts

Top