Read Env Variable in Crontab

Viewed 20

I want to read a env variable in crontab.

I tried with run printenv from crontab and it runs fine:

* * * * * BASH_ENV=/etc/profile bash -c "printenv CNE_CLUSTER_NAME  >> output.txt"

I need to run a job with some operation performed by python's script. I sheduled it in crontab like this:

* * * * * BASH_ENV=/etc/profile bash -c "/usr/bin/python3 /var/cne/cluster/$(echo $(printenv CNE_CLUSTER_NAME))/scripts/setup/job.py"

But when running from crontab is var CNE_CLUSTER_NAME is empty

python3: can't open file '/var/cne/cluster//scripts/setup/job.py': [Errno 2] No such file or directory)

So, how can i do to read variable or replaced the values's variable in the string ? Please.

1 Answers

You don't need printenv to acces the value of CNE_CLUSTER_NAME.

* * * * * BASH_ENV=/etc/profile bash -c '/usr/bin/python3 /var/cne/cluster/$CNE_CLUSTER_NAME/scripts/setup/job.py'

$CNE_CLUSTER_NAME is passed literally, and the shell that executes the command will expand the parameter to the value found in its environment.

Related