variables in crontab

To keep my crontab clean, I would just call a shell script and do the fun stuff in the script.


In Vixie cron, which is possibly the most common, you can do this almost exactly like a shell script.

VARIABLE=value
PATH=/bin:/path/to/doathing
0 0 * * * doathing.sh $VARIABLE

The man page says:

An active line in a crontab will be either an environment setting or a cron command. An environment setting is of the form,

     name = value

where the spaces around the equal-sign (=) are optional, and any subsequent non-leading spaces in value will be part of the value assigned to name. The value string may be placed in quotes (single or double, but matching) to preserve leading or trailing blanks. The name string may also be placed in quote (single or double, but matching) to preserve leading, trailing or inner blanks.

You can tell if you have Vixie cron by checking the man page for crontab; the author will be Paul Vixie. Different crons may or may not support this (BusyBox's cron for example, does not), in which case your best option is to wrap your command in a shell script and run that script from cron instead. In fact, this is a good thing to do for anything complicated.


Just a working example of using variables in the crontab file and their substitution in the strings:

CURRENT_TIME=date +%Y.%m.%d_%H:%M:%S.%3N
CURRENT_DATE=date +%Y_%m_%d

SIMPLE_VAR=the_simple_var
LOG_DIR=/var/log/cron

* * * * * /bin/echo "simple variable test! ${SIMPLE_VAR}__test!" >> "${LOG_DIR}/test.log"
* * * * * /bin/echo "complex variable test! $(${CURRENT_TIME})__test!" >> "${LOG_DIR}/test.log"

Tested on this Docker image (paste the above crontab to the crontab.txt):

FROM debian:10-slim

# Install docker (Yep, this is a docker in docker):
RUN curl -fsSL https://get.docker.com -o get-docker.sh && sh get-docker.sh

# Install CRON:
RUN apt-get update && apt-get install -y --no-install-recommends cron

# Add a crontab_config.txt task:
COPY crontab.txt /var/crontab.txt
RUN crontab /var/crontab.txt

ENTRYPOINT ["cron", "-f"]

Add this to the crontab to run any commands inside another docker containers:

/usr/bin/docker exec container_name ls -la

If you have a few environment variables you want to set for a particular job, just inline those into the sh snippet.

42 17 * * * myvariable='value' path/to/command

In case it's not obvious, the sh syntax var=value command sets var to value for the duration of command. You can have several of these if you need more than one.

42 17 * * * firstname='Fred` lastname='Muggs' path/to/command

If you have nontrivial variables you want to access from several places, probably put them in a separate file, and source (.) that file from your shell startup script and your cron jobs.

Let's say you have a file $HOME/bin/myenv with settings like

myvar=$(output of some complex command)
another='another
complex
variable'

then you can add

. $HOME/bin/myenv

to your .profile (or .zshrc or .bash_profile or what have you; but .profileis portable, and also used bysh`) and

42 17 * * * . $HOME/bin/myenv; path/to/command

in your crontab.

Notice the lone dot before the space before the file name; this is the dot command (also known as source in e.g. Bash) which reads the file into the current shell instance as if you had typed in the things in the file here.

Tangentially, the $HOME/ part is strictly speaking redundant; cron jobs will always run in the invoking user's home directory.

Obviously, if you want a variable to be true in your entire crontab, set it at the top, before the scheduled jobs.

Tags:

Cron