commit c1244d9dc79a525fc0be990c931eefd1c8fd6a81 from: vincent.delft date: Sat Jul 21 17:22:29 2018 UTC adapt vdcron for my Linux friends ;-) commit - 50915b378f73c0c2b94140d17a41528f39300603 commit + c1244d9dc79a525fc0be990c931eefd1c8fd6a81 blob - f893658f4cefe5d2ec00431108fcdc5844d505f0 blob + 71d79ccda6e0832f567254e7e028f98e69029c3d --- tests/scenarios +++ tests/scenarios @@ -37,31 +37,65 @@ else echo "==== END ===" fi } +if date --version >/dev/null 2>&1 ; then + echo Using GNU date + GNU_DATE=1 + BSD_DATE= +else + echo Using BSD date + GNU_DATE= + BSD_DATE=1 +fi + + +fmt_to_epoch(){ + local _date_string + if [ "$BSD_DATE" ]; then + echo $(date -j +%s $1) + else + if [ ${#1} -eq 12 ]; then + #Linux do not recognize YYYYmmddHHMM, but he recognize YYYYmmdd HHMM + _date_string="${1%????} ${1#????????}" + else + _date_string=$1 + fi + echo $(date -d "$_date_string" +%s) + fi +} + +epoch_to_fmt(){ + if [ "$BSD_DATE" ]; then + echo $(date -j -r $1 "+$2") + else + echo $(date -d @$1 "+$2") + fi +} + plus_10min() { -local _ref=$(date -j +%s $1) +local _ref=$(fmt_to_epoch $1) _ref=$((_ref+10*60)) -while [ "$(date -j -r $_ref +%s)" -lt "$(date +%s)" ] +while [ "$_ref" -lt "$(date +%s)" ] do _ref=$((_ref+60*10)) done -DATEREFERENCE=$(date -j -r $_ref +$2) +DATEREFERENCE=$(epoch_to_fmt $_ref $2) } today_plus_10days() { -local _now=$(date -j +%s) +local _now=$(date +%s) _now=$((_now+10*3600*24)) -DATEREFERENCE=$(date -j -r $_now +%Y%m%d) +DATEREFERENCE=$(epoch_to_fmt $_now %Y%m%d) } today_plus_1day() { -local _now=$(date -j +%s) +local _now=$(date +%s) _now=$((_now+24*3600)) -DATEREFERENCE=$(date -j -r $_now +%A) +DATEREFERENCE=$(epoch_to_fmt $_now %A) } today_plus_1month() { -local _now=$(date -j +%s) +local _now=$(date +%s) local _step [ $(date +%d) -gt 27 ] && _step=864000 || _step=2678400 _now=$((_now+_step)) -DATEREFERENCE=$(date -j -r $_now +%B) +DATEREFERENCE=$(epoch_to_fmt $_now %B) } @@ -109,7 +143,7 @@ fi echo "test $TEST" _refdate=$(date +%s) _refdate=$((_refdate-86400)) - _refdate=$(date -j -r $_refdate +%Y%m%d%H%M) + _refdate=$(epoch_to_fmt $_refdate %Y%m%d%H%M) echo "+$_refdate/10 echo test $TEST" > $CONF ./vdcron -s0 -f $CONF check_log "On .* execute: .* echo test \+YYYYmmddHHMM/10" @@ -197,7 +231,7 @@ fi echo "test $TEST" _refdate=$(date +%s) _refdate=$((_refdate-86400)) - _refdate=$(date -j -r $_refdate +%Y%m%d%H%M) + _refdate=$(epoch_to_fmt $_refdate %Y%m%d%H%M) echo "+$_refdate/10 echo test $TEST" > $CONF ./vdcron -s0 -f $CONF check_log "On .* execute: .* echo test \+HHMM/10" blob - 25cfb0384fd680745d296aa2ea3c17b9af8d120e blob + 03ccf637bb423bcd48deffe745fbdab374fcec98 --- vdcron +++ vdcron @@ -3,9 +3,11 @@ # Auteur : Vincent # licence : BSD -# Version : 0.5.3 +# Version : 0.6 # Description : execute schedulled commands without assuming that the machine runs permanently. -# Dependencies : This is based on the BSD's date command. +# This is developped on OpenBSD machine, but since version 0.6 runs on debian jessie too +# Dependencies : Since version 0.6, vdcron manage BSD's date and GNU's date. So, we should not have +# dependencies anymore. # Config file : ~/.vdcron/vdcron.conf or /etc/vdcron.conf # Log file : ~/.vdcron/vdcron.log or /var/log/vdcron.log @@ -13,12 +15,15 @@ DEBUG= SLEEP=10 TEMP=$(mktemp) FILE= -USAGE="$0 \n - options are:\n - -h : this help\n - -d : debug\n - -s : force an initial sleep different than the default: $SLEEP seconds\n +VERSION=0.6 +USAGE="$0 + options are: + -h : this help + -d : debug + -s : force an initial sleep different than the default: $SLEEP seconds -f : use a specific input file + + You are running $0 version $VERSION " PARAMS="hdf:s:" @@ -53,6 +58,40 @@ if [ ! -f "$FILE" ]; then exit 1 fi + +if date --version >/dev/null 2>&1 ; then + [ $DEBUG ] && echo Using GNU date + GNU_DATE=1 + BSD_DATE= +else + [ $DEBUG ] && echo Using BSD date + GNU_DATE= + BSD_DATE=1 +fi + +fmt_to_epoch(){ + local _date_string + if [ "$BSD_DATE" ]; then + echo $(date -j +%s $1) + else + if [ ${#1} -eq 12 ]; then + #Linux do not recognize YYYYmmddHHMM, but he recognize YYYYmmdd HHMM + _date_string="${1%????} ${1#????????}" + else + _date_string=$1 + fi + echo $(date -d "$_date_string" +%s) + fi +} + +epoch_to_fmt(){ + if [ "$BSD_DATE" ]; then + echo $(date -j -r $1 +$2) + else + echo $(date -d @$1 +$2) + fi +} + echo -n "Starting at ..." >> "$LOG" sleep $SLEEP echo " $(date)" >> "$LOG" @@ -150,7 +189,7 @@ while IFS="" read -r line; do _tmpdate=$_pastfiledate fi if [ "$_tmpdate" ]; then - _refdate=$(date -j +%s $_tmpdate) + _refdate=$(fmt_to_epoch $_tmpdate) else _refdate=$(date +%s) fi @@ -163,9 +202,9 @@ while IFS="" read -r line; do do _refdate=$((_refdate + repeat * _step)) done - [ "$DEBUG" ] && echo " _refdate final: $_refdate" - _newdate=$(date -j -r $_refdate +$_datefmt) - [ "$DEBUG" ] && echo " reschedule at:$(date -j -r $_refdate +%Y%m%d%H%M), will be $_newdate" + [ "$DEBUG" ] && echo " _refdate final: $_refdate, datefmt: $_datefmt" + _newdate=$(epoch_to_fmt $_refdate $_datefmt) + [ "$DEBUG" ] && echo " reschedule at:$(epoch_to_fmt $_refdate %Y%m%d%H%M), will be $_newdate" newline="$_firstchar$_newdate/$repeat $_nolog$cmd" if [ "$newline" = "$line" ]; then line=_$newline blob - 85dfed1e259d0e60b2145f22bd9d206aed523e28 blob + e34c305fa2a210dc0b156a2d9670c28c929e7591 --- vdcron.8 +++ vdcron.8 @@ -124,6 +124,8 @@ THe log file for non-root user .Nm has been created in June 2018 on .Ox 6.3 . +.Pp +Since version 0.6 is runs on debian Jessie too. So should run successfully on all linux machines. .Sh AUTHOR .Nm was written by blob - 272d699cd97e9f1b036a3559fc4a07d2a27f2a32 blob + e16405f350f2bfc12448ebe659cda50e6d715f4e --- vdcron.conf.8 +++ vdcron.conf.8 @@ -195,4 +195,6 @@ was written by .Nm has been developed on .Ox . +.Pp +Since version 0.6 is runs on debian Jessie too. So should run successfully on all linux machines.