Skip to content

A Node.js script that monitors the status of PM2-managed applications, logs stopped or non-existent processes, and updates a log file to trigger SMS notifications based on configurable time-of-day rules.

License

Notifications You must be signed in to change notification settings

emadasefi/pm2-checker

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 

Repository files navigation

PM2 Checker (PM2 Status Monitor and SMS Notification Updater)

This Node.js script provides automated monitoring of applications managed by PM2, a production process manager for Node.js. It checks the status of each application, identifies those that are in a 'stopped' or 'nonExistent' state, and records these events in a log file (checking.log).

The script is designed to facilitate SMS notifications when applications encounter issues. It updates the checking.log file, adding a flag to indicate that an SMS notification should be sent. A key feature is the ability to control SMS notifications based on the time of day, allowing you to suppress notifications during off-hours (e.g., at night) if desired. This is managed through a configurable isNightModeActive setting.

The script is designed to run periodically (e.g., using cron or a similar task scheduler) to provide continuous monitoring and timely alerts.

🌟 Features

  • PM2 Process Monitoring: Retrieves the status of all applications managed by PM2.
  • Status Filtering: Identifies applications that are in a 'stopped' or 'nonExistent' state, indicating potential issues.
  • Log File Recording: Records the status of problematic applications in a checking.log file.
  • Unique Log Entry Generation: Creates a unique ID for each log entry.
  • Duplicate Entry Prevention: Prevents duplicate log entries for the same application status.
  • SMS Notification Trigger: Updates the checking.log file to indicate when an SMS notification should be sent for a specific application issue.
  • Time-Based SMS Control: Allows configuration to suppress SMS notifications during specified hours (e.g., for "night mode").
  • Configurable Night Mode: isNightModeActive variable allows to enable or disable night mode.
  • Modular Design: The code is structured into functions (getPM2ProcessOutput, updateCheckingFile, notifyWithFunction) for readability and maintainability.
  • Error Handling: Includes basic error handling for file system operations and command execution.
  • Extensible Notification System: The notifyWithFunction is designed to be easily extended to integrate with your preferred SMS or notification service.


  • 💡 Guide on How to Use This Script

    Node.js Packages Used

  • child_process: Included with Node.js, so no installation is necessary. Used for executing shell commands (e.g., pm2 ls).
  • moment: For date and time formatting.
  • fs: Included with Node.js, so no installation is necessary. Used for file system operations (reading and writing to the log file).
  • path: Included with Node.js, so no installation is necessary. Used for constructing file paths.
  • How to Install Packages

  • 1. Install Node.js and npm: Make sure you have Node.js and npm (Node Package Manager) installed on your system. You can download them from the official Node.js website (https://nodejs.org/).
  • 2. Navigate to the Script Directory: Open a terminal or command prompt and navigate to the directory where you have saved the script.
  • 3. Install the Required Package: Run the following command to install the moment package:
  • npm install fs path moment

    How to Run the Script Every Minute via Crontab

  • 1. Open Crontab: Open the crontab editor by running the following command in your terminal:
  • crontab -e
    This will open the crontab file in a text editor. If this is the first time you're using crontab, it might ask you to choose an editor.
  • 2. Add a Cron Entry: Add the following line to the crontab file to run the script every minute:
  • * * * * * /usr/bin/node /path/to/your/script.js >> /path/to/your/script.log 2>&1
  • Replace /usr/bin/node with the actual path to your Node.js executable. You can find this by running which node in your terminal.
  • Replace /path/to/your/script.js with the full path to your script file.
  • Replace /path/to/your/script.log with the desired path to a log file. This will capture the output of the script. The 2>&1 redirects standard error to the same log file.
  • 3. Save and Close: Save the crontab file and close the editor. The cron daemon will automatically pick up the changes.


  • ▚ A Node.js script that monitors the status of PM2-managed applications, logs stopped or non-existent processes, and updates a log file to trigger SMS notifications based on configurable time-of-day rules.

    — Feedback ❤️—

    Thank you for choosing the PM2 Checker! I hope it meets your needs effectively.
    Please leave a comment if you have any comments, suggestions or problems.

    About

    A Node.js script that monitors the status of PM2-managed applications, logs stopped or non-existent processes, and updates a log file to trigger SMS notifications based on configurable time-of-day rules.

    Topics

    Resources

    License

    Stars

    Watchers

    Forks

    Releases

    No releases published