0
0
Node.jsframework~10 mins

PM2 for process management in Node.js - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - PM2 for process management
Start Node.js app with PM2
PM2 launches app process
PM2 monitors process health
If process crashes
PM2 restarts process automatically
User can check status with PM2 commands
User can stop/restart/delete process
Exit
PM2 starts and monitors Node.js apps, restarts them if they crash, and lets users control processes with commands.
Execution Sample
Node.js
pm2 start app.js
pm2 status
pm2 restart app
pm2 stop app
pm2 delete app
These commands start, check, restart, stop, and delete a Node.js app process managed by PM2.
Execution Table
StepCommandPM2 ActionProcess StateOutput/Result
1pm2 start app.jsLaunch app.js as a managed processRunningProcess started with id 0
2pm2 statusShow all managed processesRunningList shows app.js with status 'online'
3app.js crashesPM2 detects crashStopped brieflyPM2 restarts app.js automatically
4pm2 restart appRestart app processRunningProcess restarted successfully
5pm2 stop appStop app processStoppedProcess stopped
6pm2 delete appRemove app from PM2 listNot runningProcess deleted from PM2
7pm2 statusShow all managed processesNo app processEmpty list or no app.js found
💡 Process deleted, no longer managed by PM2, so status shows no app running
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 5After Step 6
Process StateNot runningRunningRestarting (after crash)RunningStoppedDeleted
Key Moments - 3 Insights
Why does PM2 restart the app automatically after a crash?
PM2 monitors the process health and restarts it immediately when it detects a crash, as shown in step 3 of the execution_table.
What happens if you delete a process from PM2?
Deleting removes the process from PM2's management list, so it no longer monitors or controls it, as seen in step 6 and 7.
How can you check if your app is running under PM2?
Use 'pm2 status' command to see all managed processes and their states, demonstrated in steps 2 and 7.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the process state immediately after the app crashes?
ADeleted
BStopped briefly
CRunning
DNot running
💡 Hint
Check step 3 in the execution_table under 'Process State'
At which step does PM2 remove the app from its management list?
AStep 2
BStep 4
CStep 6
DStep 5
💡 Hint
Look at the 'Command' and 'PM2 Action' columns in the execution_table
If you run 'pm2 stop app' instead of 'pm2 delete app', what will be the process state?
AStopped
BRunning
CDeleted
DRestarting
💡 Hint
Refer to step 5 in the execution_table for 'pm2 stop app' command
Concept Snapshot
PM2 manages Node.js apps by running them as processes.
It monitors and restarts apps if they crash.
Use 'pm2 start <app>' to launch.
Check status with 'pm2 status'.
Stop with 'pm2 stop <app>' and remove with 'pm2 delete <app>'.
Full Transcript
PM2 is a tool to manage Node.js applications by running them as background processes. When you start an app with PM2, it launches the app and keeps it running. If the app crashes, PM2 automatically restarts it to keep it alive. You can check the status of all apps managed by PM2 using the 'pm2 status' command. To stop an app, use 'pm2 stop', and to remove it completely from PM2's control, use 'pm2 delete'. This way, PM2 helps keep your apps running smoothly without manual restarts.