Docs/数据库/postgresql/monitor.sh
2022-10-18 16:59:37 +08:00

25 lines
522 B
Bash
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Edit 2020/04/16
# sql script
# https://blog.csdn.net/Hehuyi_In/article/details/95893869
# example
# sql="select count(*) from pg_stat_activity where state='idle in transaction';"
# message="处于空闲状态的会话"
sql=""
message=""
user=postgres
source /home/$user/.bash_profile
if [ $UID -ne `id -u $user` ];then
echo -e "\033[31;1mNot Manager of PostgreSQL! \033[0m"
exit 1
fi
# get value
valuse=`psql postgres -c "$sql" | sed -n '3p' | sed 's/ //g'`
echo "$message=$valuse"