user7590556
user7590556

Reputation:

Print console logs of a shell script

I have a shell script like below. I want to save the console full logs to a file.

#!/bin/bash
#This script is to import tables from mysql to hdfs

source /home/$USER/source.sh

[ $# -ne 1 ] && { echo "Usage : $0 table ";exit 1; }

table=$1

TIMESTAMP=`date "+%Y-%m-%d"`
touch /home/$USER/logs/${TIMESTAMP}.success_log
touch /home/$USER/logs/${TIMESTAMP}.fail_log
success_logs=/home/$USER/logs/${TIMESTAMP}.success_log
failed_logs=/home/$USER/logs/${TIMESTAMP}.fail_log

#Function to get the status of the job creation
function log_status
{
status=$1
message=$2
if [ "$status" -ne 0 ]; then
echo "`date +\"%Y-%m-%d %H:%M:%S\"` [ERROR] $message [Status] $status : failed" | tee -a "${failed_logs}"
#echo "Please find the attached log file for more details"
exit 1
else
echo "`date +\"%Y-%m-%d %H:%M:%S\"` [INFO] $message [Status] $status : success" | tee -a "${success_logs}"
fi
}

`hive -e "create table test_1 as select * from db.test"`

g_STATUS=$?
log_status $g_STATUS "Table created ${table}"
echo "******************************************************************************"

When I run the script The logs show only whether the job is success or failed. How do I get the whole console logs saved to a file.

How do save the both status logs to one file and the console logs to another file

Upvotes: 1

Views: 2769

Answers (1)

heemayl
heemayl

Reputation: 41987

Leverage two file descriptors, pointing to two different files and then redirect the relevant logs to the desired file descriptors.

For example, put this after shebang:

exec 3>console.log
exec 4>status.log

Now, to send all console logs to console.log, do:

echo .... >&3

Similarly, for all status logs:

echo ... >&4

Change the file names to meet your need.

Upvotes: 1

Related Questions