我写了一个脚本,得到&从其他服务器备份文件,它的工作原理 从控制台执行时,但从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
那有什么问题?
附加说明
[解决]
我发现$()
命令作为子shell执行,因此子shell中没有初始连接。所以我修改了下面的脚本。
#!/斌/庆典
# 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文件分配每个变量。
感谢您的评论!
答案 0 :(得分:0)
$()
命令作为子外壳程序执行,因此初始连接在子外壳程序中不可用。所以我修复了如下脚本。
#!/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文件为每个变量分配了变量。