This is a script I was hired to write by a company that has a Ruby On Rails application that uses MS SQL for its backend. MSSQL runs select DB processes each day through an SSIS package. Upon successfully completing its responsibilities, it drops a file via ftp on the webserver. A nightly cron job runs this script, which checks for a current ftp’d file from MSSQL. If the file is there, it then kicks off a ruby sript/runner process which grabs information from the database; generates creates pdf reports and emails them to designated users. If it doesn’t find the file, it emails the website admins to let them know something has failed. (I’ve sanitized the site/domain details):
#!/bin/sh
#####################################################
# #
# website.com script for allowing the server to #
# run a ruby script/runner-enabled mailer action. #
# This script runs every day... It checks for a #
# datestamped file that is ftp'd from a Windows #
# SQL server. This script checks the date and #
# acts accordingly #
# #
# last modified: 12/09/08 by tburns #
SCRIPT_VERSION="2.0" #
# #
#####################################################
#Script Variables:
DATE=`date +%m%d%y-%H%M`
SCRIPTS_CONTAINER=/etc/scripts
LOG_FILE=$SCRIPTS_CONTAINER/websiteEmail.log
#The website is deployed via subversion. Each time a new version is checked out
#it created a new directory/file structure. These next few variables find the
#latest checked out version from which to run the process. It is necessary to
#do this to be sure the process is running using the most current application files:
APPLICATION_FILES=/wwwFiles/public/domain.com/releases/
CURRENT_FT_RELEASE=`ls -t $APPLICATION_FILES | head --lines 1`
APPLICATION_LOG_FILE=$APPLICATION_FILES$CURRENT_FT_RELEASE/log/production.log
#This is the location of the FTP'd file. This system uses vsftpd which creates a
#chroot'd environment for dropping files:
USER_DIR=/home/deploy/ftpuser
CRITICAL_FILENAME=done.txt
#This locates and searches for the date stamp on the file that is FTP'd from SQL
#It looks for a file that was created within the past 24 hours (-mtime 0) and
#that was created on the same date as the run-time on the script (-daystart)
CURRENT_FILENAME=`find $USER_DIR/$CRITICAL_FILENAME -daystart -mtime 0 -print`
ADMINS=websiteAdmins@domain.com
echo " " >>$LOG_FILE
echo " "
echo "Ok, starting . . ." > $LOG_FILE
echo "Ok, starting . . ."
echo " " >>$LOG_FILE
echo " "
echo "Date: $DATE" >> $LOG_FILE
echo "Date: $DATE"
echo This begins the $0 script, version $SCRIPT_VERSION >> $LOG_FILE
echo This begins the $0 script, version $SCRIPT_VERSION
echo " " >> $LOG_FILE
echo " "
if [ $CURRENT_FILENAME ]; then
echo " The file appears to be here: $USER_DIR/$CRITICAL_FILENAME " >> $LOG_FILE
echo " The file appears to be here: $USER_DIR/$CRITICAL_FILENAME "
echo " We can proceed with the ruby job " >> $LOG_FILE
echo " We can proceed with the ruby job "
echo " " >> $LOG_FILE
echo " "
#...
#I've cleaned out all of the if/else routines and script/runner details
#...
echo "Done. Now emailing the ADMINS = $ADMINS" >>$LOG_FILE
echo "Done. Now emailing the ADMINS = $ADMINS"
mail -s "$0 script completed on $HOSTNAME " $ADMINS < $LOG_FILE
else #If it doesn't find what it's looking for:
echo " There is no current $USER_DIR/$CRITICAL_FILENAME. " >> $LOG_FILE
echo " There is no current $USER_DIR/$CRITICAL_FILENAME. "
echo " The contents of the Directory: ls -alh $USER_DIR" >> $LOG_FILE
echo " The contents of the Directory: ls -alh $USER_DIR"
echo " " >> $LOG_FILE
echo " "
ls -alh $USER_DIR >> $LOG_FILE
ls -alh $USER_DIR
echo " Emailing the admins..." >> $LOG_FILE
echo " Emailing the admins..."
echo " " >> $LOG_FILE
echo " "
mail -s "$HOSTNAME's $0 script failed. There was no current SSIS file found. " $ADMINS < $LOG_FILE
fi
echo " " >> $LOG_FILE
echo " "
exit 0



