Skip to content
Snippets Groups Projects
Commit 1fa311b2 authored by ale's avatar ale
Browse files

Add a simple cron job wrapper script

parent a1fc2397
No related branches found
No related tags found
No related merge requests found
...@@ -6,6 +6,7 @@ ENV S6_READ_ONLY_ROOT=1 \ ...@@ -6,6 +6,7 @@ ENV S6_READ_ONLY_ROOT=1 \
COPY etc/ /etc/ COPY etc/ /etc/
COPY deb_autistici_org.gpg /usr/share/keyrings/deb.autistici.org.gpg COPY deb_autistici_org.gpg /usr/share/keyrings/deb.autistici.org.gpg
COPY every /usr/bin/every
ADD https://github.com/just-containers/s6-overlay/releases/download/v2.2.0.3/s6-overlay-amd64.tar.gz /tmp/ ADD https://github.com/just-containers/s6-overlay/releases/download/v2.2.0.3/s6-overlay-amd64.tar.gz /tmp/
......
...@@ -55,7 +55,7 @@ it is simpler to create a secondary service that will run a loop with ...@@ -55,7 +55,7 @@ it is simpler to create a secondary service that will run a loop with
*sleep*: *sleep*:
``` ```
#!/bin/sh -e #!/bin/sh
period=600 period=600
...@@ -65,27 +65,16 @@ while true; do ...@@ -65,27 +65,16 @@ while true; do
done done
``` ```
while the above is ok for short periods, for longer ones (and for more To simplify this problem, we ship a simple shell-based "cron job
resource-intensive jobs) it might be better to wait a random amount of runner" that implements a sligthly more sophisticated version of the
time at startup. Consider that "dash" (/bin/sh in Debian) has no above loop (with random initial delay to stagger jobs on restart)
$RANDOM, so something like this might be necessary: called *every*. The above example should then be written like this:
``` ```
#!/bin/sh #!/bin/sh
exec every 600 /usr/bin/my-command foo bar
period=${SCAN_PERIOD_SECS:-86400}
offset=$(shuf -i 0-${period} -n 1)
sleep $offset
while true; do
/usr/bin/my-command foo bar
sleep $period
done
``` ```
Randomization maximizes the chances that the job will run regularly
even in face of restarts etc.
## Differences with stock *s6-overlay* ## Differences with stock *s6-overlay*
The base s6-overlay distribution is modified slightly to work around The base s6-overlay distribution is modified slightly to work around
......
every 0 → 100755
#!/bin/sh
#
# Cron runner for a single job.
#
if [ $# -lt 2 ]; then
echo "Usage: $0 <period> <cmd>..." >&2
exit 2
fi
period="$1"
shift
# Work around the lack of $RANDOM in dash.
offset=$(shuf -i 0-${period} -n 1)
sleep ${offset}
while true; do
"$@"
sleep ${period}
done
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment