Public-Helperscripts/distributed_cron.sh

40 lines
794 B
Bash
Raw Normal View History

2024-11-15 19:22:29 +01:00
#!/usr/bin/env bash
2024-11-15 19:53:15 +01:00
# Author: H2 invent GmbH <support@h2-invent.com>
# License: AGPLv3
# Usage: Use the helper script in cronjobs in hosts or containers.
# The cron_lock can be shared so the job will only be executed one.
2024-11-17 10:06:16 +01:00
TIME=$(shuf -i 0-900 -n1)
2024-11-15 19:22:29 +01:00
sleep 0.$TIME
echo "Sleep for 0.$TIME"
echo "Checking cron_lock file"
FILE=$1
if [ ! -f $FILE ]
then
echo $(hostname) > $FILE
2024-11-17 10:25:06 +01:00
echo "$(date) -- START CRON_LOCK -- $(hostname)" >> $FILE.log
2024-11-15 19:22:29 +01:00
$2
if [ $? -ne 0 ]
then
echo "FAILED -- $(date) -- $(hostname)" >> $FILE.log
else
echo "OK -- $(date) -- $(hostname)" >> $FILE.log
fi
else
echo "Other Server is running cron job"
2024-11-15 19:53:15 +01:00
fi
2024-11-17 10:06:16 +01:00
2024-11-17 10:25:06 +01:00
sleep 10.$TIME
if [ -f $FILE ]
then
echo "$(date) -- REMOVE CRON_LOCK -- $(hostname)" >> $FILE.log
rm $FILE
fi