DEV Community

Cover image for 📟 Updates to my autosched script to schedule posts with Hugo
Ryan Collins
Ryan Collins

Posted on • Originally published at on

📟 Updates to my autosched script to schedule posts with Hugo

A couple of weeks ago I posted how I schedule posts with Hugo. At the time, I had two scripts. The first one automatically selected the next day to schedule, while the other script let you pick the day. I’ve now combined the two. Here you go, the finished autosched:


# Set these to where you want the posts to go and where you
# want to archive the posts
postsdir="${HOME}/Web/$(date +"%Y")"
archive="${HOME}/notes/Archive/$(date +"%Y")"

RANDOM=$(date +%s%N | cut -b10-19)
h=$(( $RANDOM % 8 + 8 ))
m=$(( $RANDOM % 59 + 0 ))

# Only one argument was passed, the script assumes it's a filename
if ["$#" -eq 1]; then
    d=$(find ${postsdir} | xargs -I {} basename "{}" | sort -r | head -1)
    day=$(date -d "${d:0:10} + 1 day" +"%Y-%m-%d")
# Two arguments - first is the date in YYYY-MM-DD format, the second
# is the filename
elif ["$#" -eq 2]; then
    echo "Incorrect number of arguments"

if [[${filename:0:1} == "2"]]; then

schedule="${day}T$(printf "%02d" ${h}):$(printf "%02d" ${m}):00-04:00"

#echo "$postsdir $outfilename $schedule" 
# Put the file in the right place
cp "${filename}" "${postsdir}/${outfilename}"

# Add new schedule date and time
sed -i "s/^Date: .*$/date: ${schedule}/" "${postsdir}/${outfilename}"

# Remove Status
sed -i "/^Status: .*$/d" "${postsdir}/${outfilename}"

# Archive post
# My drafts are in a git repo.
git add "${filename}"
git mv "${filename}" "${archive}/${outfilename}"

echo "${filename} scheduled for ${day}!"

Enter fullscreen mode Exit fullscreen mode

Top comments (0)