cron part II
As a follow up to yesterday’s post I want to talk about a unique feature of the cron
utility.
cron
jobs are run by a daemon, aka a background process. This means that any commands executed in a cron
script are not executed in the standard terminal shell. The env provided is much more stripped down.
A side effect of this is that it becomes tricky to get access to environmental variables while executing a cron task.
Let’s build on yesterday’s example to see what I mean.
docker run -it lombardo/ubuntu-sandbox bash
inside the container let’s look at our env:
printenv
# HOSTNAME=05394d0aa42e
# TERM=xterm
# LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01: .....
# PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
# PWD=/
# SHLVL=1
# HOME=/root
# _=/usr/bin/printenv
As expected. Now, let’s run that same command as a cron
task and compare the output.
touch /var/log/cron-env.log
vi etc/crontab
and insert the following task:
* * * * * root printenv > /var/log/cron-env.log 2>&1
Now start the utility
cron
and tail the logs
tail -f /var/log/cron-env.log
After 60 seconds the output of the printenv
command will appear. It should look much different than the output we saw from the bash prompt.
# HOME=/root
# LOGNAME=root
# PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# SHELL=/bin/sh
# PWD=/root
These are all specific variables set up by the cron utility. (or so says man 5 crontab
)
This utility is running processes which are not attached to the terminal shell controller by the user.
So let’s think…how can we get ENV vars from our normal terminal shell into a cron
task? What if we needed to run cron
in a Docker container, but needed to pass ENV vars in at runtime?
There’s a few ways I can think of. Perhaps I will write about it in the future.