Cron jobs and random times, within given hours

后端 未结 12 2289
甜味超标
甜味超标 2020-11-27 11:02

I need the ability to run a PHP script 20 times a day at completely random times. I also want it to run only between 9am - 11pm.

I\'m familiar with creating cron job

相关标签:
12条回答
  • 2020-11-27 11:12

    at -f [file] [timespec]

    or

    echo [command] | at [timespec]

    or

    at [timespec] ... and interactive specification like script's recording.

    Command

    At runs the text provide on stdin or in the file specified by -f [file].

    Timespec

    Here's the [timespec] grammar. It can be something like:

    • 24-hour time as 4-digit int, e.g. 0100, 2359, 1620
    • now + 10 minutes
    • 2071-05-31 - 5 hours 12 minutes UTC

    If you're explicitly specifying the timezone, some versions of the timespec might only allow UTC for the optional timezone argument.

    Example

    cat script.sh | at now + $(($RANDOM % 10)) hours $(($RANDOM % 60)) minutes

    at -f script.sh now + $(($RANDOM % 10)) hours $(($RANDOM % 60)) minutes

    Try it out...

    You can test the bash parsing by pre-pending echo and escaping the | (pipe).

    echo cat script.sh \| at now + $(($RANDOM % 10)) hours $(($RANDOM % 60)) minutes

    echo at -f script.sh now + $(($RANDOM % 10)) hours $(($RANDOM % 60)) minutes

    To see jobs scheduled, use atq and job contents (environment vars, setup, and command/script) with at -c [jobid].

    Note

    The system is part of cron, and the interactive prompt actually captures the whole current state of your shell, so you can run commands without specifying absolute paths.

    0 讨论(0)
  • 2020-11-27 11:16

    My first thought would be to create one cron job launching 20 randomly scheduled at jobs. The at utility (http://unixhelp.ed.ac.uk/CGI/man-cgi?at) is used for executing commands at specified time.

    0 讨论(0)
  • 2020-11-27 11:16

    I ended up using sleep $(( 1$(date +%N) % 60 )) ; dostuffs (compatible with bash & sh)

    The 1 prefix is to force NON base 8 interpretation of date +%N (e.g. 00551454)

    Do not forget to escape % using \% in a crontab file

    * * * * *  nobody  sleep $(( 1$(date +\%N) \% 60 )) ; dostuffs 
    
    0 讨论(0)
  • 2020-11-27 11:17

    al-x 's Solution does not work for me since crontab commands are not executed in bash but in sh I guess. What does work is:

    30 8 * * * bash -c "sleep $[RANDOM\%90]m" ; /path/to/script.py
    
    0 讨论(0)
  • 2020-11-27 11:19

    I realize it's an older thread, but I want to add one random-value related thing that I use a lot. Instead of using the $RANDOM variable with a fixed and limited range, I often make arbitrary-range random values in the shell with

    dd if=/dev/urandom bs=4 count=1 2>/dev/null | od -N4 -t u4 -A none
    

    so you can do, for example,

    FULLRANDOM=$(dd if=/dev/urandom bs=4 count=1 2>/dev/null | od -N4 -t u4 -A none)
    

    and overcome some the restrictions that were discussed in this thread.

    0 讨论(0)
  • 2020-11-27 11:24

    So I'm using the following to run a command between 1AM and 330AM

    0 1 * * * perl -le 'sleep rand 9000' && *command goes here*
    

    That has been taking care of my random needs for me. That's 9000 seconds == 150 minutes == 2.5 hours

    0 讨论(0)
提交回复
热议问题