19

Can I say:

MYPATH=/root/scripts  
MYSCRIPT=doit.sh

0 1 * * * $MYPATH/$MYSCRIPT

in crontab -e?

Is it possible to use variables in crontab -e?

1

4 Answers 4

23

Yes, you can define and use variables in this way. There's a limitation (which you haven't hit in your examples): the string on the right of the = sign is interpreted literally, with leading spaces removed, so you can't use constructs like FOO=$BAR/qux (e.g. PATH=$HOME/bin:$PATH won't do anything useful).

This is stated in the documentation, which you can see by running

man 5 crontab

(Note that man crontab shows the documentation of the crontab command, in section 1 of the manual; you want the documentation of the crontab file format, in section 5.)

10

Just made a try, yes it's possible. You can figure it out with this simple example, put this in your crontab:

FOO=qwerty
* * * * * echo $FOO > ~/out

And check the file ~/out (updated every minute), it should contain "qwerty".

2
  • 3
    +1 to showing how the OP could figure out similar things on their own.
    – Arthaey
    Feb 9, 2012 at 22:02
  • A better example will make use of $FOO inside the target script.
    – Asclepius
    Apr 7, 2014 at 23:27
8

While Linux crontabs support defining some variables that hold literal values, it isn't much use besides condensing long text to a shorter representation or controlling some things about cron itself, such as where to send email output.

The flexibility is understanding that the command part of the cron entry will be passed to /bin/sh -c or the shell defined with SHELL on systems using Vixie cron (the one usually installed on Linux systems). What this means is the remainder of the command line is a simple shell script. NOTE: realize what shell is being used. On Linux /bin/sh is normally /bin/bash so the $( ... ) embedded command works, but it would not on older systems where /bin/sh only understands ` instead.

For example, I have a simple crontab line that archives an MBX file of saved messages monthly and compresses it. It looks like this:

15 0 1 * *  nf=MailFeed-$( date +\%Y\%m ).mbx && cd Logs && mv MailFeed.mbx $nf && bzip2 -9 $nf

This will run the first of each month at 12:15 AM, set a new file name with CCYYMM in it, move current file to new name and compress it. The thing to remember is an unescaped % (percent sign) will be treated as a newline and data following it will be sent as stdin to command preceding the percent sign. That is why the normal date +Y+m is written as date +\%Y\%m above.

0
0

Yes, it is very much possible to use environment variables in cron:

You can set the /bin/bash shell right inside crontab in order to solve the conundrum:

(I am assuming you are dealing with your user crontab.)

$ crontab -e

Add this line at the top:

SHELL=/bin/bash

As for the command, you can now run a script or command in bash interactive mode with (example for your case -- please note the -ic switches, i is for "interactive" which allows to read your profile env vars):

* * * * * bash -ic "(echo \"USING TOKEN $GITTOKEN\" >> /var/log/custom-cron.log 2>&1)"

Save with Ctrl+O. Exit with Ctrl+X.

In case you have a bash script that uses your user environment variables, the cron line becomes something like this (my example runs every 15 minutes):

*/15 9-15 * * 1-5 bash -ic "($HOME/.config/conky/scripts/stocks >> $HOME/.local/log/stocks.log 2>&1)" &

You must log in to answer this question.

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