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

user370717
  • 151
  • 1
  • 1
  • 4

1 Answers1

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
user9517
  • 115,471
  • 20
  • 215
  • 297