Linux USES the Cron+AT implementation to execute commands at random for a specified period of time

  • 2020-05-10 23:23:10
  • OfStack

I wrote a script to check in, but I don't want to always check in at a certain time, otherwise the records in the database are too fake, so I need to randomly select a time to execute within a certain period of time. Finally, I thought of using Cron+AT implementation

The idea is simple: cron sets a starting time of, say, 6 a.m. each day and checks in at random for 2 to 350 minutes after that time

So as long as that's the case


0 6 * * * echo 'sleep ' $(shuf -i 1-60 -n 1)';date >>/tmp/xxxx.txt' |at now + $(shuf -i 2-350 -n 1) min

The reason for the addition of random 1-60 second sleep is that at is an hourly execution and does not want to have only minute-by-minute check-in records in the database.

This saves a lot of resources compared to the method of sleep directly in cron, where the maximum residence time of sleep is no more than 60 seconds

To keep it simple inside cron, you can wrap another script


Related articles: