I've setuped load balancers: lb1
(active) and lb2
(passive), Glustered web servers: web1
(active), web2
(backup), and some clustered database. Both web servers and databases are A
pointed to the VIP of the load balancers.
Both of web servers have their copy of cron jobs. Assuming the following tasks:
* * * * * echo $(hostname) >> crontab.txt
0 0 1 * * ~/bin/another/task 2>&1
With some random lock algorithm:
lock_dir=~/.cronlock
pid_file=~/.cronlock/pid
if ( mkdir ${lock_dir} ) 2> /dev/null; then
echo $$ > $pid_file
trap 'rm -rf "$lock_dir"; exit $?' INT TERM EXIT
# Crons
rm -rf "$lock_dir"
trap - INT TERM EXIT
fi
Is safe to have something like
* * * * * ./lock_algorithm -f LOCK_FILE1 -c "echo $(hostname) >> crontab.txt"
0 0 1 * * ./lock_algorithm -f LOCK_FILE2 -c "~/bin/another/task 2>&1"
Where I send a "per-cron-command" unique lock file name and a command to be executed?
By "safe" I mean web1
OR web2
will run, not both.
And if I need cron overlap (eg: each minute I perform a long task limited to the current minute)? How to get web1
's cron executing again, assuming that web1
is the active "cron runner"?