How to save time with cron: Basic guide and sample codes

What is cron?

Cron is a Linux/UNIX daemon that is designed to execute a command at a predefined time. Since cron is a daemon, once it is executed it does not require any administration from the user. Cron is controlled by a set of files called “cronfiles”, below is a list of common cron commands.

Crontab filename Install filename as your crontab file.
crontab -eEdit your crontab file.
crontab -lShow your crontab file.
crontab -rRemove your crontab file.
MAILTO=user@domain.comEmails the output to the specified address.

Each entry into the crontab file will consist of the following six fields separated by a space. The order of the fields along with a brief description of each one is listed below.
minute(s) hour(s) day(s) month(s) weekday(s) command(s)

FieldValueDescription
Minute0-59Defines the exact minute the command will execute.
Hour0-23Defines the hour of the day the command will execute.
Day1-31Defines the day of the month the command will execute.
Month1-12Defines the month of the year the command will execute.
Weekday0-6Defines the day of the week the command will execute.
Sunday=0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6
CommandSpecialThe complete command that will be executed.

You can also use an * in place of the numerical character of the first five fields to indicate all legal values. For example, 0 0 * * 1 command, would run a script on every Monday.
Most of this section is only relevant if you are running scripts from the command terminal in Linux/UNIX, if you are using a cPanel please view the “How do I run a cron script from my cPanel” section.

How can I save time with cron?

There are many ways you can use cron to save some but to me the most useful tasks you can schedule are running database and website backup scripts. Both of these jobs can easily be done manually but they are often over looked. With cron you can set them up once and know it will get done.

Below are sample scripts you can use to setup these jobs with cron.

Automatic WordPress Database Backups (Script provided by T2)

#Set the 4 variables
#Replace what is AFTER the = with the information from your wp-config.php file

DBNAME=DB_NAME

DBPASS=DB_PASSWORD

DBUSER=DB_USER

#Keep the ” around your address
EMAIL=”you@your_email.com”

mysqldump –opt -u $DBUSER -p$DBPASS $DBNAME > backup.sql
gzip backup.sql
DATE=`date +%Y%m%d` ; mv backup.sql.gz $DBNAME-backup-$DATE.sql.gz
echo ‘Blog Name:Your mySQL Backup is attached’ | mutt -a $DBNAME-backup-$DATE.sql.gz $EMAIL -s “MySQL Backup”
rm $DBNAME-backup-$DATE.sql.gz

Automated Website Backups (Script provided by Ubuntu)

#!/bin/sh

####################################

#

# Backup to NFS mount script.

#

####################################

# What to backup.

backup_files=”/home /var/spool/mail /etc /root /boot /opt”

# Where to backup to.

dest=”/mnt/backup”

# Create archive filename.

day=$(date +%A)

hostname=$(hostname -s)

archive_file=”$hostname-$day.tgz”

# Print start status message.

echo “Backing up $backup_files to $dest/$archive_file”

date

echo

# Backup the files using tar.

tar czf $dest/$archive_file $backup_files

# Print end status message.

echo

echo “Backup finished”

date

# Long listing of files in $dest to check file sizes.

ls -lh $dest

 

*Disclaimer: We are not a responsible if the script fails to run correctly or if you set it up incorrectly. If you have any questions or concerns about the script or how to set it up the best contact will be your host provider.

How do I run a cron script from my cPanel?

1. Log into you cPanlel

2. Locate the “cron jobs” icon (This is generally in the advanced section).

3. Enter your E-Mail address if you would like a copy of the crop output to be emailed to you.

4. Choose when you would like your cron script to run. (Choosing an item from the “Common Settings” dropdown box will fill in the fields for you.)

5. Enter the path of the script you would like to run. (Note: You will need to upload your script file to your server, for more information please see below – “How do I upload my script file” section for details.)

6. Click “Add New Cron Job”

7. Your cron job should now be listed under “Current Cron Jobs”.

How do I upload my script file?

  1. From your cPanel choose “File Manager”
  2. Next choose “Home Directory” then click “Go”
  3. Now choose “Upload”.
  4. Set your File Permissions to 755
  5. Click “Browse”
  6. Browse to the folder that has your script and click on it, and then click “Open”.

Note: Your cPanel may be setup differently than the one shown above but the overall concepts should still be the same.