test(bkgpslog): Test 'tee /dev/null' as passthrough-function
[EVA-2020-02.git] / exec / bkplog.sh
1 #!/bin/bash
2
3 # Date: 2020-04-10T04:32Z
4
5 # Author: Steven Baltakatei Sandoval
6
7 # License:
8
9 # Description: Logs pressure to a daily log file. Restarts daily at
10 # UTC midnight.
11
12 # Usage: plog.sh [ sensor script path ] [ output log dir path ]
13
14 echoerr() { echo "$@" 1>&2 ; } # Define echo to stderr function.
15
16 SENSOR_SCRIPT_PATH="$1" # Identify first argument as sensor script path.
17 DIROUT1="$2" # Identify second argument as output log file directory path.
18
19 SCRIPT_PATH="${0}" # [1]
20 SCRIPT_BASENAME="$(basename "$SCRIPT_PATH")"
21 SCRIPT_DIR="$(dirname "$SCRIPT_PATH")"
22 SCRIPT_TIME_SHORT="$(date +%Y%m%dT%H%M%S%z)"
23 SCRIPT_DATE_SHORT="$(date +%Y%m%d)"
24
25 SCRIPT_ERROR_LOG_PATH=~/"$SCRIPT_DATE_SHORT""_""$SCRIPT_BASENAME"..error.log
26
27 # Check that time synchronization service ('ntp') exists. Warn if it doesn't.
28 if command -v ntp 1>/dev/null 2>&1; then echoerr "WARNING: ntp not found. Log timestamps may not be accurate."; fi
29
30 # Main loop
31 if [ -d "$DIROUT1" ] && [ -f "$SENSOR_SCRIPT_PATH" ] ; then # Check that output directory and sensor script file exist.
32 while true; do
33 echoerr "DEBUG:Starting while loop."
34
35 # Update time constants
36 TIME_CURRENT="$(date --iso-8601=seconds)" ;
37 #echoerr "DEBUG:TIME_CURRENT is:""$TIME_CURRENT" ;
38 TIME_CURRENT_SHORT="$(date -d "$TIME_CURRENT" +%Y%m%dT%H%M%S%z)"
39 #echoerr "DEBUG:TIME_CURRENT_SHORT is""$TIME_CURRENT_SHORT"
40 DATE_CURRENT="$(date -d "$TIME_CURRENT" --iso-8601=date)" ;
41 #echoerr "DEBUG:DATE_CURRENT is:""$DATE_CURRENT" ;
42 DATE_CURRENT_SHORT="$(date -d "$TIME_CURRENT" +%Y%m%d)" ;
43 #echoerr "DEBUG:DATE_CURRENT is:""$DATE_CURRENT_SHORT" ;
44 DATE_TOMORROW="$(date -d "$TIME_CURRENT next day" --iso-8601=date)" ;
45 #echoerr "DEBUG:DATE_TOMORROW is:""$DATE_TOMORROW" ;
46 TIME_NEXT_MIDNIGHT="$(date -d "$DATE_TOMORROW" --iso-8601=seconds)" ;
47 #echoerr "DEBUG:TIME_NEXT_MIDNIGHT is:""$TIME_NEXT_MIDNIGHT" ;
48 ####SECONDS_UNTIL_NEXT_MIDNIGHT="$(( $(date +%s -d "$TIME_NEXT_MIDNIGHT") - $(date +%s -d "$TIME_CURRENT") ))" ;
49 SECONDS_UNTIL_NEXT_MIDNIGHT="10" # DEBUG 10 second. Hail Satan.
50 #echoerr "DEBUG:SECONDS_UNTIL_NEXT_MIDNIGHT is:""$SECONDS_UNTIL_NEXT_MIDNIGHT" ;
51 if [ "$SECONDS_UNTIL_NEXT_MIDNIGHT" -eq 0 ]; then echoerr "WARNING:Is it exactly midnight?" ; continue; fi ; # Exit loop early if equal to 0 because "timeout 0s" never ends.
52 if [ "$SECONDS_UNTIL_NEXT_MIDNIGHT" -lt 0 ]; then echoerr "ERROR:Time until midnight error." ; exit 1; fi ; # Exit script if equal to 0 because "timeout 0s" never ends.
53
54 # Update output file names.
55 DIROUTN="$DIROUT1" ;
56 FILEOUTN="$DATE_CURRENT_SHORT""..""$(hostname)""_pressure.csv" ;
57
58 # DEBUG
59 echoerr "SECONDS_UNTIL_NEXT_MIDNIGHT:""$SECONDS_UNTIL_NEXT_MIDNIGHT"
60 echoerr "SENSOR_SCRIPT_PATH :""$SENSOR_SCRIPT_PATH"
61 echoerr "DIROUTN :""$DIROUTN"
62 echoerr "FILEOUTN :""$FILEOUTN"
63
64 # Append pressure log until midnight.
65 timeout "$SECONDS_UNTIL_NEXT_MIDNIGHT""s" python2 "$SENSOR_SCRIPT_PATH" 1>> "$DIROUTN"/"$FILEOUTN" 2>&1 ; # log script data
66 done
67
68 else
69 echoerr "$SCRIPT_TIME_SHORT"">Sensor script file not found at: $SENSOR_SCRIPT_PATH or $DIROUT1 not found."
70 echo "$SCRIPT_TIME_SHORT"">Sensor script file not found at: $SENSOR_SCRIPT_PATH or $DIROUT1 not found." >> "$SCRIPT_ERROR_LOG_PATH" ;
71 fi
72
73 # == References ==
74 # 1. How to indicate script file name in a bash script. https://stackoverflow.com/a/3588939