I am trying to capture the elapsed time for long running jobs in Linux.
I tried the following method 1, but it shows WRONG duration as the operation took 4 hours, but actual duration in minutes only
METHOD 1:
$ TAR_STARTTIMESTAMP=$(date +%s)
$ TAR_STARTTIME=`date +"%d/%b/%Y %H:%M"`
$ tar -cvf /u05/expdpdump/exppdb/TAR_EXP_DUMP_Wallet.tar /u05/expdpdump/exppdb/*.dmp
tar: Removing leading '/' from member names
/u05/expdpdump/exppdb/NoTDE_PDB_FULL.dmp
tar: /u05/expdpdump/exppdb/NoTDE_PDB_FULL.dmp: file changed as we read it
/u05/expdpdump/exppdb/NoTDE_PDB_FULL_wallet_01.dmp
/u05/expdpdump/exppdb/NoTDE_PDB_FULL_wallet_02.dmp
$ TAR_TAR_ENDTIMESTAMP=$(date +%s)
$ TAR_ENDTIME=`date +"%d/%b/%Y %H:%M"`
$ TAR_DIFFTIMESTAMP=$(( ${TAR_ENDTIMESTAMP} - ${TAR_STARTTIMESTAMP} ))
$ TAR_DIFFTIME=`date -u -d @${TAR_DIFFTIMESTAMP} "+%H hours, %M minutes and %S seconds"`
$ echo "Tar Started: ${TAR_STARTTIME}" >> /u05/expdpdump/exppdb/EXP_TAR_DUMP.log
$ echo "Tar Finished: ${TAR_ENDTIME}" >> /u05/expdpdump/exppdb/EXP_TAR_DUMP.log
$ echo "Tar took ${TAR_DIFFTIME} to complete" >> /u05/expdpdump/exppdb/EXP_TAR_DUMP.log
$ less /u05/expdpdump/exppdb/EXP_TAR_DUMP.log
Tar Started: 19/Mar/2019 19:12
Tar Finished: 19/Mar/2019 20:29
Tar took 04 hours, 47 minutes and 30 seconds to complete
I tried to change with the following shell to assign variables and create log
METHOD 2
Here is my code
### Code Starts Here ###
read -p "Enter the File Path: " FILE_PATH
read -p "Enter the Log Name: " LOG_NAME
read -p "Enter the Activity Name: " ACTIVITY
touch $FILE_PATH/$LOG_NAME
"${ACTIVITY}"_START_TIMESTAMP=$(date +%s)
ACTIVITY_START_TIME=`date "+%Y/%m/%d %H:%M:%S"`
# run the desired long running activity
$ACTIVITY_END_TIMESTAMP=$(date +%s)
$ACTIVITY_END_TIME=`date "+%Y/%m/%d %H:%M:%S"`
$ACTIVITY_DIFF_TIMESTAMP=$(( ${ACTIVITY_END_TIMESTAMP} - ${ACTIVITY_START_TIMESTAMP} ))
$ACTIVITY_DIFF_TIME=`date -u -d @${ACTIVITY_DIFF_TIMESTAMP} "+%H hours, %M minutes and %S seconds"`
echo "$ACTIVITY Started: ${ACTIVITY_STARTTIME}" >> $FILE_PATH/$LOG_NAME
echo "$ACTIVITY Finished: ${ACTIVITY_ENDTIME}" >> $FILE_PATH/$LOG_NAME
echo "$ACTIVITY took ${ACITIVITY_DIFFTIME} to complete" >> $FILE_PATH/$LOG_NAME
### Code Ends Here ###
when i try to export the following variable, i get the error
$ ${ACTIVITY}_START_TIMESTAMP=$(date +%s)
-bash: TAR_START_TIMESTAMP=1553021544: command not found
$ '${ACTIVITY}'_START_TIMESTAMP=$(date +%s)
-bash: ${ACTIVITY}_START_TIMESTAMP=1553021623: command not found
$ "${ACTIVITY}"_START_TIMESTAMP=$(date +%s)
-bash: TAR_START_TIMESTAMP=1553021737: command not found
$ "${ACTIVITY}_START_TIMESTAMP=$(date +%s)"
-bash: TAR_START_TIMESTAMP=1553021808: command not found
Can someone either help fixing my first method or 2nd Method.