Bash newbie so kindly bear with me!
Let us say I want to print output to an error log file and the console. I assume I have 2 options
Option 1: Include error logging inside the function
<code>copy_to_s3() {
local INPUT_FILE_NAME=$1
local BUCKET_NAME=$2
if aws s3 cp "${INPUT_FILE_NAME}" "s3://${BUCKET_NAME}" >error.log 2>&1; then
echo "Successfully copied the input file ${INPUT_FILE} to s3://${BUCKET_NAME}"
else
error=$(cat "error.log")
# EMAIL this error to the admin
echo "Something went wrong when copying the input file ${INPUT_FILE} to s3://${BUCKET_NAME}"
exit 1
fi
rm -rf "${INPUT_FILE_NAME}"
}
copy_to_s3 "test.tar.gz" "test-s3-bucket"
</code>
<code>copy_to_s3() {
local INPUT_FILE_NAME=$1
local BUCKET_NAME=$2
if aws s3 cp "${INPUT_FILE_NAME}" "s3://${BUCKET_NAME}" >error.log 2>&1; then
echo "Successfully copied the input file ${INPUT_FILE} to s3://${BUCKET_NAME}"
else
error=$(cat "error.log")
# EMAIL this error to the admin
echo "Something went wrong when copying the input file ${INPUT_FILE} to s3://${BUCKET_NAME}"
exit 1
fi
rm -rf "${INPUT_FILE_NAME}"
}
copy_to_s3 "test.tar.gz" "test-s3-bucket"
</code>
copy_to_s3() {
local INPUT_FILE_NAME=$1
local BUCKET_NAME=$2
if aws s3 cp "${INPUT_FILE_NAME}" "s3://${BUCKET_NAME}" >error.log 2>&1; then
echo "Successfully copied the input file ${INPUT_FILE} to s3://${BUCKET_NAME}"
else
error=$(cat "error.log")
# EMAIL this error to the admin
echo "Something went wrong when copying the input file ${INPUT_FILE} to s3://${BUCKET_NAME}"
exit 1
fi
rm -rf "${INPUT_FILE_NAME}"
}
copy_to_s3 "test.tar.gz" "test-s3-bucket"
Option 2: Include error logging when calling the function
<code>copy_to_s3() {
local INPUT_FILE_NAME=$1
local BUCKET_NAME=$2
if aws s3 cp "${INPUT_FILE_NAME}" "s3://${BUCKET_NAME}"; then
echo "Successfully copied the input file ${INPUT_FILE} to s3://${BUCKET_NAME}"
else
echo "Something went wrong when copying the input file ${INPUT_FILE} to s3://${BUCKET_NAME}"
exit 1
fi
rm -rf "${INPUT_FILE_NAME}"
}
copy_to_s3 "test.tar.gz" "test-s3-bucket" >error.log 2>&1
</code>
<code>copy_to_s3() {
local INPUT_FILE_NAME=$1
local BUCKET_NAME=$2
if aws s3 cp "${INPUT_FILE_NAME}" "s3://${BUCKET_NAME}"; then
echo "Successfully copied the input file ${INPUT_FILE} to s3://${BUCKET_NAME}"
else
echo "Something went wrong when copying the input file ${INPUT_FILE} to s3://${BUCKET_NAME}"
exit 1
fi
rm -rf "${INPUT_FILE_NAME}"
}
copy_to_s3 "test.tar.gz" "test-s3-bucket" >error.log 2>&1
</code>
copy_to_s3() {
local INPUT_FILE_NAME=$1
local BUCKET_NAME=$2
if aws s3 cp "${INPUT_FILE_NAME}" "s3://${BUCKET_NAME}"; then
echo "Successfully copied the input file ${INPUT_FILE} to s3://${BUCKET_NAME}"
else
echo "Something went wrong when copying the input file ${INPUT_FILE} to s3://${BUCKET_NAME}"
exit 1
fi
rm -rf "${INPUT_FILE_NAME}"
}
copy_to_s3 "test.tar.gz" "test-s3-bucket" >error.log 2>&1
2 questions
- Which of these methods is recommended?
- If I put this file inside a crontab like this, will it still log errors?
Crontab
<code>crontab -u ec2-user - <<EOF
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin
0 0,4,8,12,16,20 * * * /home/ec2-user/test.sh
EOF
</code>
<code>crontab -u ec2-user - <<EOF
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin
0 0,4,8,12,16,20 * * * /home/ec2-user/test.sh
EOF
</code>
crontab -u ec2-user - <<EOF
PATH=/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/home/ec2-user/.local/bin:/home/ec2-user/bin
0 0,4,8,12,16,20 * * * /home/ec2-user/test.sh
EOF