I have 2 servers: 1 master with 1 slave and have a script which is running every 1 minute on the slave to check the availability of the master.
It should run always:
* * * * * /data/BackupServer/StatusCheck.bash >> /data/BackupServer/Output.log 2>&1
Background: Master is responsible to run scripts in the background using crontab.
What I want is:
- If the master is not available then the slave should add those responsibilities to its crontab to take over the master.
- If the master is available again, the slave should
rm
these crontab configurations and hand over its duties to master again, if it has any.
What I have is:
#!/bin/bash
check=$(curl -s -w "%{http_code}\n" -L "master" -o /dev/null)
if [[ $check == 200 || $check == 403 ]]
then
# Service is online
echo "Service is online, slave is handing over the tasks to master if any"
exit 0
else
# Service is offline or not working correctly
echo "Service is offline or not working correctly, slave is taking over master now"
exit 1
fi
What I need is:
When the server is not available, the slave should start manually some of the scripts which are running in crontab of the master. But, here am not adding them in to crontab, instead asking the script to run only if the server is not available.
Can somebody suggest me a way how can I do this process automated in bash. I am not asking someone to write complete code for me, as am not familiar with the concepts need some help.
E.G. Can I do something as below ?
#!/bin/bash
check=$(curl -s -w "%{http_code}\n" -L "master" -o /dev/null)
if [[ $check == 200 || $check == 403 ]]
then
# Service is online
echo "Service is online, slave is handing over the tasks to master if any"
exit 0
else
# Service is offline or not working correctly
echo "Service is offline or not working correctly, slave is taking over master now"
/manoj/scripts/location.plx > /manoj/logs/location/sync.log 2>&1
/manoj/scripts/report.py > /manoj/logs/dashboard/dashboard.log 2>&1
/etc/profile; /manoj/scripts/Space.py > /manoj/logs/dashboard/Consumption.log 2>&1
exit 1
fi
Don't do
crontab
manipulation.Run scripts at both the master and the slave. On the slave, start each script with:
If the master is available, the scripts on the slave will exit after the test.
I would prefer to have the same script running on both machines to reduce maintenance. Assuming the
/etc/hosts
file on the master contained the name "master" both machines could have the same script like this:Now there is only a single script to maintain for both servers. When a new job is setup only one file has to be edited.