Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to use .envrc within crontab job script on ubuntu 18

Tags:

bash

cron

ubuntu

We have a set of environment variables being exported from an .envrc file within a dir. In the shell you can run direnv allow and the .envrc file gets invoked, thus creating the env vars.

We would like to have this same functionality when running a script with a crontab job, but the envrc file never runs.

We have tried:

  • In crontab: * * * * . $HOME/.profile; some/script/to/run
  • In script: direnv allow
    • OR
  • In script: eval "$(direnv export bash)"

None of that works, is there a way to run the .envrc file from a script invoked by a cron job?

like image 980
Austin L Avatar asked Nov 06 '25 04:11

Austin L


1 Answers

You can use direnv exec to achieve this. It has the the arguments [DIR] COMMAND [...ARGS]. If the .envrc file is in the same folder as the script, you can change the following:

* * * * * sh /path/to/script/script.sh

To this:

* * * * * direnv exec sh /path/to/script/script.sh

If the .envrc file is in a different directory than the script, then you'll have to pass the path to the directory just after the exec argument like so:

* * * * * direnv exec /path/to/secrets sh /path/to/script/script.sh

source

like image 78
GammaGames Avatar answered Nov 09 '25 10:11

GammaGames



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!