Cron jobs and random times, within given hours

后端 未结 12 2317
甜味超标
甜味超标 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.

提交回复
热议问题