Bird
Raised Fist0
Expressframework~10 mins

PM2 for process management in Express - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using PM2 with an Express application?
easy
A. To write the Express app code faster
B. To style the Express app's frontend
C. To keep the app running continuously without manual restarts
D. To create database schemas automatically

Solution

  1. Step 1: Understand PM2's role

    PM2 is a process manager that keeps Node.js apps running smoothly by restarting them if they crash or stop.
  2. Step 2: Identify its use with Express

    Using PM2 with Express ensures the server stays online without manual restarts after crashes or updates.
  3. Final Answer:

    To keep the app running continuously without manual restarts -> Option C
  4. Quick Check:

    PM2 manages app uptime = B [OK]
Hint: PM2 keeps your app alive automatically [OK]
Common Mistakes:
  • Thinking PM2 helps with frontend styling
  • Confusing PM2 with code writing tools
  • Assuming PM2 manages databases
2. Which of the following is the correct command to start an Express app named app.js using PM2?
easy
A. pm2 run app.js
B. pm2 start app.js
C. pm2 launch app.js
D. pm2 execute app.js

Solution

  1. Step 1: Recall PM2 start command

    The correct PM2 command to launch an app is pm2 start <file>.
  2. Step 2: Match the command with options

    Among the options, only pm2 start app.js matches the correct syntax.
  3. Final Answer:

    pm2 start app.js -> Option B
  4. Quick Check:

    Start app with 'pm2 start' = D [OK]
Hint: Use 'pm2 start' to launch apps [OK]
Common Mistakes:
  • Using 'pm2 run' which is invalid
  • Confusing 'launch' or 'execute' as PM2 commands
  • Omitting the 'start' keyword
3. Given the following PM2 commands run in order:
pm2 start app.js --name myapp
pm2 restart myapp
pm2 stop myapp
pm2 delete myapp

What is the state of the app after these commands?
medium
A. The app is running but without a name
B. The app is stopped but still managed by PM2
C. The app is running with the name 'myapp'
D. The app is removed from PM2 process list and not running

Solution

  1. Step 1: Analyze each command effect

    pm2 start app.js --name myapp starts the app named 'myapp'.
    pm2 restart myapp restarts it.
    pm2 stop myapp stops the app but keeps it in PM2 list.
    pm2 delete myapp removes the app from PM2 list and stops it completely.
  2. Step 2: Determine final state

    After deletion, the app is no longer managed by PM2 and is not running.
  3. Final Answer:

    The app is removed from PM2 process list and not running -> Option D
  4. Quick Check:

    Delete removes app from PM2 = A [OK]
Hint: Delete removes app from PM2 and stops it [OK]
Common Mistakes:
  • Thinking stop deletes the app from PM2
  • Assuming restart keeps app stopped
  • Confusing app running state after delete
4. You tried to start your Express app with PM2 using pm2 start app.js, but it immediately stops. Which of the following is the most likely cause?
medium
A. The app.js file has a syntax error causing it to crash
B. PM2 does not support Express apps
C. You need to use pm2 run instead of pm2 start
D. The app.js file is missing the package.json

Solution

  1. Step 1: Understand PM2 behavior on crashes

    If the app crashes immediately after start, PM2 will stop it and try to restart repeatedly.
  2. Step 2: Identify common crash reasons

    A syntax error in app.js is a common cause for immediate crash and stop.
  3. Final Answer:

    The app.js file has a syntax error causing it to crash -> Option A
  4. Quick Check:

    Crash causes immediate stop = C [OK]
Hint: Check app.js syntax if PM2 stops immediately [OK]
Common Mistakes:
  • Believing PM2 can't run Express apps
  • Using wrong PM2 commands like 'run'
  • Thinking package.json is required to start app.js
5. You want to run multiple instances of your Express app using PM2 to improve performance on a multi-core server. Which command correctly achieves this?
hard
A. pm2 start app.js -i max
B. pm2 start app.js --instances 1
C. pm2 start app.js --scale 4
D. pm2 start app.js --multi 4

Solution

  1. Step 1: Understand PM2 instance options

    PM2 uses -i or --instances to specify how many app instances to run. Using -i max runs as many instances as CPU cores.
  2. Step 2: Evaluate options

    --instances 1 runs only one instance.
    --scale and --multi are not valid PM2 options.
  3. Final Answer:

    pm2 start app.js -i max -> Option A
  4. Quick Check:

    Use '-i max' for multi-core instances = A [OK]
Hint: Use '-i max' to run all CPU cores [OK]
Common Mistakes:
  • Using invalid options like --scale or --multi
  • Setting instances to 1 for multi-core use
  • Confusing PM2 flags for scaling
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard