Tuesday, December 22, 2015

Simple Message Scheduler -- w/Zenity

#!/usr/bin/env bash 

### Copyrighted by +A.M.Danischewski  2015+ (c)
### This program may be reutilized without limits, provided this 
### notice remain intact. 

 ## Any valid date format will do  
 ##  E.g. "+ 10 minutes", "+ 2 hours", "23:00:00", "@1450829119"
declare     MSG_FMTSTRING=${1:-"20:00:00"} 
declare     MSG_SCHEDULE
declare     PROG_NAME="${0##*/}"
declare -r  SEND_MSG=${2:-"Check up on Marina"}
declare -ir SLEEP_CYCLE=2 
declare -ir ZENITY_WIDTH=400 
declare -ir ZENITY_HEIGHT=200 
declare -r  ZENITY_MSG_TITLE="Important Reminder" 

function usage() { 
 echo -en "\n Usage: ${0##*/} <date format str> <\"message\">\n" 
 echo -en "  E.g. date formats =>> \"+ 10 minutes\", \"+ 2 hours\", \"23:00:00\", \"@1450829119\"\n\n"
 exit 0  
}  

function list_sched_msgs() { 
local cmd_pid
local cmd_line
local cmd_sched
local start_time 
local end_time
while IFS= read a; do 
 cmd_pid=$(awk "{print \$1}" <<< "${a}") 
 cmd_line=$(sed "s/^.*${cmd_pid} *//" <<< "${a}" | cut -d' ' -f3-)
 cmd_sched=$(awk "{print \$4}" <<< "${a}") 
   ## If its not a normal time (20:00:00) then presume its an offset (+ 5 Seconds). 
 ! grep -Eq "@|:" <<< "${cmd_sched}" && cmd_sched=$(grep -iEo '^.*(hour[s]*|minute[s]*|second[s]*)' <<< "${cmd_line}" | tr '[[:upper:]]' '[[:lower:]]') 
 start_time=$(date -d"$(ps -o lstart= -p${cmd_pid})")
 end_time=$(date -d"${start_time} ${cmd_sched}" 2>/dev/random)
  ### If end_time had an error (for the simple "+ 1 hour" type cases, then calculate off the 
  ### start time of the process. 
 (($?)) && end_time=$(date -d"@$(($(date -d"${start_time}" +%s)+$(($(date -d"${cmd_sched}" +%s)-$(date -d"${start_time}" +%s)))))")
 printf "Sched msg: %-35.35s.. Pid:%6.6s  Ends: %-.35s\n" "${cmd_line##*/}" "${cmd_pid}" "${end_time}"  
done < <(ps -eo pid,cmd | grep "[${PROG_NAME:0:1}]${PROG_NAME:1}" | grep -v "[${PROG_NAME:0:1}]${PROG_NAME:1} -l") 
exit 0 
}

function kill_msgs() { 
 kill $(ps -eo pid,cmd | grep "[${PROG_NAME:0:1}]${PROG_NAME:1}" | grep -Ev "[${PROG_NAME:0:1}]${PROG_NAME:1} -(k|l)" | awk '{printf $1" "}')
 exit 0 
} 

function sanity_checks() {
[[ "${MSG_FMTSTRING}" =~ ^-l ]] && list_sched_msgs
[[ "${MSG_FMTSTRING}" =~ ^-k ]] && kill_msgs
[[ "${MSG_FMTSTRING}" =~ ^-	]] && usage 
MSG_SCHEDULE=$(date --date="${MSG_FMTSTRING}" +%s 2>/dev/random)
if (($?)); then 
 echo -en "\nImproperly formatted date string (${MSG_FMTSTRING}) valid strings include: \n"
 echo -en "\t\"+ 10 minutes\", \"+ 2 hours\", \"23:00:00\", \"@1450829119\"\n\n"
 exit 0 
fi 
}
   
function main() { 
sanity_checks	
while :; do 
 if (($(date +%s) > ${MSG_SCHEDULE})); then 
   paplay "/usr/share/sounds/freedesktop/stereo/complete.oga"       
   zenity --width=${ZENITY_WIDTH} --height=${ZENITY_HEIGHT} --text-info \
          --title "${ZENITY_MSG_TITLE}" <<< "${SEND_MSG}"
   break; 
 else 
   sleep ${SLEEP_CYCLE}; 
 fi 
done & ## Background our dear process.  
} 

main 
exit 0 
 
alias msg='msg_sched.bsh' 
alias lsmsgs='msg_sched.bsh -l' 
alias killmsgs='msg_sched.bsh -k' 
alias wakeups='_(){ INTERVALMIN=$((${1}*60)); MSGCNT=${2}; CURRTIME=$(($(date +%s)+${INTERVALMIN})); for a in $(eval echo {${CURRTIME}..$((${CURRTIME}+$((${INTERVALMIN}*${MSGCNT}))))..${INTERVALMIN}}); do msg_sched.bsh "@${a}" "Wake up its $(date -d"@${a}")"; done; lsmsgs; }; _' 

No comments:

Post a Comment