Search code examples
pythonbashshellubuntukeep-alive

How Can I Make a Python Script to Get a Return?


I want to make a script with python for bash that returns 1 or 0, how can I do that? To make more clear I can give that example:

I am trying to do configurations for keepalived and it needs a script that returns 0 or 1 to check whether an application is running or not, but I have no idea how to make that script with python?

There is an example code but it uses .sh instead of .py, I want to make this with .py script:

vrrp_script chk_myscript {
script       "/usr/local/bin/mycheckscript.sh" # the script must be .py
interval 2   # check every 2 seconds
fall 2       # require 2 failures for KO
rise 2       # require 2 successes for OK
}

Solution

  • sys.exit should do the trick

    import sys
    sys.exit(0)
    

    and

    import sys
    sys.exit(1)
    

    How to use sys.exit() in Python