Created
September 4, 2020 17:29
-
-
Save 181221/c8f3253eb63f5050533a0e3e61931179 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
FILE=$LOGFILE | |
WORKING_DIR=$(pwd) | |
MESSAGE="This program works as follows: track [start | <jobname>][stop][status] | |
track start <jobname>, will start track logging in the specified log file. | |
track status, reads from the last line of the log file and checks if there is a logged job. | |
track stop, stop the job | |
" | |
if [ -z "$FILE" ]; # check if there is an environment - if not export default one | |
then | |
echo "No environment variable set" | |
echo "Setting environment variable LOGFILE=${WORKING_DIR}/LOGFILE.txt'" | |
# export LOGFILE=${WORKING_DIR}/LOGFILE.txt default working dir | |
export LOGFILE=~/.local/share/LOGFILE.txt | |
fi | |
if [ ! -f "$FILE" ]; then # if logfile does not exit create one. | |
touch $LOGFILE | |
FILE=$LOGFILE | |
echo "Created file $LOGFILE" | |
echo $MESSAGE | |
fi | |
startDateString="" | |
declare -a durations # durations array for appending duration of task. Then it can be printed out later. | |
track () { | |
if [[ $# -eq 0 ]]; then # no args | |
echo $MESSAGE | |
exit 1 | |
fi | |
type="$1" | |
label="$2" | |
line=$(tail -n 1 $FILE) # read last line of file | |
jobName=$(tail -n 1 $FILE| cut -d " " -f 5) # get the label name of the job | |
case "$type" in | |
"start") | |
if [[ -z $label ]]; then | |
echo "missing label to start job" && exit 1 | |
fi | |
if [ ! -z "$line" ]; then | |
echo "Job is already running" && exit 1 | |
else | |
startDateFormatted=$(date '+%A, %B %d, %Y. %H:%M:%S') | |
startDateString=$(date '+%H:%M:%S') | |
echo "START ${startDateFormatted}" >> $FILE | |
echo "LABEL This is task $label" >> $FILE | |
fi | |
;; | |
"stop") | |
isEnd=$(tail -n 1 $FILE | cut -d ' ' -f 1) | |
if [[ $isEnd == "LABEL" ]] # Check that job is not running. | |
then | |
# Get end label format dates and calculate duration. | |
# Add duration to durations array | |
# append to file | |
endLabel=$(tail -n 1 $FILE | cut -d ' ' -f 5) | |
stopDateFormatted=$(date '+%A, %B %d, %Y. %H:%M:%S') # for display | |
stopDateString=$(date '+%H:%M:%S') # for calculating duration | |
StartDate=$(date -u -d "$startDateString" +"%s") | |
FinalDate=$(date -u -d "$stopDateString" +"%s") | |
duration=$(date -u -d "0 $FinalDate sec - $StartDate sec" +"%H:%M:%S") | |
str="Task ${endLabel}: ${duration}" | |
durations+=($str) | |
echo "END ${stopDateFormatted}" >> $FILE | |
echo >> $FILE | |
else | |
echo "No jobs running" | |
fi | |
;; | |
"status") | |
if [[ $jobName ]] ; then | |
echo $startDate | |
echo "Currently tracking: $jobName" | |
else | |
echo "No jobs running" | |
fi | |
;; | |
"log") | |
for value in "${durations[@]}" | |
do | |
echo $value | |
done | |
;; | |
*) | |
echo $MESSAGE | |
;; | |
esac | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment