Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of python code in crontab in centos 7 while auto renewing certificates?

I am going to set crontab for auto renewal of lets-encrypt certificate. I have centos7.

Following is my command for crontab.

0 0,12 * * * python -c 'import random; import time; time.sleep(random.random() * 3600)' && certbot renew 

I know only thing is certbot renew will renew the certificate prior expiry date. And 0 0,12 * * * is a cron time, this cron will run noon & midnight per day.

What is the use of this python command? Simply I don't know following part of cron.

python -c 'import random; import time; time.sleep(random.random() * 3600)'

like image 516
Mayank Dudakiya Avatar asked Jan 02 '23 03:01

Mayank Dudakiya


1 Answers

It sleeps for on average half an hour, presumably to prevent all bots in the world from hitting the server exactly on the hour when they want an update.

The argument to time.sleep() is a number of seconds, and the randomization picks a value between 0 and 3600.

If you had Bash you could do something similar with sleep $((RANDOM/10)); but cron jobs by definition run /bin/sh, not Bash. (RANDOM returns an integer between 0 and 32767 - the proper divisor would be something like 9.1; but Bash only supports integer arithmetic.)

like image 163
tripleee Avatar answered Jan 05 '23 15:01

tripleee