Question

I have a cron job set to execute a shell script telling the server to run a single ruby command (jekyll build --source /path/to/source/dir --destination /path/to/dest/dir) 2 minutes past every hour. The script executes just fine when I run it via the terminal, but cron doesn't seem to fire it. I know cron's environment isn't the same as the user's and I've set its source path to my .bash_profile, where the user ruby environment is defined, as per advice elsewhere. I'm rather at a loss now.

The crontab entry looks like this:

2 * * * * . $HOME/.bash_profile ~/jek.sh

FWIW, the relevant section of .bash_profile, set up automatically when RVM was installed (on shared hosting, with somewhat outdated ruby no less, getting Jekyll up and running without being able to write to the server's own ruby directory means using RVM; this seems to work absolutely fine), is:

PATH=$PATH:$HOME/bin

export PATH

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

Am I calling my user profile for use in the cron environment wrongly? Am I missing something glaringly obvious in the syntax? Any help would be much appreciated.

Was it helpful?

Solution 2

In your question, the cron you are running goes like

2 * * * * . $HOME/.bash_profile ~/jek.sh

There are multiple things you need to correct/verify with this entry

  1. Check that file permissions for both is 777 or 775 (rwx). If not, then change the file permission using chmod 777 ~/jek.sh
  2. Check that each of them define in their shebang line which language script they are (#!/usr/local/env sh)
  3. Separate the two scripts by an && or ; so that both of them are run properly. Currently, the second script's name will be treated as a parameter for the first.
  4. There is a . after the 2 * * * * part. I am not sure why you added it - it has to be removed.


In case @psny's answer doesn't work for you, try exporting your path variable in your cron entry. After that, the whole thing should work properly. Steps

1) Find the value of $PATH

echo $PATH #Lets call the string :some/path/:another/path

2) Manually set the path in your crontab entry

2 * * * * export PATH=:some/path/:another/path && /bin/bash /home/username/jek.sh

OTHER TIPS

What do you think $HOME will evaluate to in cron's environment?

Since you are using rvm. This is the correct way to run cron jobs

You may need to use absolute paths because cron can be very picky.

Try this after 'crontab -e':

2 * * * * /bin/bash /home/username/jek.sh

If you want to string commands together, use && between each:

2 * * * * /bin/bash /home/user_name/.bash_profile && /bin/bash /home/username/jek.sh

As a note, besides /bin/ you can find many commands in /usr/bin. To find the absolute location of a command, use 'which'. For example, :

user@computer:~$ which bash
 /bin/bash

Finally, you can probably use cron to run the ruby script directly, with an absolute path to jekyll, and probably after && in order to set up the environment first.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top