Bird
Raised Fist0
Node.jsframework~10 mins

PM2 for process management in Node.js - 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 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.

Practice

(1/5)
1. What is the primary purpose of PM2 in Node.js application management?
easy
A. To keep Node.js applications running and restart them if they crash
B. To compile Node.js code into machine code
C. To serve static files like images and CSS
D. To replace the Node.js runtime environment

Solution

  1. Step 1: Understand PM2's role

    PM2 is a process manager designed to keep Node.js apps alive and restart them automatically if they crash.
  2. Step 2: Compare options

    Options A, B, and C describe unrelated tasks like replacing the runtime, compiling code, or serving static files, which PM2 does not do.
  3. Final Answer:

    To keep Node.js applications running and restart them if they crash -> Option A
  4. Quick Check:

    PM2 = process manager for app uptime [OK]
Hint: PM2 manages app uptime and restarts crashed apps [OK]
Common Mistakes:
  • Confusing PM2 with a compiler or bundler
  • Thinking PM2 serves static content
  • Believing PM2 replaces Node.js runtime
2. Which of the following is the correct command to start an app named app.js using PM2?
easy
A. pm2 run app.js
B. pm2 launch app.js
C. pm2 start app.js
D. pm2 execute app.js

Solution

  1. Step 1: Recall PM2 start command

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

    Options A, B, and D use incorrect verbs like run, launch, or execute which are not valid PM2 commands.
  3. Final Answer:

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

    Start app = pm2 start [OK]
Hint: Use 'pm2 start' to launch apps [OK]
Common Mistakes:
  • Using 'pm2 run' instead of 'pm2 start'
  • Confusing PM2 commands with other tools
  • Typing 'pm2 launch' which is invalid
3. Given the following PM2 command sequence:
pm2 start app.js --name myapp
pm2 restart myapp
pm2 stop myapp
pm2 delete myapp

What is the state of myapp after these commands?
medium
A. The app crashed and PM2 is trying to restart it
B. The app is stopped but still listed in PM2 process list
C. The app is running and managed by PM2
D. The app is removed from PM2 process list

Solution

  1. Step 1: Analyze each command effect

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

    After deletion, the app is no longer managed or listed by PM2.
  3. Final Answer:

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

    Delete removes app from PM2 list [OK]
Hint: Delete removes app from PM2 list, stop just pauses it [OK]
Common Mistakes:
  • Confusing stop with delete
  • Assuming restart keeps app stopped
  • Thinking delete crashes the app
4. You run pm2 start app.js but the app crashes immediately. Which PM2 command helps you see the error logs to debug the issue?
medium
A. pm2 status
B. pm2 logs
C. pm2 restart
D. pm2 list

Solution

  1. Step 1: Identify command to view logs

    pm2 logs shows real-time logs including errors from apps managed by PM2.
  2. Step 2: Compare other commands

    pm2 status and pm2 list show process info but not detailed logs. pm2 restart restarts apps but doesn't show logs.
  3. Final Answer:

    pm2 logs -> Option B
  4. Quick Check:

    Logs show errors for debugging [OK]
Hint: Use 'pm2 logs' to see app errors instantly [OK]
Common Mistakes:
  • Using 'pm2 status' to find errors
  • Restarting app without checking logs
  • Confusing 'pm2 list' with logs
5. You want PM2 to automatically restart your app server.js whenever the file changes during development. Which command achieves this?
hard
A. pm2 start server.js --watch
B. pm2 start server.js --no-restart
C. pm2 start server.js --ignore-watch
D. pm2 start server.js --restart-delay 1000

Solution

  1. Step 1: Understand PM2 watch option

    The --watch flag tells PM2 to watch for file changes and restart the app automatically.
  2. Step 2: Evaluate other options

    --no-restart disables restarts, --ignore-watch disables watching, and --restart-delay adds delay but doesn't enable watching.
  3. Final Answer:

    pm2 start server.js --watch -> Option A
  4. Quick Check:

    Watch flag enables auto-restart on file changes [OK]
Hint: Use '--watch' to auto-restart on file changes [OK]
Common Mistakes:
  • Using --no-restart disables restarts
  • Ignoring watch disables file monitoring
  • Restart delay doesn't watch files
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