Jekyll build via Cron

Hey guys,
I’ve looked through a lot of topics but I still can’t get my thing to work.
The premise:
I’ve built a kind of cms for our editors to create Jekyll posts, which works just fine. The last step is uploading the post and publishing it via “jekyll build”.
My problem(s):
At first the idea was that a php script executes the jekyll build command on the server, which didn’t work. Then I had the idea of a cron that executes the command as a certain user, which has all the necessary privileges, but it still won’t work.

My code:
First idea .sh script:
jekyll build
wait
The scipt is already in the correct path. To test it’s functionality I let it create a new folder in a different location and it worked just fine. Also when I execute it via “bash script.sh” it works fine, but when I added it to a cron, only the folder was created but the Jekyll page was untouched.

I would really appreciate your help.
Also if anyone is interested in my front end editor, just hmu.

Best regards,

Sebastian

are you cd’ing into the source directory before running the script?

we did something like that in the past, maybe it’s useful to you: https://0xacab.org/sutty/sutty/-/blob/896f71161ae0c46b8713b94f8445dc4fac697b63/bin/jekyll_build_all

(when a user wanted to build the site, a .generate file would be touched, then the script would be run by cron every 3 minutes and build any site that had this file in it)

Which source directory do you mean?
I am really not an expert in Bash scripts or Ruby and just tried what was logical to me.

your site files are in a directory, this is called source directory by jekyll. if your script works on what you say is the correct path but not on cron, it probably means the script needs to be run from that specific directory too (as a simple jekyll build would do).

so you either cd into this directory from the cronjob definition so the script is run on the correct path or make the script change to this directory before running the build commands, for instance:

# every minute
* * * * * sh -c 'cd /to/correct/path && jekyll build'

or

#!/bin/bash

cd /to/correct/path
jekyll build
2 Likes

Oh yeah, sorry.
Yeah I tried that and now tried again, just for my sanity, but it still doesn’t work, which really puzzles me.
There is nothing inherently wrong with my installation or anything. It’s just that the jekyll build command does not work, either through PHP or a Bash script or Cron.

Start from the simplest working state and add to it gradually until to see it no longer works and you isolate your bug

Can you get a simplified script to work? Use cd and jekyll build from the bash file just above and make that your script. Add an echo in at the top if you want.

Make it executable.

Execute directly and confirm it works.

Then add the path to that script to your cron.

* * * * * ~/script.sh

Then you can check if cron produces the mail using echo and other output and also check if the build works.

Then add complexity with additional bash lines or PHP.

Hey Michael,
my simplest script was

cd /path/to/jekyll jekyll build echo "done"

It worked just fine, when I executed it in the command line. Then I added it to a cron and piped the output to a log, since my server isn’t allowed to send emails. The done appeared in the logs but the site was not built. Then I’ve tried a wait after the jekyll build but it did not work either.
I really don’t need it to do any other thing, than build my page.

Make it’s a PATH issue. Check your user’s path and set this part of it in crontab.

I added something below to send stdout and stderr to the same log file in case Jekyll is complaining silently with stderr getting lost.

PATH=/usr/bin:$HOME/.gems:...

* * * * * cd ... && jekyll build > log.txt 2>&1
1 Like

Maybe SHELL=/bin/bash
As default is sh
Make crontab consistent with your user

Ok, so the stdout and stderr helped a lot.
The error I’m getting is /bin/sh: 1: jekyll: not found.
So I guess I need to link it to Ruby or something?
When I added the SHELL=/bin/bash, the error is the same.

Could you maybe quickly explain th PATH= variable you set up, so I can adapt and try it, because I don’t think it works out of the box with my setup.

which ruby finds where the interpreter is and you can add that directory to the PATH like others suggested :slight_smile:

Thanks for your suggestions guys.
I now have

PATH=/usr/bin/ruby
SHELL=/bin/bash

In my Log I still get the error /bin/bash: jekyll build: Befehl nicht gefunden (command not found).

Do I have to link it to the Jekyll Gem itself?

Jekyll is a gem so you’ll still need to make sure anything that your user normally has in PATH like ruby 2.x.x bin below is there.

I use this in bashrc. But crontab doesn’t know about this so I’d have so set this in my crontab PATH too

export GEM_PATH="$HOME/.gem/ruby/2.6.0/bin"
export PATH="$GEM_PATH:$PATH"

Regards,
Michael

Dear Sebastian,

I think you are on the wrong track. I would just commit back to the repo using the ‘git push’ command on ‘save’ (or ‘publish’, whatever you prefer). Then use any CI/CD tool to build your website. Netlify is really good at it. If you host on Github Pages you do not even need to build anything at all.

Kind regards,
Joost

Good Morning Joost,

I am not using git in any way, shape or form. My page is hosted on my own webserver, thats why I can’t use any git commands.
Still thanks for your reply.

Best regards,

Sebastian

Hi Sebastian,

Probably stating the obvious: You can install git on your server.

An SSG on your own server without CDN and git does not make sense at all. You will get all disadvantages of a monolith combined with all disadvantages of an SSG.

Have you considered hosting the CMS on your server and hosting the websites on a CDN (splitting hosting and CMS)? It would make much more sense.

Kind regards,
Joost

So after a long trial and error, I still couldn’t get it working. I got to the point, where I got a different error message, but this was too exhausting and time consuming to get fixed.

My solution now is a simple .bat file on my PC, which connects via plink to the server and executes the commands. It works just fine and is simple enough for my users to use.

I know Git could have worked too, but this doesn’t fit in my use case, so it is not an option. I hope you understand this.