如何从shell脚本中的postgresql提取pg_backend_pid并将其传递给另一个进程? [英] How to extrace pg_backend_pid from postgresql in shell script and pass it to another process?

查看:457
本文介绍了如何从shell脚本中的postgresql提取pg_backend_pid并将其传递给另一个进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在命令行(或脚本)上运行bin/psql并打印出它的pg_backend_pid,以便可以将pg_backend_pid作为命令行参数传递给另一个进程(由root运行).我的问题是,其他进程获取pid后需要运行.然后,psql(具有相同的pid会话)在其他进程开始后运行查询.

I need to run bin/psql on the command line (or script) and print its pg_backend_pid out, so that the pg_backend_pid can be passed to another process (run by root) as command line argument. The problem for me is that the other process needs to run after it obtains the pid. The psql (with same pid session) then runs a query after the other process has started.

诀窍是Psql需要等待,直到其他进程获得pg_backend_pid,并且它必须保持相同的会话.

The trick is that Psql needs to wait until the other process gets the pg_backend_pid and it has to remain the same session.

这可以通过shell脚本或perl完成吗?

Can this be done through shell script or perl?

推荐答案

您将要在bash或与某种双向管道的perl中使用协同处理.在Python中,您可以使用os.popen2命令; perl还具有通过管道与子流程进行交互的功能.但是,尽可能使用DBD::Pgpsycopg2之类的本地语言数据库驱动程序要好得多.

You'll want to use a coprocess in bash, or in perl with some kind of two-way pipe. In Python you can use the os.popen2 command; perl also has facilities for interacting with subprocesses over pipes. However, it's much better to use native language database drivers like DBD::Pg or psycopg2 if at all possible.

如果必须在外壳中执行此操作,请参见"info bash"并搜索"coprocess".

If you must do this in the shell, see "info bash" and search for "coprocess".

这是一个快速的演示bash脚本,可以帮助您入门.

Here's a quick demo bash script to get you started.

#!/bin/bash
set -e -u
DBNAME=whatever_my_db_is_called
coproc psql --quiet --no-align --no-readline --tuples-only -P footer=off --no-password "$DBNAME"
echo 'SELECT pg_backend_pid();' >&${COPROC[1]}
read -u ${COPROC[0]} backend_pid
echo "Backend PID is: ${backend_pid}"
echo "SELECT random();" >&${COPROC[1]}
read -u ${COPROC[0]} randnum
echo "\q" >&${COPROC[1]}
wait %1
echo "Random number ${randnum} generated by pg backend ${backend_pid}"

psql的参数是确保它不会暂停输入,不会将制表符或metachar解释为readline命令,并且不能漂亮地打印输出,因此在shell上进行交互更容易.

The arguments to psql are to ensure it doesn't pause for input, doesn't interpret tabs or metachars as readline commands, and doesn't pretty-print output so it's easier to interact with on the shell.

或者,您根本可能根本不需要psql,您只需要通过某种脚本与PostgreSQL服务器通信即可.如果真是这样,仅将脚本语言与PostgreSQL数据库接口一起使用将更加容易.例如,在Python中:

Alternately, it's possible you don't really need psql at all, you just need to talk to the PostgreSQL server via some kind of script. If that's the case, it'll be MUCH easier to just use a scripting language with a PostgreSQL database interface. In Python, for example:

#!/usr/bin/env python
import os
import sys
import psycopg2

def main():
        conn = psycopg2.connect("dbname=classads")
        curs = conn.cursor()
        curs.execute("SELECT pg_backend_pid();");
        pid = curs.fetchall()[0][0]
        # Do whatever you need to here,
        # like using os.system() or os.popen() or os.popen2() to talk to
        # system commands, using curs.execute() to talk to the database, etc.
        conn.close();

if __name__ == '__main__':
        main()

在Perl中,您可以使用DBI和DBD :: Pg达到类似的效果.

In Perl you can use DBI and DBD::Pg to achieve a similar effect.

这篇关于如何从shell脚本中的postgresql提取pg_backend_pid并将其传递给另一个进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆