代码之家  ›  专栏  ›  技术社区  ›  dar

如何优雅地杀死过时的服务器进程postgres

  •  3
  • dar  · 技术社区  · 17 年前

    在我们的实验室中,我们的postgres 8.3数据库偶尔会从pid文件中孤立出来,当我们尝试关闭数据库时会收到以下消息:

    Error: pid file is invalid, please manually kill the stale server process postgres

    当这种情况发生时,我们立即 pg_dump 所以我们可以稍后恢复数据库。但是,如果我们杀了那个孤儿 postgres psql 在杀死它之前,数据都是可用的,因此为什么

    有没有一种方法可以优雅地关闭孤立的postgres进程,这样我们就不必经过pg_dump和还原?或者有没有一种方法可以在杀死孤立进程后恢复数据库?

    3 回复  |  直到 17 年前
        1
  •  4
  •   Milen A. Radev    17 年前

    根据 documentation 你可以发送SIGTERM或SIGQUIT。SIGTERM是首选。无论哪种方式,都不要使用SIGKILL(正如你从个人经验中所知)。

    编辑: 另一方面,你所经历的并不正常,可能表明配置错误或错误。请在以下方面寻求帮助 pgsql-admin 邮件列表。

        2
  •  3
  •   Magnus Hagander    17 年前

    使用kill-9。

    但要回答直接的问题,请对进程使用常规的kill(no-9)来关闭它。如果有多个运行中的postgres进程,请确保杀死所有这些进程。

        3
  •  0
  •   Dan Benamy    14 年前

    我每分钟使用一个由cron运行的脚本,如下所示。

    #!/bin/bash
    
    DB="YOUR_DB"
    
    # Here's a snippet to watch how long each connection to the db has been open:
    #     watch -n 1 'ps -o pid,cmd,etime -C postgres | grep $DB'
    
    # This program kills any postgres workers/connections to the specified database
    # which have been running for 2 or 3 minutes. It actually kills workers which
    # have an elapsed time including "02:" or "03:". That'll be anything running
    # for at least 2 minutes and less than 4. It'll also cover anything that
    # managed to stay around until an hour and 2 or 3 minutes, etc.
    #
    # Run this once a minute via cron and it should catch any connection open
    # between 2 and 3 minutes. You can temporarily disable it if if you need to run
    # a long connection once in a while.
    #
    # The check for "03:" is in case there's a little lag starting the cron job and
    # the timing is really bad and it never sees a worker in the 1 minute window
    # when it's got "02:".
    old=$(ps -o pid,cmd,etime -C postgres | grep "$DB" | egrep '0[23]:')
    if [ -n "$old" ]; then
        echo "Killing:"
        echo "$old"
        echo "$old" | awk '{print $1}' | xargs -I {} kill {}
    fi