3

I need postgresql shell script to alert me if database is goes down.

3
  • 1
    while true; do psql -c "select 1"; if [ $? -gt 0 ]; then echo 'alert'; fi; sleep 1; done; Commented Jun 13, 2017 at 9:12
  • Could u please tell me logic of this shell script. Commented Jun 13, 2017 at 9:24
  • 1
    it tries to connect to db once in a sec and if fails, echoes 'alert' Commented Jun 13, 2017 at 9:27

1 Answer 1

6

pg_isready is a utility for checking the connection status of a PostgreSQL database server. The exit status specifies the result of the connection check.

Example:

while true; do
    if ! /usr/bin/pg_isready &>/dev/null; then 
        echo 'alert';
    fi;
    sleep 3;
done;

This will check the status of postgresql database every 3 seconds and echos "alert" if it is down.

https://www.postgresql.org/docs/9.3/static/app-pg-isready.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.