Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

Scheduling my blog updates

So because I don't actually write these posts at the date they appear on the site (or are listed as when they appeared on the site), I need to schedule running my build and deploy scripts at those times.

Nikola has a feature for THE_FUTURE_IS_NOW, but I do want to wait until the appointed time before content is visible.

So my plan is to use a custom script to check for files whose scheduled publication time is in the same quarter-hour as this invocation of the cron job.

So in cron:

14,29,44,59  *  *  *  *  blog sh /var/server1/shares/public/Support/Programs/nikola/scripts/follow-schedule.sh 1>>/var/server1/shares/public/Support/Systems/server1/var/log/nikola_schedule/log 2>&1

Notice how in this cron entry I actually rely on cron to send the output to log files. This is a departure from the norm.

And file follow-schedule.sh:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh
# Startdate: 2021-09-01 21:38
# Purpose: find nikola posts that should be released now and then build and deploy
# Usage:
#    ./follow-schedule.sh "2021-09-03 09:16"
# Design assumption:
#    expecting only one post per quarter-hour!
#    due to ssh possibility, cannot rely on DEBUG, VERBOSE, APPLY, DRYRUN environment variables. Just always be verbose.
# Dependencies:
#    plecho from bgscripts-core
# Documentation:
#    /mnt/public/Support/Programs/nikola/blog-README.md
# flow: find anything scheduled within this 15-minute block
if ! test "$( hostname -s )" = "server1" ;
then
   echo "Connecting to server1..."
   ssh server1 /mnt/public/Support/Programs/nikola/scripts/follow-schedule.sh
else

if ! test "${USER}" = "blog" ;
then
   echo "Switching to user blog..."
   sudo su blog -c '/mnt/public/Support/Programs/nikola/scripts/follow-schedule.sh'
else

echo "START nikola scheduled job" | plecho
INDIR=/mnt/public/Support/Programs/nikola/kb2
test -n "${1}" && DATE_AND_HOUR="${1}"
test -z "${DATE_AND_HOUR}" && DATE_AND_HOUR="$( date "+%F %H" )"
test -n "${2}" && MINUTE="${2}"
test -z "${MINUTE}" && MINUTE="$( date "+%M" )"
export DATE_AND_HOUR
export THISDATE="$( echo "${DATE_AND_HOUR}" | awk '{print $1}' )"
results="$( grep --include '*.md' -riIE '^\.\. date: [0-9]{4}(-[0-9]{2}){2}' "${INDIR}" 2>/dev/null | grep "${DATE_AND_HOUR}" 2>/dev/null )"
NOW_QUARTER="$( printf '%s\n' "scale=0;${MINUTE}/15" | bc )"
echo "Evaluating date+hour ${DATE_AND_HOUR} with quarter-hour ${NOW_QUARTER}"
if test -n "${results}" ;
then
   POST_QUARTER="$( printf "%s\n" "scale=0;$( echo "${results}" | awk '{print $NF}' | awk -F':' '{print $2}' )/15" | bc )"
   if test "${POST_QUARTER}" = "${NOW_QUARTER}" ;
   then
      tf="$( echo "${results}" | sed -r -e 's/:\.\..*$//;' )"
      scheduled_time="$( echo "${results}" | awk '{print $NF}' )"
      echo "Found a post for this quarter-hour: ${tf} at ${scheduled_time}"
      # determine if needs to be moved to YYYY/MM
      YYYY_MM_DD="$( echo "${THISDATE}" | awk -F'-' 'BEGIN{OFS="/"} {print $1,$2,$3}' )"
      if ! echo "${tf}" | grep -qiE "${YYYY_MM_DD}" ;
      then
         echo "mv \"${tf}\" \"${INDIR}/posts/${YYYY_MM_DD}\""
         mkdir -p "${INDIR}/posts/${YYYY_MM_DD}"
         mv "${tf}" "${INDIR}/posts/${YYYY_MM_DD}/"
      else
         echo "File \"${tf}\" does not need to be moved, but it is scheduled for now."
      fi
      # and now run build and deploy
      echo "Now will build and deploy."
      /mnt/public/Support/Programs/nikola/scripts/build.sh && \
      /mnt/public/Support/Programs/nikola/scripts/deploy.sh
   else # this quarter-hour
      echo "No files found for this quarter-hour."
   fi # end-if this quarter-hour
fi # end-if any 
echo "STOP nikola scheduled job" | plecho

# end-if-not-user-blog
fi

# end-if-not-server1
fi

Comments