I wrote a script which get & backup the file from other server, and it works when executing it from console, but it fails when executing from cron.
Here is my script.
#!/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'
and crontab is like this.
15 17 * * * /bin/bash -l /somedir/otherdir/myscript.sh > crontab.log
what's wrong with that?
additional note
- user account of script and crontab's account is same.
- DB2 instance name is db2inst1
- Operating system is RHEL(version is unknown).
[SOLVED]
I found $() command is executed as sub shell, so initial connection is not available in the sub shell. so I fixed the script like below.
#!/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'
I split elements in the array into two variables, and assigned each variables by using two SQL files.
Thank you for comments!