6

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?

0

2 Answers 2

6

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
1

crontab has 2 types of statements (see man 5 crontab):

  1. environment setting (e.g. SHELL=/bin/bash)
  2. cron command (e.g. * * * * * /path/to/cmd )

The environment setting lines will not do variable substitution (e.g. PATH=/usr/bin:$PATH), but the cron command lines will. So you could just put the variable substitution line in the cron command instead of environment setting.

AUTH=user:password
BASE_URL=http://...

* * * * * COMMAND_OLDB="curl -u $AUTH $BASE_URL/openligadb" ; $COMMAND_OLDB >> /var/log/cron.openligadb.log

Note: This answer is more for educational purposes to demonstrate how it could be done. Dan's answer is the more practical answer.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .