0
0
Expressframework~10 mins

PM2 for process management in Express - 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 crash or stop detected
PM2 restarts app automatically
User can manage app via PM2 commands
Stop, restart, list, logs, scale processes
PM2 runs your Node.js app, watches it, and restarts it if it crashes, letting you control it easily.
Execution Sample
Express
pm2 start app.js
pm2 list
pm2 restart app
pm2 stop app
pm2 logs
These commands start, list, restart, stop, and show logs of your app managed by PM2.
Execution Table
StepPM2 CommandAction TakenProcess StatusOutput/Result
1pm2 start app.jsLaunch app.js as a managed processonlineProcess started with id 0
2pm2 listShow all managed processesonlineList shows app.js with status online
3app.js crashesPM2 detects crashstoppedProcess exited unexpectedly
4PM2 auto-restarts app.jsRestart process automaticallyonlineProcess restarted with id 0
5pm2 restart appUser restarts app manuallyonlineProcess restarted successfully
6pm2 stop appUser stops app processstoppedProcess stopped
7pm2 logsShow app logsstoppedLogs displayed in terminal
💡 User stops the app with 'pm2 stop app', process status becomes stopped, ending management session.
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4After Step 6
Process Statusnoneonlinestopped (crash)online (auto-restart)stopped (manual stop)
Key Moments - 3 Insights
Why does PM2 restart the app automatically after a crash?
PM2 monitors the process status and when it detects a crash (see step 3 in execution_table), it restarts the app automatically (step 4) to keep it running without manual intervention.
What is the difference between 'pm2 stop' and a crash?
'pm2 stop' is a manual command by the user to stop the app (step 6), while a crash is an unexpected failure detected by PM2 (step 3). The app status changes to 'stopped' in both, but only crashes trigger automatic restarts.
How can you see the current status of your app processes?
Using 'pm2 list' (step 2) shows all managed processes and their statuses, helping you know if apps are online or stopped.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the process status immediately after the app crashes?
Aonline
Bstarting
Cstopped
Drestarting
💡 Hint
Check step 3 in the execution_table where the crash is detected.
At which step does PM2 automatically restart the app after a crash?
AStep 3
BStep 4
CStep 2
DStep 6
💡 Hint
Look for the step where the action says 'Restart process automatically'.
If you run 'pm2 stop app', what will be the process status shown in variable_tracker after that?
Astopped
Bonline
Ccrashed
Dstarting
💡 Hint
Refer to the 'After Step 6' column in variable_tracker for Process Status.
Concept Snapshot
PM2 manages Node.js apps by running them as processes.
It watches for crashes and restarts apps automatically.
Use commands like 'pm2 start', 'pm2 stop', 'pm2 restart', and 'pm2 logs'.
Check status anytime with 'pm2 list'.
This keeps your app running smoothly without manual restarts.
Full Transcript
PM2 is a tool that helps you run and manage your Node.js apps easily. When you start your app with PM2, it runs the app as a process and watches it closely. If the app crashes, PM2 notices and restarts it automatically so your app stays online. You can control your app with simple commands like 'pm2 start' to launch it, 'pm2 stop' to stop it, 'pm2 restart' to restart it, and 'pm2 logs' to see what your app is doing. You can also see all running apps and their status with 'pm2 list'. This way, PM2 helps keep your app running smoothly without you needing to watch it all the time.