Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically setting time of cron job based off of system variables

I am wondering if there is a way to dynamically populate the time of a cron job off of an environment variable. For example:

export JOB_MINUTES=4

Then in the crontab

$JOB_MINUTES * * * * /tmp/my_script.sh

Resulting in a crontab of

4 * * * * /tmp/my_script.sh

like image 433
sudobangbang Avatar asked Oct 27 '25 07:10

sudobangbang


1 Answers

Environment variables aren't expanded inside the crontab file. But you can use the environment variable when you're creating the crontab file, by piping an echo command to crontab.

{ crontab -l ; echo $JOB_MINUTES * * * * /tmp/my_script.sh; } | crontab -

The - filename argument means to get the crontab contents from standard input.

like image 150
Barmar Avatar answered Oct 28 '25 20:10

Barmar



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!