Here’s a quick tutorial on how to get your jekyll blog to post at certain time intervals.
- This article assumes you already have the basic knowledge and ability to modify the server that is running your jekyll site.
- This article assumes you are used git to store revision history of your website. If you are looking for a good place to host your repos, take a look at Bitbucket. They provided unlimited repos for up to 5 contributers. Pretty snazy.
- Assumes you are using a Rakefile to deploy your site with rsync. I do include an example of my rakefile below if you are unfamiliar.
- Cron is installed on your server
- ruby (I am used 1.9.3) is installed on your server (I use RVM to handle all my different versions of ruby)
- jekyll (I am using the latest 1.1.2) is installed on your server
###1 - Set up your config file.
Open your _config.xml and add future: false. This will prevent jekyll from posting any posts that have future dates on them in your _posts directory.
Also add “publish.*" to your exclude variable. Here is an example of my exclude varable:
exclude: [ ".rvmrc", ".rbenv-version", "README.md", "Rakefile", "Gemfile", "Gemfile.lock", "Vagrantfile", "provisioning", "publish.*" ]
This will prevent jekyll from publishing your utility files.
###2 - Add a Rakefile.
This portion assumes you are using a Rakefile to deploy your site. If not read on anyway (you may find it useful).
If you don’t already have one, create a file in your jekyll root directory and call it Rakefile. Rake files are much like Makefiles where they are essentially scripts with several sub commands to choose from. Take a look at the Rakefile below.
require "rubygems" require 'rake' require 'yaml' require 'time' require 'open-uri' desc 'Build site with Jekyll' task :build do sh 'jekyll build --trace' end desc "Pings search engines with sitemap" task :ping do http = open("https://www.bing.com/webmaster/ping.aspx?siteMap=https://www.yoururl.com/sitemap.xml") http = open("https://www.google.com/webmasters/sitemaps/ping?sitemap=https://yoururl.com/sitemap.xml") end desc "Launch preview environment" task :preview do system "jekyll serve -w" end desc 'Deploy' task :deploy => [:build, :ping] do sh 'rsync -rtzhq --progress --delete _site/ email@example.com:/path/to/www/' end desc 'Upload Images' task :upload_images => [:build] do sh 'rsync -rtzhq --progress --delete _site/img/ firstname.lastname@example.org:/path/to/www/img/' end
In this case some tasks include build, ping, preview, deploy, upload_images. Some of these tasks are dependant on other ones. Thus, some tasks look like this:
:deploy => [:build, :ping]
The task that we’re most concerned with at this point is deploy.
###3 - Create the publish script
Create a file in your site directory and call it publish.sh.
#!/bin/sh export LANG=en_US.UTF-8 export LANGUAGE=en_US.UTF-8 export LC_ALL=en_US.UTF-8 cd /path/to/jekyll/site/ /path/to/git/git pull source /Users/jaredwolff/.rvm/environments/ruby-1.9.3-p429 rake deploy
The language exports help tell the shell that encoding to use. If you don’t use these you will get some errors when you attempt to build your jekyll project.
Change directories to your repo location on the server. (If you haven’t already clone your repo to an accessible location.)
Tip: Make sure you are using the full paths in this file. (or else the shell script will exit with errors. you can also export the path to git etc if that tickles your fancy as well.)
Tip: Make sure you also indicate the full path to your git install.
This script will pull to get the latest goodies from the remote server (bitbucket).
Using the source command will import the ruby 1.9.3 environment. This ensures there is nothing missing when we run rake in our next step.
Rake deploy will deploy your site! It will run the task as shown earlier in the example Rakefile I posted. ###4 - Create a crontab file
Make a new file in the root of your site and call it jekyll.crontab
Now open it and copy and past these contents:
# min hour mday month wday command 15 10 * * * /bin/sh /path/to/jekyll/site/publish.sh
This cron script will publish your site at 10:15 am every day. If you need to debug at all you can add: "> /path/to/jekyll/site/cron.log 2>&1” after publish.sh. This will place a log in your site directory.
###5 - Add crontab to cron
At this point your configuration should be ready to go!
All you need to do is add your crontab file to cron:
You can see if your job is set in cron by using the crontab -l command.
That’s it! Your server will now come around at 10:15 am, pull your site, and publish your latest post(s). Good luck!
Last Modified: 2020.3.7