Find Elapsed Time or Duration of Long Running jobs without using sleep command
本问题已经有最佳答案,请猛点这里访问。
我试图捕获Linux中长时间运行的作业所用的时间。
我尝试了以下方法1,但它显示错误的持续时间,因为操作需要4个小时,但实际持续时间仅为几分钟
方法1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | $ 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 |
我尝试使用以下shell进行更改以分配变量并创建日志
方法2
这是我的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | ### 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 ### |
当我尝试导出以下变量时,我得到错误
1 2 3 4 5 6 7 8 | $ ${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 |
有人可以帮助修复我的第一个方法或第二个方法。
码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #!/bin/bash array=() function dump () { timestamp=$(date +%s) object=${1} action=${2} if ["${action}" =="start" ]; then array[object]=${timestamp} elif ["${action}" =="end" ]; then spent=$(( ${timestamp} - ${array[${object}]} )) echo"Time report for ${object}" echo"Started: $(date -d @${array[${object}]})" echo"Ended: $(date -d @${timestamp})" printf"Spent: %dh:%dm:%ds " $((${spent}/3600)) $((${spent}%3600/60)) $((${spent}%60)) fi } # MAIN dump"iter1""start" echo"iter1 started" sleep 2 dump"iter1""end" echo"iter2 started" dump"iter2""start" echo"iter3 started" dump"iter3""start" sleep 2 dump"iter2""end" sleep 2 dump"iter3""end" |
产量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | iter1 started Time report for iter1 Started: Wed Mar 20 11:27:17 UTC 2019 Ended: Wed Mar 20 11:27:19 UTC 2019 Spent: 0h:0m:2s iter2 started iter3 started Time report for iter2 Started: Wed Mar 20 11:27:19 UTC 2019 Ended: Wed Mar 20 11:27:21 UTC 2019 Spent: 0h:0m:2s Time report for iter3 Started: Wed Mar 20 11:27:19 UTC 2019 Ended: Wed Mar 20 11:27:23 UTC 2019 Spent: 0h:0m:4s |