monit

When calling exec in Monit, nothing happens, although everything is fine in the logs


I have Monit 5.31.0 on Ubuntu 22.04.3 LTS and I'm struggling to understand why the exec is not working.

I have a monitoring file for a website, for example:

check host all with address google.com
group test
if failed url https://google.com with content = 'nothing' for 2 cycles
then exec "/home/ubuntu/bot/run_shell.sh"

This intentionally triggers an error (for testing) and should execute the script /home/ubuntu/bot/run_shell.sh

The script has a simple content: (I tried to simplify the script to the maximum - "echo 123" the result is the same)

#!/usr/bin/env bash
/usr/bin/python3.11 /home/ubuntu/bot/my_bot.py

If I run the script manually in the console:

./run_shell.sh or /home/ubuntu/bot/run_shell.sh Everything works and the script runs.

In the Monit logs:

[2023-08-11T16:56:45+0300] info     : Reinitializing monit daemon
[2023-08-11T16:56:45+0300] info     : Reinitializing Monit -- control file '/etc/monit/monitrc'
[2023-08-11T16:56:45+0300] info     : Monit reloaded
[2023-08-11T16:56:45+0300] warning  : 'all' failed protocol test [HTTP] at [google.com]:443/ [TCP/IP TLS] -- Network is unreachable
[2023-08-11T16:57:15+0300] error    : 'all' failed protocol test [HTTP] at [google.com]:443/ [TCP/IP TLS] -- Network is unreachable
[2023-08-11T16:57:15+0300] info     : 'all' exec: '/home/ubuntu/bot/run_shell.sh'

I see the line 'all' exec: '/home/ubuntu/bot/run_shell.sh' in the logs, but the script is not being executed.

I also tried adding exec "/bin/bash /home/ubuntu/bot/run_shell.sh" but it still didn't work.

Can you please help me identify the issue?

What I Tried:

Manually running the script with ./run_shell.sh and /home/ubuntu/bot/run_shell.sh in the console, which worked as expected. Using the original exec line as provided in the Monit configuration. Modifying the exec line to explicitly call the Bash interpreter. Expected Outcome:

I expected the exec line in the Monit configuration to trigger the execution of /home/ubuntu/bot/run_shell.sh, resulting in the successful execution of the Python script.

Actual Outcome:

Despite seeing the 'all' exec: '/home/ubuntu/bot/run_shell.sh' line in the logs, the script doesn't execute when triggered by Monit.

Any insights or suggestions on how to resolve this issue would be greatly appreciated. Thank you in advance for your assistance!


Solution

  • Because there is no output being saved with exec: did you ensure that the script is really not executed? For example, write something in the first line, like

    #!/usr/bin/env bash
    touch /tmp/monit-is-writing-this-file-yay
    
    /usr/bin/python3.11 /home/ubuntu/bot/my_bot.py
    

    Since monit seems to not log a problem executing your script, the problem source should be inside your script. One possible catch (by design) is, that monit does not provide an environment! So if python or your bot relys on anything you provide by ENV, you have to manually set it in your script, like

    #!/usr/bin/env bash
    
    # manually set env
    export PYTHON_WHATEVER="yes, please"
    export MYBOT_DATABASE="postgresql://user:pass@host:port/db"
    
    
    # and/or write env to be able to check it:
    env > /tmp/my_bot.env
    
    
    /usr/bin/python3.11 /home/ubuntu/bot/my_bot.py
    

    PS: If your script has the +x flag and a correct shebang (it has!), it will execute without problems. No need to set the interpreter manually, so just exec "/home/ubuntu/bot/run_shell.sh" is the correct way to go.