Ugur
Ugur

Reputation: 2044

Concatenating variables in crontab won't work

I have a very long (and messy) crontab (on my Mac). So I started cleaning it up by introducing variables for instance. Then I realized that concatenating variables (just like in a Bash script) does not work in my crontab. This is what I have:

SHELL=/bin/bash
HOME=/Users/leuchtturm
# [1] Previously had this, but this does not seem to work
# $HOME is not being evaluated? Why?
# 
# VIRTUALENV_PYTHON=$HOME/.virtualenvs/py361/bin/python
# Now I have this (elaborated)
VIRTUALENV_PYTHON=/Users/leuchtturm/.virtualenvs/py361/bin/python

# the crontab entry
# Here $HOME is being expanded, but not in the example above [1]
#
*/2  *  *  *  *   source $HOME/.config_vars && $VIRTUALENV_PYTHON $HOME/workspace/monitoring/check_server.py

So in this line VIRTUALENV_PYTHON=$HOME/.virtualenvs/py361/bin/python the variable $HOME is not being evaluated. My cron log has an entry that says "path not found".

Can someone enlighten me? Thanks!

Upvotes: 3

Views: 1089

Answers (1)

Javier Elices
Javier Elices

Reputation: 2154

Within crontab files, lines that look like VAR=value are definitions recognized by cron and not executed under a shell. So the value of a variable definition is not expanded in any way.

On the other hand, the command in a crontab entry, gets executed by the default crontab shell or, in your case, by the shell defined with the SHELLvariable.

Beware also that particular cron versions may not let you define arbitrary variables, but only particular ones like SHELL, HOME, MAILTO, etc. See Variables in crontab?.

Upvotes: 4

Related Questions