2

I want to define environment variables in one place which will be accessible through different user's crontabs, and from bash shell running.

How can I write it once in one place to be accessible to all usages mentioned above?

Until now I'm writing to system wide bashrc in /etc/bashrc , and in crontabs I write this again above crontab definitions

  • 1
    AFAIK those are not linked by design and duplicating certain settings is the only solution – HBruijn Sep 14 '16 at 11:58
  • @HBruijn: I didn't understand, do you mean that duplicating settings such as I did is the only solution? – user370717 Sep 14 '16 at 13:44
  • As far as I understand, yes. – HBruijn Sep 14 '16 at 13:49
3

I don't think you can do this in the crontab directly. The crontab file isn't a shell script so trying to source a script containing envvars into it doesn't work

. /path/to/envvars

crontab won't install this as it's not valid

crontab: installing new crontab
"/tmp/crontab.iZqWJX/crontab":1: bad minute
errors in crontab file, can't install.

What you could try is sourcing a script containing your envvars immediately before you run our job

* * * * * . /path/to/envars ; /path/to/your/job

I tested this with

* * * * * . /home/iain/envvars ; echo $HELLO >/tmp/test.out

with the envvars file containing

HELLO=fred

the file /tmp/test.out contained the following

fred

So I guess that worked. The source command (a synonym for . ) is a bash builtin but source is not posix so using . is preferred.

help source
source: source filename [arguments]
    Execute commands from a file in the current shell.

    Read and execute commands from FILENAME in the current shell.  The
    entries in $PATH are used to find the directory containing FILENAME.
    If any ARGUMENTS are supplied, they become the positional parameters
    when FILENAME is executed.

Exit Status:
Returns the status of the last command executed in FILENAME; fails if
FILENAME cannot be read

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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