Home > Net >  Read .env variables in crontab
Read .env variables in crontab

Time:04-21

I have a crontab and .env file. And I want to reach .env variables from crontab. Is it possible?

.env

variable1=value1
variable2=value2

crontab

2 11 * * * curl -Ssi -X POST -u 'value1:value2' https://example.com...

CodePudding user response:

I think the best way is to create a script that reads you .env file an runs the command, like this:

#!/bash
# This is /my_path/my_script.sh file,
# do not forget to set executable permission by chmod

# Reading vars
. /my_path/my_env.env
# Calling the command
curl -Ssi -X POST -u "${variable1}:${variable2}" https://example.com...

and your crontab line will be like this:

2 11 * * * /my_path/my_script.sh

or alternatively in not so readable manner, directly in the crontab:

2 11 * * * . /my_path/my_env.env; curl -Ssi -X POST -u "${variable1}:${variable2}" https://example.com...

CodePudding user response:

Source your .env file within cron, and I assume you mean $variable1:$variable2.

2 11 * * * . /path/to/.env; curl -Ssi -X POST -u "$variable1:$variable2" https://example.com...
  • Related