大网数据广告活动
服务器运维问答

psql exit code 2 in bashscript

· 52CE 我爱测

postgres 9.6 Debian GNU/Linux 9 is use following simplyfied bash script wich gets called from a users crontab, myid is a passed parameter: #!/bin/bash ... PGPASSFILE="/home/user/.pgpass" PGCONNECT=" psql -U myuser -w -h 000.000.0…

问题原文

原帖链接 · FatFreddy · CC BY-SA 4.0

postgres 9.6 Debian GNU/Linux 9

is use following simplyfied bash script wich gets called from a users crontab, myid is a passed parameter:

#!/bin/bash
...
PGPASSFILE="/home/user/.pgpass"
PGCONNECT=" psql -U myuser -w -h 000.000.000.000 -d mydatabase -p 5432"
PGCOMMAND1="SELECT something, somemmore FROM schema.table WHERE id= ${myid}"
PGRESULT=`$PGCONNECT -A -F ";" -t -c "$PGCOMMAND1"`
RETCODE1=$?
...
PGCOMMAND2="SELECT schema.functionname(${myid});"
PGRESULT=`$PGCONNECT -A -F ";" -t -c "$PGCOMMAND2"`
RETCODE2=$?

if [ $RETCODE1 -eq 0 ] &&  [ $RETCODE2 -eq 0 ]; then
          mailx -s sendme_asuccessemail
else
          echo -e "ret1: $RETCODE1\n" >> $MAIL_FILE
          echo -e "ret2: $RETCODE2\n" >> $MAIL_FILE
          mailx -s sendme_errormailwith retcode1=2
fi

and most of the time all is ok ( the script runs 60 times a day to different times ). once per week or less i get a errormail with psql exit status 2 in RETCODE1 as a result of the select.

from the manpage:

 2 if the connection to the server went bad and the session was not
       interactive

it may happen, this script is called per usercrontab, that it gets executeted twice or more on the same time with different myid. The second command always gets called, which does the real work.

How can i narrow down the error ? What does the error realy mean ? Can i ignore it ?

已采纳解答

原帖链接 · asktyagi · CC BY-SA 4.0

psql exit code 2 can be ignored if it is not too frequent, reason could be network or db health. To dig more you can add debug steps to script which shows network connectivity and db health(server is busy and loaded with queries) before doing real work.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-09-02)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。