1

I use cron for years now. In order to load all my environment variables at once, I define them in a dedicated file (often but not always .bashrc) that I source in the crontab:

* * * * * (. /home/me/my_environment_variables.sh; my_script.sh)

Unfortunately, this trick fails to work with the debian wheezy server I've been recently asked to move to. More specifically, all happens as if the source command was ignored: my_script.sh is executed but with empty environment variable so that the results of the script are not the expected ones. I really don't understand the issue. Everything was perfectly working on my ubuntu

The installed cron package is "3.0pl1-124".

Have you any idea how to solve or to bypass this issue?

PS : Defining environmental variables in this way seems to work :

* * * * * (export OPTOS_HOME=/home/me/src/optos; my_script.sh)

However, defining more than one environment variables in this way is not convenient.

closed as unclear what you're asking by Gilles, slm Sep 23 '14 at 23:40

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • In what way does this fail to work? What errors do you get (check your local email)? – Gilles Sep 23 '14 at 22:58
  • It fails to work in the way the environment variables are not defined . – S Legrand Sep 25 '14 at 6:27
  • I don't get any error message, neither by email nor in the /etc/var/syslog. All happens as if the source command was ignored: my_script.sh is executed but with empty environment variable so that the results of the script are not the expected ones. I really don't understand the issue. Everything was working perfectly on my ubuntu. – S Legrand Sep 25 '14 at 6:39
  • Put set -x at the beginning of the command line, so that the shell will print each command as it is executed. You'll get a transcript in local email, post that. Is /bin/sh bash or dash? Do you have a line SHELL=… in the crontab file? Post the content of my_environment_variables.sh. – Gilles Sep 25 '14 at 7:21
1

. is an internal shell command. Check what shell is used by cron (by default it is /bin/sh).

Alternative solution is to create a wrapper script and put these commands inside. It will work for sure.

  • Not true. man 5 crontab: "The ``sixth'' field (the rest of the line) specifies the command to be run. The entire command portion of the line, up to a newline or % character, will be executed by /bin/sh or by the shell specified in the SHELL variable of the cronfile." – Patrick Sep 22 '14 at 14:05
  • Thanks, this helped me to trace back some mistakes. To solve my problem, I've defined SHELL=/bin/bash and made some minor changes in my script. I've also replace '.' by the 'source' in the crontab, but I'm not sure this was necessary. – S Legrand Sep 25 '14 at 7:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.