2

I have a script which is written in Python and the application is setup in such a way that you need to enable virtual env to run the application. There is a filewith the name of startProcess.py if you run the application with python startProcess.py it will throw the below error.

Traceback (most recent call last):
  File "startProcess.py", line 1, in <module>
    import pika, sys, os
ModuleNotFoundError: No module named 'pika'

But, when I enable the env using .\venv\Scripts\activate command and then run the script, the process will start as expected. All the packages are installed to the venv only.

I need this code to be running in background, and the solution which I'm going with is pm2.

I've installed NodeJS & PM2 as global, tried to run the script using the pm2 start startProcess.py, the application failes to start with the same error as above. I did some research and followed THIS process to create a ecosystem.config.json and aded some configurations to that file. My config files looks something like below.

{
    "apps": [{
        "name": "app_name",
        "cwd": "<---absolute_path_to_project_dir--->",
        "script": "./startProcess.py",
        "exec_mode": "fork_mode",
        "instances": "1",
        "wait_ready": true,
        "autorestart": false,
        "max_restarts": 5,
        "interpreter" : "<---absolute_path_to_project_dir--->\\venv\\Scripts\\python"
    }]
}

When I run the start script pm2 start ecosystem.config.json, the application works as expected. but the problem is, it will open the python.exe inside the venv\Scripts\python.exe, which we've provided as interpreter will pop the empty terminal window as showin in the below screenshot.

enter image description here

When I close the python.exe terminal window, the process will also get stopped. Screenshot below.

enter image description here

The Code is running in windows server, due to some restrictions, we can't use linux servers to deploy the code.

I've also referred the official documentation, and tried both cluster_mode and fork_mode. I did not understand the difference between both modes much, but the configuration which I added above will work with fork_mode properly and will throw the same error as I mentioned earler for cluster_mode.

Only 1 change which I can make is setting autorestart to true and when I close that python.exe terminal, pm2 will restart the process, and open new python.exe window.

Accepted Criteria:

Coniguration which is metioned above is fine, but the interpreter (python.exe) should not be opopped up as new terminal window. It can be running in background, but everything else is fine as configued above. As of now.

rakcode
  • 2,256
  • 4
  • 19
  • 44

0 Answers0