编写一个shell,查询greenplum空进程,并杀掉进程
时间: 2023-07-13 18:16:36 浏览: 117
可以编写如下的Shell脚本来查询并杀掉Greenplum的空进程:
```bash
#!/bin/bash
# 查询正在运行的Greenplum进程
pg_processes=$(ps -ef | grep postgres | grep postgres | grep -v grep)
if [ -z "$pg_processes" ]; then
echo "No Greenplum processes running." >&2
exit 1
fi
# 遍历进程,找到空进程并杀掉
while read -r pg_process; do
pg_pid=$(echo "$pg_process" | awk '{ print $2 }')
pg_mem=$(pmap "$pg_pid" | tail -n 1 | awk '{ print $2 }')
if [ "$pg_mem" = "0K" ]; then
echo "Killing empty Greenplum process (PID: $pg_pid)"
sudo kill -9 "$pg_pid"
fi
done <<< "$pg_processes"
```
这个脚本首先查询正在运行的Greenplum进程,然后遍历每一个进程。对于每一个进程,它使用`pmap`命令查询它的内存占用情况。如果内存占用为0,就认为这是一个空进程,使用`kill`命令杀掉它。
相关问题
编写一个shell,通过pg_stat_activity查询greenplum空进程,并杀掉进程
可以编写如下Shell脚本来查询并杀掉Greenplum的空进程:
```bash
#!/bin/bash
# 查询正在运行的Greenplum进程
pg_processes=$(psql -h <host> -p <port> -U <user> -c "SELECT pid, usename, datname, application_name, client_addr, query, state FROM pg_stat_activity WHERE state = 'idle' AND query NOT LIKE '%IDLE%' AND query NOT LIKE '%pg_stat_activity%';" -t)
if [ -z "$pg_processes" ]; then
echo "No Greenplum idle processes running." >&2
exit 1
fi
# 遍历进程,找到空进程并杀掉
while read -r pg_process; do
pg_pid=$(echo "$pg_process" | awk '{ print $1 }')
echo "Killing idle Greenplum process (PID: $pg_pid)"
psql -h <host> -p <port> -U <user> -c "SELECT pg_terminate_backend($pg_pid)"
done <<< "$pg_processes"
```
这个脚本首先通过`psql`命令查询正在运行的Greenplum进程,筛选出状态为`idle`、且`query`不包含`IDLE`和`pg_stat_activity`的进程。然后遍历每一个进程,使用`psql`命令杀掉进程。
编写一个shell,通过pg_stat_activity查询greenplum空进程,并调用pg_terminagte_backend进程
可以编写如下Shell脚本来查询并杀掉Greenplum的空进程:
```bash
#!/bin/bash
# 查询正在运行的Greenplum进程
pg_processes=$(psql -h <host> -p <port> -U <user> -c "SELECT pid, usename, datname, application_name, client_addr, query, state FROM pg_stat_activity WHERE state = 'idle' AND query NOT LIKE '%IDLE%' AND query NOT LIKE '%pg_stat_activity%';" -t)
if [ -z "$pg_processes" ]; then
echo "No Greenplum idle processes running." >&2
exit 1
fi
# 遍历进程,找到空进程并杀掉
while read -r pg_process; do
pg_pid=$(echo "$pg_process" | awk '{ print $1 }')
echo "Killing idle Greenplum process (PID: $pg_pid)"
psql -h <host> -p <port> -U <user> -c "SELECT pg_terminate_backend($pg_pid)"
done <<< "$pg_processes"
```
这个脚本首先通过`psql`命令查询正在运行的Greenplum进程,筛选出状态为`idle`、且`query`不包含`IDLE`和`pg_stat_activity`的进程。然后遍历每一个进程,使用`psql`命令调用`pg_terminate_backend`函数杀掉进程。
阅读全文