【问题标题】:database connection in shell script failed when execute from cron从 cron 执行时,shell 脚本中的数据库连接失败
【发布时间】:2018-03-03 16:53:27
【问题描述】:

我写了一个脚本,从其他服务器获取和备份文件,它可以工作 从控制台执行时,但从 cron 执行时失败。

这是我的脚本。

#!/bin/bash

# working directory
dir_work="somedir/work"

# backup directory
dir_bkup="somedir/bkup"

# db2 setting
. $HOME/sqllib/db2profile

# Get file by using SCP
getFilebyScp(){
    scp -i KEY -p user@10.0.0.1:"$download_file" "$dir_work"
RC=$?
    if [ $RC -ne 0 ]; then
      { echo 'Cannot get the file...'; exit $RC; }
    fi
}

# Processing Start
echo 'START'

# DB Connection
db2 connect to DATABASE user USERNAME using PASSWORD || { echo "database connection failed."; exit 1; }

# assign file_information to an array
file_info="($(db2 -x -t "select status, file_path from SOME_TABLE \
                where id = 999 \
                and status = 2 \
                and entry_id = (select max(entry_id) from SOME_TABLE where id = 999)"))"
                
download_file="${file_info[1]}"

# check the status of file 
if [ "${file_info[0]}" -eq 2 ]; then
  getFilebyScp
elif [ "${file_info[0]}" -eq 1 ]; then
  { echo 'Skip the process because output if the file is not finished yet.'; exit 1; }
else
  { echo 'Skip the process because some error happen in output file.'; exit 1; }
fi

new_file="$(basename "${download_file// /_}")"
mv -f "$download_file" "$new_file"

bkup_file=${new_file%%.csv}'_'$(date +%Y%m%d%H%M%S).csv
mv -f "${new_file}" "${dir_bkup}"/"${bkup_file}" || { 'Backup failed...'; exit 1; }

echo 'END'

而crontab是这样的。

15 17 * * * /bin/bash -l /somedir/otherdir/myscript.sh > crontab.log 

这有什么问题?

补充说明

  • 脚本的用户账号和crontab的账号是一样的。
  • DB2 实例名称为 db2inst1
  • 操作系统为 RHEL(版本未知)。

[已解决]

我发现$() 命令是作为子shell 执行的,所以在子shell 中初始连接不可用。所以我修复了下面的脚本。

#!/bin/bash

# working directory
dir_work="somedir/work"

# backup directory
dir_bkup="somedir/bkup"

# db2 setting
. $HOME/sqllib/db2profile

# Get file by using SCP
getFilebyScp(){
    scp -i KEY -p user@10.0.0.1:"$download_file" "$dir_work"
RC=$?
    if [ $RC -ne 0 ]; then
      { echo 'Cannot get the file...'; exit $RC; }
    fi
}

# Processing Start
echo 'START'

# DB Connection
db2 connect to DATABASE user USERNAME using PASSWORD || { echo "database connection failed."; exit 1; }

# assign file_informations
db2 -txf "/somedir/otherdir/status.sql" > "$dir_work"/status
db2 -txf "/somedir/otherdir/path.sql" > "$dir_work"/path
original_file_status=$(cat "$dir_work/status")
original_file_path=$(cat "$dir_work/path")

download_file="${original_file_path}"

# check the status of file 
if [ "$original_file_status" -eq 2 ]; then
  getFilebyScp
elif [ "$original_file_status" -eq 1 ]; then
  { echo 'Skip the process because output if the file is not finished yet.'; exit 1; }
else
  { echo 'Skip the process because some error happen in output file.'; exit 1; }
fi

new_file="$(basename "${download_file// /_}")"
mv -f "$download_file" "$new_file"

bkup_file=${new_file%%.csv}'_'$(date +%Y%m%d%H%M%S).csv
mv -f "${new_file}" "${dir_bkup}"/"${bkup_file}" || { 'Backup failed...'; exit 1; }

echo 'END'

我将数组中的元素拆分为两个变量,并使用两个 SQL 文件为每个变量赋值。

感谢cmets!

【问题讨论】:

  • cron 运行时,您能否查看$HOME 是否可以访问?我确信HOME 使用了一个用户特定的路径,可能在使用 cron 时无法访问
  • 这可能会有所帮助:How to debug a bash script?
  • 你能给一些error trace吗?可能 cron 无法获得正确的 $HOME 值或某些命令无法正常工作,请尝试使用 eval()
  • $HOME 可以从cron访问,我使用bash -x ./myscript.sh调试了这个脚本,它显示db连接成功,但是在执行SQL时,它说“SQL1024N数据库连接不存在.SQLSTATE=08003".
  • 编辑您的问题以添加缺失的事实:(1) which crontab(哪个用户帐户)您正在使用?,(2) 您的 Db2 实例名称是哪个,因为我希望在 db2profile 的路径中看到该名称,例如:. ~db2inst1/sqllib/db2profile 和(3)哪个操作系统详细信息正在运行 bash(cygwin?,Windows-10 下的 bash,哪个 linux 发行版/版本)?

标签: bash shell cron db2


【解决方案1】:

$()命令作为子shell执行,所以在子shell中初始连接不可用。所以我修复了下面的脚本。

#!/bin/bash

# working directory
dir_work="somedir/work"

# backup directory
dir_bkup="somedir/bkup"

# db2 setting
. $HOME/sqllib/db2profile

# Get file by using SCP
getFilebyScp(){
    scp -i KEY -p user@10.0.0.1:"$download_file" "$dir_work"
RC=$?
    if [ $RC -ne 0 ]; then
      { echo 'Cannot get the file...'; exit $RC; }
    fi
}

# Processing Start
echo 'START'

# DB Connection
db2 connect to DATABASE user USERNAME using PASSWORD || { echo "database connection failed."; exit 1; }

# assign file_informations
db2 -txf "/somedir/otherdir/status.sql" > "$dir_work"/status
db2 -txf "/somedir/otherdir/path.sql" > "$dir_work"/path
original_file_status=$(cat "$dir_work/status")
original_file_path=$(cat "$dir_work/path")

download_file="${original_file_path}"

# check the status of file 
if [ "$original_file_status" -eq 2 ]; then
  getFilebyScp
elif [ "$original_file_status" -eq 1 ]; then
  { echo 'Skip the process because output if the file is not finished yet.'; exit 1; }
else
  { echo 'Skip the process because some error happens in the output file.'; exit 1; }
fi

new_file="$(basename "${download_file// /_}")"
mv -f "$download_file" "$new_file"

bkup_file=${new_file%%.csv}'_'$(date +%Y%m%d%H%M%S).csv
mv -f "${new_file}" "${dir_bkup}"/"${bkup_file}" || { 'Backup failed...'; exit 1; }

echo 'END'

我将数组中的元素拆分为两个变量,并使用两个 SQL 文件为每个变量赋值。

【讨论】:

    猜你喜欢
    • 2015-02-21
    • 1970-01-01
    • 2011-12-21
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 2015-11-14
    • 1970-01-01
    • 2021-01-20
    相关资源
    最近更新 更多