Start Jekyll via cron

Thanks, again, Michael.

sudo -u ubuntu echo $PATH
/home/ubuntu/gems/gems:/home/ubuntu/gems/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Here’s the complete use case:

  1. I’ve got Jekyll+Jekyll-Admin working for multiple sites on the same server, using a setup tweaked from that other thread, which dynamically determines available ports, and updates new site _config.yml files to use the ports. (The server name is still hard-coded into the Jekyll file, as on the other thread.)

  2. I’m also running Apache2+PHP, pointing to my main “template” directory, and I’ve built a process with a PHP form that defines things like personalization and “clones” the working directory to a new, domain-themed directory. Each of those will be used, with the CMS, to customize site content, then I will deploy them (’_site’) to the public-facing server(s).

  3. Part of the cloning process creates the virtual host file to put in nginx’s sites-available directory (and ln -s in the sites-enabled directory.) I’m using a root cronjob to copy those files and restart nginx because PHP (www-data user) doesn’t have the permissions to do it.

That script works great from both the command line (sudo php script.php) and from the cronjob.

  1. Once nginx has been restarted with the new site’s config included, I need to start Jekyll/Jekyll-Admin in the new site’s working directory in order to use the CMS.

That script works great from the command line (php start_jekyll.php), but since I need to run it after nginx has been updated and restarted, I daisy-chained the user-script into the cronjob, as above (root_script.php && sudo -u ubuntu user_script.php …)

  1. The user script uses PHP’s chdir() to cd into the new site directory, and then runs ‘jekyll serve’, as noted. Also as noted, at the top of the user script I inserted exec('whoami > /tmp/whoami') to check that, indeed, the script was executing under the ubuntu user, and it is confirmed.

The idea is to wrap all of these processes up into a single workflow, so I don’t need to manually run anything, other than to complete the PHP form and submit it, whci will create a new site that is built on Jekyll and uses the Jekyll-Admin CMS.

Everything is working great … except for starting Jekyll after the nginx restart, due to the Ruby error.

The cronjob actually runs every 2 minutes … but that seemed like a distraction for debugging purposes. I set it like that to check for new sites to set up, so I could set up new sites whenever I wanted to, and almost immediately be able to use the CMS.

The command I’m using, inside the new site top-level directory as the user who owns that directory (required because the gems are in that user’s home directory) is /home/ubuntu/gems/gems/jekyll-4.0.0/exe/jekyll serve --verbose --trace. I need to use the full path because without it, Jekyll is not found. With it, Jekyll starts, but then throws that Ruby error.