My cron framework..;-) How do you do it?
Posted : May 4, 2004 at 10:41 am [America/Los_Angeles]
Ok, so here’s Anand’s Law
:
For every transactional web application out there in businesses, there are atleast twice as many scheduled (cron) jobs propping it up.
Here are some details of how I tend to handle my cron needs:
Folder (adjust it to your liking):
-------------------------------------
/usr/local/allcrons
Sub-folders (feel free to add/subtract):
-----------------------------------------------
bin/ etc/ log/ var/
Main cron script (allcrons/bin/run.sh):
----------------------------------------------
#!/bin/sh
# Top level folder
CRON_TOP=/usr/local/allcrons
export CRON_TOP
#### Log folder
CRON_LOG=${CRON_TOP}/var/log
export CRON_LOG
### Capturing the date/time
CRON_EXEC_TIME=`date +%d-%m-%y_%H.%M.%S`
export CRON_EXEC_TIME
### Email Address
EMAIL_ADDRESS=<your-email-address>
export EMAIL_ADDRESS
#### Environment variables
export PATH=$PATH:/usr/local/mysql/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/lib:/usr/lib
########################################################################
# Display the end of execution
########################################################################
function displayfinalbanner () {
echo
echo "Execution completed !!!"
echo
}
########################################################################
# Function to display usage information and exit
########################################################################
function displayusagebanner () {
echo "usage: ${0} <param>"
cat <<EOF
where <param> equals one of the following:
mysqlbk: Execute MySQL backup
EOF
}
########################################################################
# Getting ready to start execution...
########################################################################
function init() {
echo "Setting the current dir to ${CRON_TOP} ..."
echo
cd ${CRON_TOP}
PRESENT_DIR=`pwd`
echo "Changed current dir to ${PRESENT_DIR} ..."
echo
}
########################################################################
# Execute MySQL Backup
########################################################################
function mysqlbk() {
echo "Starting mysqldump..." >> ${CRON_TOP}/var/cron-output.txt
mysqldump --add-drop-table --extended-insert --lock-tables -u <your-blogdb-id>
--password="<your-blogdb-password>" <your-blogdb> > ${CRON_TOP}/db/mt.dmp
echo "Compressing the dump using bzip2..." >> ${CRON_TOP}/var/cron-output.txt
bzip2 -f ${CRON_TOP}/db/mt.dmp
}
########################################################################
# Function to start email before a run
########################################################################
function startmail () {
echo "Starting cron run..." > ${CRON_TOP}/var/cron-output.txt
echo "-----------------------------" >> ${CRON_TOP}/var/cron-output.txt
echo "" >> ${CRON_TOP}/var/cron-output.txt
}
########################################################################
# Function to send email after a run
########################################################################
function sendmail () {
echo "" >> ${CRON_TOP}/var/cron-output.txt
echo "Signed" >> ${CRON_TOP}/var/cron-output.txt
echo "The Ghost who walks, talks and emails...." >> ${CRON_TOP}/var/cron-output.txt
# Send email
/usr/lib/sendmail ${EMAIL_ADDRESS} < ${CRON_TOP}/var/cron-output.txt
}
#### Check for proper usage (mainly number of arguments passed)
ERROR=1
ARGS=$#
if [ $ARGS -lt 1 ]
then
displayusagebanner
exit 1
fi
case ${1} in
mysqlbk|MYSQLBK)
init
startmail
mysqlbk
sendmail
;;
*)
displayusagebanner
exit 1
;;
esac
echo
displayfinalbanner
# End
Cron entries (allcrons/etc/cron.txt):
0 1 * * * /usr/local/allcrons/bin/run.sh mysqlbk > /tmp/cron-mysqlbk.log 2>&1
How to add it to your crontab:
(unix-prompt)>crontab cron.txt
So, if I want to add a new cron, all I do is:
- Update my ‘displayusagebanner’ banner method, add a new method (’doblah’) and then create a new ‘case’ entry to call the new method.
- Anand
Category: Services and Software
Viewed: 841 times