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.
inside the container let’s look at our env:
As expected. Now, let’s run that same command as a cron
task and compare the output.
vi etc/crontab
and insert the following task:
Now start the utility
and tail the logs
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.
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.