Monit fails to import python library

62 views Asked by At

I am trying to monitor a python program so it restarts when it crashes. However, monit fails to restart the program because it does not recognize a library which is clearly installed since I am able to run the program from my terminal.

Monit log outputs the following:

[2023-02-07T11:33:11-0800] error    : 'PythonCrashTest' failed to restart (exit status 1) -- '/mnt/c/Users/ARSialer/Desktop/monitTest/main.py start': Traceback (most recent call last):
  File "/mnt/c/Users/ARSialer/Desktop/monitTest/main.py", line 4, in <module>
    import psutil
ModuleNotFoundError: No module named 'psutil'

this is how my monitrc file looks like:

check process PythonCrashTest with pidfile /mnt/c/Users/ARSialer/Desktop/monitTest/test.pid
        start = "/mnt/c/Users/ARSialer/Desktop/monitTest/main.py" with timeout 3 seconds
        restart =  "/mnt/c/Users/ARSialer/Desktop/monitTest/main.py"  with timeout 3 seconds

And my python program starts with the following comment to make it executable:

#!/usr/bin/env python3

Any help would be appreciated

Note: I am running monit using sudo and using windows linux subsystem

1

There are 1 answers

0
lutzmad On

a suggestion only.

Monit use a limited environment to execute scripts like Initd or Systemd only. To get a more useful/proper environment use a shell script to start your python script and initialize the environment, set things like PYTHONPATH, etc., in the script.

See https://mmonit.com/monit/documentation/monit.html