加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 如何在ENTRYPOINT中等待postgres启动?

发布时间:2020-12-15 18:23:33 所属栏目:安全 来源:网络整理
导读:在继续执行nosetests之前,等待postgres在ENTRYPOINT内完全启动的最佳方法是什么? 现在我把我的机器上的启动时间定在50秒左右.所以我只是睡了60秒.这感觉不太好,因为在另一台机器上运行时这可能不起作用. ENTRYPOINT runuser -l postgres -c '/usr/lib/pos
在继续执行nosetests之前,等待postgres在ENTRYPOINT内完全启动的最佳方法是什么?

现在我把我的机器上的启动时间定在50秒左右.所以我只是睡了60秒.这感觉不太好,因为在另一台机器上运行时这可能不起作用.

ENTRYPOINT 
           runuser -l postgres -c '/usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf & ' && 
           sleep 60 && 
           nosetests --verbose --cover-erase --with-coverage --cover-package=stalker

这是启动postgres的输出:

2017-02-13 13:46:49.541 UTC [9] LOG:  database system was interrupted; last known up at 2017-02-13 12:53:23 UTC
2017-02-13 13:47:37.951 UTC [9] LOG:  database system was not properly shut down; automatic recovery in progress
2017-02-13 13:47:37.994 UTC [9] LOG:  redo starts at 0/1783EA0
2017-02-13 13:47:37.995 UTC [9] LOG:  record with zero length at 0/17841E8
2017-02-13 13:47:37.995 UTC [9] LOG:  redo done at 0/17841B8
2017-02-13 13:47:37.995 UTC [9] LOG:  last completed transaction was at log time 2017-02-13 12:53:23.731984+00
2017-02-13 13:47:38.384 UTC [9] LOG:  MultiXact member wraparound protections are now enabled
2017-02-13 13:47:38.387 UTC [7] LOG:  database system is ready to accept connections
2017-02-13 13:47:38.387 UTC [13] LOG:  autovacuum launcher started

请注意,这与在ENTRYPOINT中运行多个命令的惯例相违背.在这种情况下,我有充分的理由这样做.

感谢@zeppelin建议pg_isready.我最终使用了这个:
#!/bin/bash
# wait-for-postgres.sh

set -e

cmd="$@"
timer="5"

until runuser -l postgres -c 'pg_isready' 2>/dev/null; do
  >&2 echo "Postgres is unavailable - sleeping for $timer seconds"
  sleep $timer
done

>&2 echo "Postgres is up - executing command"
exec $cmd

我在我的ENTRYPOINT中使用它:

ENTRYPOINT 

            # Start PostgreSQL
            runuser -l postgres -c '/usr/lib/postgresql/9.3/bin/postgres -D /var/lib/postgresql/9.3/main -c config_file=/etc/postgresql/9.3/main/postgresql.conf & ' && 

            # Exectute tests when db is up
            ./wait-for-postgres.sh nosetests --verbose --cover-erase --with-coverage --cover-package=stalker

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读