pinephone-scripts

Scripts and small applications for the PinePhone
git clone https://src.jayvii.de/pub/pinephone-scripts.git
Log | Files | Refs | README | LICENSE

daemon.sh (4236B)


      1 #!/usr/bin/env bash
      2 # Copyright (C) 2020 Jan "JayVii"
      3 # Author 2020 Jan "JayVii" <jayvii [AT] posteo [DOT] de>
      4 # SPDX-License-Identifier: gpl-3.0
      5 # About this header: <https://reuse.software>
      6 
      7 ###############################################################################
      8 # daemon.sh - time-watcher script for balarm.sh, the alarm-script written in  #
      9 #             bash.                                                           #
     10 #                                                                             #
     11 # ATTENTION: requires balarm.sh to be in $PATH                                #
     12 ###############################################################################
     13 
     14 # Configuration ---------------------------------------------------------------
     15 BASE_PATH="$HOME/.config/balarm"
     16 PID_FILE="$HOME/.cache/balarm.pid"
     17 RANG_FILE="$HOME/.cache/balarm.log"
     18 TIME_FILE="$BASE_PATH/timings.txt"
     19 
     20 # SetUp -----------------------------------------------------------------------
     21 if [ ! -d "$BASE_PATH" ]; then mkdir -p "$BASE_PATH"; fi
     22 if [ ! -f "$TIME_FILE" ]; then touch "$TIME_FILE"; fi
     23 if [ ! -f "$RANG_FILE" ]; then touch "$RANG_FILE"; fi
     24 if [ ! -f "$PID_FILE" ]; then touch "$PID_FILE"; fi
     25 
     26 # Functions -------------------------------------------------------------------
     27 
     28 # compares two time strings in "%H %M %a" format.
     29 # sets $TIME_MATCH to 1 if time is equal or diff by 1
     30 function compare_time {
     31     # Extract hours, minutes & day from current time
     32     _HOURS=$(echo "$1" | awk '{ print $1 }')
     33     _MINUTES=$(echo "$1" | awk '{ print $2 }')
     34     _DAY=$(echo "$1" | awk '{ print $3 }')
     35     # construct +1 and -1 minute strings
     36     if [[ $_MINUTES -eq 0 ]]; then
     37         if [[ $_HOURS -lt 11 ]]; then
     38             M1="0$(($_HOURS - 1)) 59 $_DAY"
     39         else
     40             M1="$(($_HOURS - 1)) 59 $_DAY"
     41         fi
     42         P1="$_HOURS $(($_MINUTES + 1)) $_DAY"
     43     elif [[ $_MINUTES -eq 59 ]]; then
     44         M1="$_HOURS 58 $_DAY"
     45         P1="$(($_HOURS + 1)) 00 $_DAY"
     46     else
     47         if [[ $_MINUTES -lt 11 ]]; then
     48             M1="$_HOURS 0$(($_MINUTES - 1)) $_DAY"
     49         else
     50             M1="$_HOURS $(($_MINUTES - 1)) $_DAY"
     51         fi
     52         P1="$_HOURS $(($_MINUTES + 1)) $_DAY"
     53     fi
     54     # compare strings
     55     if [[ "$2" == "$1" ]] || [[ "$2" == "$M1" ]]; then #|| [[ "$2" == "$P1" ]]; then
     56         TIME_MATCH=1
     57     else
     58         TIME_MATCH=0
     59     fi
     60 }
     61 
     62 # checks whether current alarm has been executed already.
     63 function rang_yet {
     64     # check whether current alarm has been executed today already...
     65     _RANG=$(grep "$CURRDATE $ALARM" "$RANG_FILE")
     66     if [[ "$_RANG" == "$CURRDATE $ALARM" ]]; then
     67         HAS_RANG=1 # yes, it has!
     68     else
     69         HAS_RANG=0 # no, it has not!
     70     fi
     71 }
     72 
     73 # Refresh PID file ------------------------------------------------------------
     74 
     75 # reported PID for daemon
     76 TMP_PID=$(cat "$PID_FILE")
     77 
     78 # does reported PID correspond to THIS process?
     79 DAE_PID=$(ps all | grep " $TMP_PID " | grep -v "grep" | grep "daemon.sh")
     80 
     81 # if so, restart daemon. Else just refresh PID
     82 if [ "$DAE_PID" ]; then
     83     echo "Refreshing..."
     84     kill -9 "$TMP_PID"
     85 fi
     86 
     87 # either way, refresh reported PID for daemon
     88 echo $$ > "$PID_FILE"
     89 
     90 # Run time-watch daemon -------------------------------------------------------
     91 
     92 while :; do
     93 
     94     # How many active alarms do we have?
     95     ALARM_N=$(wc -l "$TIME_FILE" | awk '{ print $1 }')
     96 
     97     # Check current time and date
     98     CURRTIME=$(date "+%H %M %a")
     99     CURRDATE=$(date "+%Y %m %d")
    100 
    101     # loop through all set up alarms
    102     for i in $(seq 1 1 $ALARM_N); do
    103         # current alarm's time
    104         ALARMTIME=$(sed -ne "${i}p" "$TIME_FILE")
    105         # compare alarm time to current time
    106         compare_time "$CURRTIME" "$ALARMTIME"
    107         # if they match, check whether it has been executed already
    108         if [ $TIME_MATCH -eq 1 ]; then
    109             rang_yet
    110             # if it has not been executed yet, execute it
    111             if [ $HAS_RANG -eq 0 ]; then
    112                 balarm.sh &
    113                 echo "$CURRDATE $ALARMTIME" > "$RANG_FILE"
    114             fi
    115         fi
    116     done
    117 
    118     # timeout (1min - execution time)
    119     CURRSEC=$(date +%S) # current time (seconds) after execution loop
    120     sleep $((60 - $CURRSEC)) # should precisely land on HH:MM+1:00.
    121 
    122 done
    123 
    124 # EOF daemon.sh