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 jobs in linux.
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 jobs in linux.
If I understand what you're looking for, you'll need to do something a bit messy, like having a cron job that runs a bash script that randomizes the run times... Something like this:
crontab:
0 9 * * * /path/to/bashscript
and in /path/to/bashscript:
#!/bin/bash
maxdelay=$((14*60)) # 14 hours from 9am to 11pm, converted to minutes
for ((i=1; i<=20; i++)); do
delay=$(($RANDOM%maxdelay)) # pick an independent random delay for each of the 20 runs
(sleep $((delay*60)); /path/to/phpscript.php) & # background a subshell to wait, then run the php script
done
A few notes: this approach it a little wasteful of resources, as it fires off 20 background processes at 9am, each of which waits around for a random number of minutes (up to 14 hours, i.e. 11pm), then launches the php script and exits. Also, since it uses a random number of minutes (not seconds), the start times aren't quite as random as they could be. But $RANDOM only goes up to 32,767, and there are 50,400 seconds between 9am and 11pm, it'd be a little more complicated to randomize the seconds as well. Finally, since the start times are random and independent of each other, it's possible (but not very likely) that two or more instances of the script will be started simultaneously.
((maxdelay = 14 * 60))
or ((delay = $RANDOM % maxdelay))
). The sleep
argument still needs to be the way you have it (although you could add spaces, if desired). –
Negligee sleep $[ ( $RANDOM % 60 ) + 1 ]s && some_script.sh
–
Zulmazulu How to cron something at a random offset 20 times a day between 9am and 11pm? That's kinda tricky within cron, because you are dividing 14 hours by 20 execution times. I don't like the other answers very much because they require writing a bash wrapper script for your php script.
However, if you'll allow me the liberty to ease the timing and frequency restriction to 13 times between 8:30am and 11:09pm, this might do the trick, and all within the confines of your crontab:
30 8-21/* * * * sleep ${RANDOM:0:2}m ; /path/to/script.php
${RANDOM:3:2} uses bash's $RANDOM that other people have mentioned above, but adds bash array slicing. Since bash variables are untyped, the pseudo-random signed 16-bit number gets truncated to the first 2 of its 5 decimal digits, giving you a succinct one-liner for delaying your cronjob between 10 and 99 minutes (though the distribution is biased towards 10 to 32).
The following might also work for you, but I found it do be "less random" for some reason (perhaps Benford's Law is triggered by modulating pseudo-random numbers. Hey, I don't know, I flunked math... Blame it on bash!):
30 8-21/* * * * sleep $[RANDOM\%90]m ; /path/to/script.php
You need to render modulus as '%' above because cron (well, at least Linux 'vixie-cron') terminates the line when it encounters an unescaped '%'.
Maybe you could get the remaining 7 script executions in there by adding another line with another 7-hour range. Or relax your restriction to run between 3am and 11pm.
$[(RANDOM/368)+10]
? –
Gingerly $((RANDOM % 90 + 10))
Test: for i in {0..9999}; do echo $((RANDOM % 90 + 10)); done | sort | uniq -c
–
Rattigan $RANDOM
: sleep $(( $(od -N1 -tuC -An /dev/urandom) \% 90 ))m
. –
Osteoblast crontab
is using bash
before you use $RANDOM
. If you have vixie-cron
(seems to be my case on Ubuntu), then you can add SHELL=/bin/bash
to the top. There are more alternatives for other cron versions here: superuser.com/a/264541/260350 –
Moccasin crontab: errors in crontab file, can't install. Do you want to retry the same edit?
please help –
Crispas 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
ruby -e 'sleep rand 900'
–
Whalebone If I understand what you're looking for, you'll need to do something a bit messy, like having a cron job that runs a bash script that randomizes the run times... Something like this:
crontab:
0 9 * * * /path/to/bashscript
and in /path/to/bashscript:
#!/bin/bash
maxdelay=$((14*60)) # 14 hours from 9am to 11pm, converted to minutes
for ((i=1; i<=20; i++)); do
delay=$(($RANDOM%maxdelay)) # pick an independent random delay for each of the 20 runs
(sleep $((delay*60)); /path/to/phpscript.php) & # background a subshell to wait, then run the php script
done
A few notes: this approach it a little wasteful of resources, as it fires off 20 background processes at 9am, each of which waits around for a random number of minutes (up to 14 hours, i.e. 11pm), then launches the php script and exits. Also, since it uses a random number of minutes (not seconds), the start times aren't quite as random as they could be. But $RANDOM only goes up to 32,767, and there are 50,400 seconds between 9am and 11pm, it'd be a little more complicated to randomize the seconds as well. Finally, since the start times are random and independent of each other, it's possible (but not very likely) that two or more instances of the script will be started simultaneously.
((maxdelay = 14 * 60))
or ((delay = $RANDOM % maxdelay))
). The sleep
argument still needs to be the way you have it (although you could add spaces, if desired). –
Negligee sleep $[ ( $RANDOM % 60 ) + 1 ]s && some_script.sh
–
Zulmazulu Some cron implementations offer a RANDOM_DELAY
variable.
The RANDOM_DELAY variable allows delaying job startups by random amount of minutes with upper limit specified by the variable.
This is seen commonly in anacron
jobs, but also can be useful in a crontab
.
You might need to be careful with this if you have some jobs that run at fine (minute) granularity and others that are coarse.
RANDOM_DELAY
is established once and remains constant for the whole runtime of the daemon. –
Hopeh cron
package is 3.0pl1-128.1ubuntu1
–
Troublemaker RANDOM_DELAY
flag is feature of cronie-crond while Ubuntu seems to be running vixie-cron
which lacks this flag. –
Gibby RANDOM_DELAY
is described in info crontab
as well as man 5 crontab
for cronie
on CentOS 8. I was starting to suspect that it wasn't there, but I was just looking at section 1 instead of section 5. –
Cosmic 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
date
(which you probably do on most Linuxes, but not on Busybox, standard MacOS, or various other BSD-derived platforms). –
Tentage 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.
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
$[ ... ]
is deprecated syntax since waaaay back; for anything from this millennium, you would prefer $((RANDOM\%90))m
which is POSIX-compatible syntax (but of course RANDOM
is still Bash only). –
Tentage You can try with this example to use random times before execute command:
#!/bin/bash
# start time
date +"%H:%M:%S"
# sleep for 5 seconds
sleep $(shuf -i 1-25 -n 1)
# end time
date +"%H:%M:%S"
at -f [file] [timespec]
or
echo [command] | at [timespec]
or
at [timespec]
... and interactive specification
like script
's recording.
At runs the text provide on stdin or in the file specified by -f [file]
.
Here's the [timespec]
grammar. It can be something like:
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.
cat script.sh | at now + $(($RANDOM % 10)) hours $(($RANDOM % 60)) minutes
at -f script.sh now + $(($RANDOM % 10)) hours $(($RANDOM % 60)) minutes
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]
.
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.
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.
~$info dd
and I can understand what's going on on the left side of |
, but can't make out the right side. So, for me and others interested in overcoming some restrictions
in random value generation, why not take a moment to explain the RHS, and make a stronger pitch for using your approach. The depth of explanation both makes people comfortable with the process you suggest and its benefits Thanks. –
Angelenaangeleno What about creating a script that rewrites the crontab every day?
I was looking for the same problem, for me the best solution was to create a schedule on the server side (it's very simple by means of a programming language), populate a db table with these tasks with two columns: run_time and status (new | processed). So my cron runs every minute and finds jobs that need to be done (status = new and running time <= now()). Very simple and there is one huge plus: in this case I can make an interface for it and see what happens...
As it's not mentioned by now, I will show another clean way: Using 'jot'
jot comes on every BSD unix (available as athena-jot in many linuxes, too) and is a sequential and random(!) number generator like seq but easier and more powerful. This simple command:
sleep $(jot -nr 1 1 3600); scriptname
in your crontab executes 'scriptname' after a delay of 1 to 3600 seconds of sleep.
jot -nr 1 1 360
means: give one (first 1) random (-r) number without newline (-n) in the range of one (second number - here 1) and 3600 (third number - here 3600) Clean syntax.
For those who googled the way here:
If you are using anacron (Ubuntu desktop and laptop) then you can edit
/etc/anacrontab
and add
RANDOM_DELAY=XX
Where XX is the amount of minutes you want to delay the base job.
Anacron is like cron but it does not expect your computer to be on 24x7 (like our laptops) and will run the scripts that it missed because the system was down.
© 2022 - 2024 — McMap. All rights reserved.