How to use variables in variables in crontab

I'm trying to use variables in variables in crontab:

AUTH=user:password
BASE_URL=http://...
COMMAND_OLDB=curl -u $AUTH $BASE_URL/openligadb >> /var/log/cron.openligadb.log

But just getting a mail in /var/mail/root:

X-Cron-Env: <BASE_URL=http://...>
X-Cron-Env: <AUTH=user:password>
X-Cron-Env: <COMMAND_OLDB=curl -u $AUTH $BASE_URL/openligadb >> /var/log/cron.openligadb.log>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/root>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=root>
Message-Id: <...>
Date: Mon,  9 Feb 2015 20:10:01 +0100 (CET)

Enter host password for user '$AUTH':
curl: (6) Couldn't resolve host '$BASE_URL'
curl: (6) Couldn't resolve host '>>'
curl: (3) <url> malformed

/var/log/cron.openligadb.log itself is empty. $AUTH as well as $BASE_URL will not be resolved in $COMMAND_OLDB. Why and how to fix?


Solution 1:

According to crontab man page (man 5 crontab)

  • if you set variables, those will be environment variables (which is fine as long as you don't overwrite other environment variables)
  • the value of such a variable is NOT parsed, substitutions will NOT work

So you can define AUTH, BASE_URL but you cannot define COMMAND_OLDB which is based on other variables, because those will not be substituted. Just put the command in the crontab line, like this (of course use your time interval)

*/10 * * * * curl -u $AUTH $BASE_URL/openligadb >> /var/log/cron.openligadb.log

but NOT like this

*/10 * * * $COMMAND_OLDB