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.
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
777
or 775
(rwx
). If not, then change the file permission using chmod 777 ~/jek.sh
#!/usr/local/env sh
)&&
or ;
so that both of them are run properly. Currently, the second script's name will be treated as a parameter for the first..
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