【问题标题】:script to keep retrying a heredoc psql query until it succeeds with until loop脚本继续重试heredoc psql查询,直到它通过直到循环成功
【发布时间】:2021-11-10 02:13:37
【问题描述】:

我通过psql手动运行多sql查询事务如下:

psql <<EOF
BEGIN;
Query1;
Query2;
COMMIT;
EOF

这里的问题是,有时我的交易失败,需要继续手动重试直到成功。当需要重复执行时,这会变得有点乏味。

有没有办法使用 until 来实现这一点?

到目前为止我的工作尝试是:

until psql --file transaction.sql
do
    sleep 5
done

但是,这需要一个超出此脚本范围的额外文件。

有没有办法让它以下列方式工作:

# this is currently not working for me.
until psql <<EOF
   BEGIN;
   Query1;
   Query2;
   COMMIT;
EOF
do
    sleep 5
done

【问题讨论】:

    标签: linux postgresql bash loops psql


    【解决方案1】:

    您可以尝试使用函数。

    #!/usr/bin/env bash
    
    my_function(){
    {
    psql <<EOF
    BEGIN;
    Query1;
    Query2;
    COMMIT;
    EOF
    } && return 0
    return 1
    }
    
    until my_function; do
      sleep 5
    done
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-28
      • 1970-01-01
      • 2013-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多