A launchd post scheduling system for Jekyll

[Tweet : nvALT]

My previous post scheduling system revolved around the Unix at command to schedule one-off future tasks. In the most recent version of OS X (10.10.4), that atrun command is utterly broken. Running at, atq, or atrm will give you:

atq: cannot regain privs: Operation not permitted

I couldn’t find any way to fix it, so I revamped the system. It now uses a lightweight launchd task that polls for dates listed in a simple text file. This is a simple solution that works well because I can easily edit the file remotely and modify it with other scripts.

The code that follows uses my notifyutil-based system to trigger the rake generate_deploy command that builds and uploads my static blog. You can also experiment with just running that command directly from a script, but because Jekyll has specific dependencies and most people use bundler/rvm/rbenv with it, the fact that launchd doesn’t load your shell environment may be problematic. The idea remains the same, though.

First, the script that the launchd task uses to check for new date triggers. It just reads in a file called “scheduled_deploys.txt” in the root of your Jekyll directory, parses each line, and if a line contains a date that’s before the time the task runs, it deletes the line and runs the build command.

require 'time'

sched_file = File.expand_path('[[YOUR JEKYLL DIR]]/scheduled_deploys.txt')
if File.exists?(sched_file)
  deploys = IO.read(sched_file).strip.split(/\n/)
  deploys.delete_if {|d|
    if Time.parse(d) < Time.now
      system "/usr/bin/notifyutil -p jekyll.gendep"
  File.open(sched_file,'w') {|f| f.puts deploys.join("\n")}

And here’s the launchd job that polls every 5 minutes. To load it, edit the script path in the file and save it to ~/Library/LaunchAgents/rb.jekyllpoller.plist (or whatever name you like), then run sudo launchctl load ~/Library/LaunchAgents/rb.jekyllpoller.plist.

Polling launchd job
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
        <string>/Users/[[USER]]/[[SCRIPT PATH]]/check_jekyll_deploys.rb</string>