Best practise for keeping cronjobs across 2 servers in sync

Hi all,

I have 2 server A and B. B is acting as standby for A.
The cronjobs running in A must not be run in B until failover.
The activation of cronjobs in B can be manual.

In server A, I am doing the following

1) create a cron/job script that does
"crontab -l > cronjob.txt" and scp the cronjob.txt to server

2) keep all my cronjob scripts in a folder /export/home/xxx/scripts and doing a daily rsync to server B in the same path

In event of failover, I will just open cronjob.txt, crontab -e and copy all the output in.

Am I doing this correctly ? or there is a more efficient way to do so ?

Regards,
Noob

Are there any cron jobs on your system that need to run on the stand-by server? If not, have you considered disabling cron on the stand-by server and enable it when there is a switch from stand-by to active.

1 Like

Do you have a shell command that shows if active or standby?
Then you can have identical crontab entries.
For example your shell command is "activecommand" and has an exit status zero when active, otherwise non-zero.

* * * * * if activecommand; then actualcroncommand; fi
1 Like

Hi Don,

Thanks for your reply and sorry for coming back late.
Yes, there are certain standalone cronjobs that needs to be run on the standby server. Thus i am not able to disable cronjob entirely.

Hence my thinking was to sync the cron scripts over and cron entries from the primary over. When there is a failover, i will manually add them (cut/paste) into the crontab in standby

Not sure if there is any other better way to do so..

Regards,
Noob

---------- Post updated at 12:48 PM ---------- Previous update was at 12:46 PM ----------

Hi MadeInGermany,
Thanks for your insight !
I can make a simple function/script that return active or standby, but how do we keep the cron entries in sync on both servers ?

is it a manual work (e.g. when you add in nodeA, do the same on nodeB) ?

Regards,
Noob

You still need to maintain identical crontabs on both servers.
Manual work might be okay.
If you have often changes, and if you have a shared directory say /shared/dir/ then you can go for another entry, for example (every hour)

1 * * * * ctab=/shared/dir/crontab.txt; if activecommand; then crontab -l >$ctab; elif [ -f $ctab ]; then crontab - <$ctab; fi

Note: most Unix crontab commands read from stdin by default, without the - argument.

1 Like

thanks madeingermany!