alt="How to save time with cron: Basic guide and sample codes" src="http://whsr.webrevenueinc1.netdna-cdn.com/wp-content/uploads/2015/04/default-image-500x308_c.jpg" />
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.
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)
Sunday=0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6
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 href="http://tamba2.org.uk/wordpress/cron/" target="_blank">T2)
#Set the 4 variables
#Replace what is AFTER the = with the information from your wp-config.php fileDBNAME=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 href="https://help.ubuntu.com/8.04/serverguide/C/backup-shellscripts.html" target="_blank">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).
class="border" src="http://www.webhostingsecretrevealed.com/images/2011/1116-1.jpg" alt="" />
3. Enter your E-Mail address if you would like a copy of the crop output to be emailed to you.
class="border" src="http://www.webhostingsecretrevealed.com/images/2011/1116-2.jpg" alt="" />
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.)
class="border" src="http://www.webhostingsecretrevealed.com/images/2011/1116-3.jpg" alt="" />
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.)
class="border" src="http://www.webhostingsecretrevealed.com/images/2011/1116-4.jpg" alt="" />
6. Click “Add New Cron Job”
class="border" src="http://www.webhostingsecretrevealed.com/images/2011/1116-5.jpg" alt="" />
7. Your cron job should now be listed under “Current Cron Jobs”.
class="border" src="http://www.webhostingsecretrevealed.com/images/2011/1116-6.jpg" alt="" />
How do I upload my script file?
class="border" src="http://www.webhostingsecretrevealed.com/images/2011/1116-7.jpg" alt="" />
- From your cPanel choose “File Manager”
- Next choose “Home Directory” then click “Go”
- Now choose “Upload”.
- Set your File Permissions to 755
- Click “Browse”
- 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.
Page 26 – Web Hosting Secret Revealed
Aucun commentaire:
Enregistrer un commentaire