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:
* * * * . $HOME/.profile; some/script/to/rundirenv allow
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?
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With