Managing Python Scripts and Scheduled Tasks with PM2

PM2 provides an efficient solution for managing long-running processes and scheduled tasks, extending beyond its typical Node.js use case. It simplifies the orchestration of Python scripts by offering automatic restarts, log management, and cron-based scheduling. This approach eliminates the need to write custom logging modules for small scripts and provides a straightforward CLI for process control.

While PM2 excels at managing a moderate number of resident services, it becomes less efficient when managing a massive fleet of dynamic scripts. Users should also be aware of potential character encoding issues when viewing logs directly in the console on Windows systems.

Environment Setup

The deployment process begins with installing Node.js, followed by the global installation of PM2. On Windows, the pm2-windows-startup package is recommended to configure the startup behavior and enable the web-based monitoring interface.

npm install pm2 -g
npm install pm2-windows-startup -g
pm2-startup install

Verify the installation by checking the version. If the command fails, ensure the Node.js installation path has been added to the system environment variables.

Process Configuration and Deployment

Using an ecosystem.config.js file allows for declarative process management. This file defines the script paths, interpreter locations (useful for virtual environments), and execution policies such as memory limits and restart strategies.

For scheduled tasks, the autorestart property must be disabled to prevent the script from running indefinitely or restarting immediately after completion. Instead, the cron_restart parameter dictates the execution schedule. For asynchronous scripts that require initialization time, adjusting the min_uptime value ensures the process is not mistakenly flagged as unstable during startup.

module.exports = {
  apps: [
    {
      name: 'order-processor',
      script: 'workers/order_handler.py',
      interpreter: '.venv/Scripts/python.exe',
      instances: 1,
      autorestart: true,
      watch: false,
      max_memory_restart: '1G',
    },
    {
      name: 'financial-report-job',
      script: 'cron/financials.py',
      interpreter: '.venv/Scripts/python.exe',
      instances: 1,
      // Prevents immediate restart; rely on cron schedule
      autorestart: false,
      // Executes every day at 16:10
      cron_restart: '10 16 * * *',
      watch: false,
      max_memory_restart: '1G',
    }
  ]
};

To deploy, navigate to the project directory and start the services using the configuration file.

cd C:\projects\automation_suite
pm2 start ecosystem.config.js
pm2 list
pm2 logs [id]

Tags: PM2 Process Management python Task Scheduling devops

Posted on Fri, 06 Nov 2026 16:04:42 +0000 by aim25