How to run a script using a cron job

In this article, we will show you how to setup a cron job to run a bash script. This can be useful if you want to run a script to rotate logs or any other kind of task that needs to be automated (backups, log dumps, etc).

In this demonstration, we will be using a script that rotates the productions logs of our rails application.

Below is the example script we will be using.

#!/bin/bash
 
## Check to make sure the user inputed a log file. ##
if [ "$1" = "" ]; then
	echo "ERROR: You did not input the log file name."
	exit 1
fi
 
## Set the logfile var ##
logfile=$1
 
## Check to make sure the file they inputed is real. ##
if [ ! -f $logfile ]; then
	echo "I could not find that log file. Please check the filename and try again."
	exit 1
fi
 
## Check to see if the user inputs a custom output dir. ##
if [ "$2" = "" ]; then
	echo "I see you don't want to save the old log file to a custom directory, thats fine."
	echo "We will just save it here: "
	echo "-------------------------------------"
	pwd
	echo "-------------------------------------"
	dest=$(pwd)
	echo; echo
else
	dest=$2
	if [ ! -d $dest ]; then
		echo "The file destonation you gave does not exist. We are going to stop now." 
		exit 1
	fi
fi
 
timestamp=`date +%Y%m%d`
newlogfile=$logfile.$timestamp
 
if [ -f $dest/$newlogfile.zip ]; then
	echo $newlogfile.zip "already exists. I changed the filename to prevent data loss."
	extra=$(date +"%H.%M.%S")  
	newlogfile=$newlogfile.$extra
	echo
	echo "The name of the new rotated logfile is now:"
	echo "-------------------------------------"
	echo $newlogfile.zip
	echo "-------------------------------------"
fi
 
cat $logfile > $dest/$newlogfile
> $logfile
 
echo
echo "I have copied the information from:"
echo "====================================="
echo $(pwd)/$logfile
echo "----------------- TO ----------------"
echo $dest/$newlogfile.zip
echo "====================================="
echo "Your original log file is now blank and ready to accept more logs."
touch $logfile
/usr/bin/zip -m $dest/$newlogfile.zip $dest/$newlogfile

This script makes a copy of the log file, zips it, then clears the original file.

Note: The log rotate script above is to be used as a base example. We are not responsible for any damage caused by this script. Use it at your own risk!

For this article, we are going to store this file (we are calling it logrotate.sh) in our home (~/) folder. However, you can place your script files anywhere you wish (be sure to update any paths).

Next we need to give it permisions to run on our account. We can do this by issuing the following command over SSH:

chmod +x ~/logrotate.sh

Now that we have permission to run it, lets test the command and make sure it works correctly. For this example, we are using a redmine install, so our logs are located at ~/redore/redmine-2.1/log/ and the log file we want to rotate is called production.log. So here is the command we are going to use.

cd ~/redore/redmine-2.1/log; ~/logrotate.sh production.log

You should see output similar to the following (if you are using the script above).

I see you don’t want to save the old log file to a custom directory, thats fine.
We will just save it here:
————————————-
/home/redmine2/redore/redmine-2.1/log
————————————-

 

I have copied the information from:
=====================================
/home/redmine2/redore/redmine-2.1/log/production.log
—————– TO —————-
/home/redmine2/redore/redmine-2.1/log/production.log.20121218.gz
=====================================
Your original log file is now blank and ready to accept more logs.

Now that we know the script works, we need to set that command up in a cron job.

For information on how to add cron jobs, please see one of the following two links.

  • SiteAdmin: Cron Jobs
  • cPanel: Cron Jobs

Here is a screen shot of our cron job in SiteAdmin

As you can see, we have it set to run every day, and we pasted the command we used above into the command field. If you want to get a copy of the results sent to you via email, leave the “Disable Email Notification” check-box unchecked. This can be helpful for reviewing results of our scripts.

Here is an example of what a email would look like (if using the script above) (sent from root@serverhost.accountservergroup.com):

I see you don’t want to save the old log file to a custom directory, thats fine.
We will just save it here:
————————————-
/home/redmine2/redore/redmine-2.1/log
————————————-

production.log.20121218.gz already exists. I changed the filename to prevent data loss.

The name of the new rotated logfile is now:
————————————-
production.log.20121218.13.30.01.gz
————————————-

I have copied the information from:
=====================================
/home/redmine2/redore/redmine-2.1/log/production.log
—————– TO —————-
/home/redmine2/redore/redmine-2.1/log/production.log.20121218.13.30.01.gz
=====================================
Your original log file is now blank and ready to accept more logs.

That’s it! You now know how to run a basic script using a cron job.

One thing to keep in mind is if you are rotating logs on a rails application, sometimes moving, renaming, or editing the original log file can cause the rails application to stop writing logs to that file. You can fix this by restarting the application. In fact, you can add a restart to the cron command if you wish.

For example, to restart our redmine application, we have to:

touch ~/redore/redmine-2.1/tmp/restart.txt

If we want to add this to our cron job, simply modify the command and add “; touch ~/redore/redmine-2.1/tmp/restart.txt” to the end of the command. Here is what our full command would look like if we are using the script above.

cd ~/redore/redmine-2.1/log; ~/logrotate.sh production.log; touch ~/redore/redmine-2.1/tmp/restart.txt

  • 0 Users Found This Useful
Was this answer helpful?

Powered by WHMCompleteSolution