I have a crontab running every hour. The user running it has environment variabless in the .bash_profile that work when the user runs the job from the terminal, however, obviously these don’t get picked up by crontab when it runs.
I’ve tried setting them in .profile and .bashrc but they still don’t seem to get picked up. Does anyone know where I can put environment vars that crontab can pick up?
Have ‘cron’ run a shell script that sets the environment before running the command.
Always.
The scripts in ~/bin/Cron are all links to a single script, ‘runcron’, which looks like:
(Written using an older coding standard – nowadays, I’d use a shebang ‘#!’ at the start.)
The ‘~/.cronfile’ is a variation on my profile for use by cron – rigorously non-interactive and no echoing for the sake of being noisy. You could arrange to execute the .profile and so on instead. (The REAL_HOME stuff is an artefact of my environment – you can pretend it is the same as $HOME.)
So, this code reads the appropriate environment and then executes the non-Cron version of the command from my home directory. So, for example, my ‘weekday’ command looks like:
The ‘daily’ command is simpler: