Affichage des articles dont le libellé est Cron. Afficher tous les articles
Affichage des articles dont le libellé est Cron. Afficher tous les articles

samedi 12 novembre 2016

Guest Post: Finding Your Feet with Cron Jobs



alt="Guest Post: Finding Your Feet with Cron Jobs" src="http://whsr.webrevenueinc1.netdna-cdn.com/wp-content/uploads/2015/04/default-image-500x308_c.jpg" />

What is so amazing about computers? Most will say that it’s their calculation power; others will state that it’s their amazing capabilities for 3-D applications, rendering, design and gaming.

I’d say it is the Internet and automation capabilities.

There are 1,7 billion Internet users today and this number grew 300% since 2002. A lot of these people have href="http://www.webhostingsecretrevealed.net/hosting-reviews/">web hosting accounts for their personal blogs, pages, forums, web sites, e-shops and so on. So if you are one of those people, think about how often you find yourself in need to repeat simple operations numerous times a week to keep your site clean, optimized and safe. If your answer is “often” and you are tired of this – here you’ll find a tip how to make your life a bit easier.

This article is about the cron – a unique automation utility for Unix based servers.

Cron daemon

To describe the concept of a cron job we should first of all consider a service running on a server. Most things executed on any computer are processes, browser being the most familiar one. If you open your browser and then go to your Task Manager, you will discover that it is running as a process.

Simply speaking process is an application, your computer is handling at the moment. The service, on the other hand, is a long running application, that is usually started automatically on system start up. It runs in the background, consuming as little resources as possible when it is not needed. When the time comes for it to perform an activity it “wakes up”, does what it must and then returns into sleeping mode? again. Examples of server processes are http service, MySQL service, ftp service and so on. On Unix servers (i.e. servers powered by Unix, Linux or FreeBSD) such services are usually called daemons.

The Cron Daemon is a special service capable of executing tasks at specific points in time without user’s intervention or control. It can be called an automatic scheduling machine. The name href="http://en.wikipedia.org/wiki/Cron">cron comes from the word title="Chronograph" href="http://en.wikipedia.org/wiki/Chronograph">chronograph . By using cron scripts, you can create sets of commands you want executed on a scheduled basis automatically.

Crontab and commands

Cron commands are stored in a special file. It is called crontab(which stands for cron table). The list of commands you can execute manually can be performed with a cron as well. Normally cron tasks include the following:

  • making backups;
  • any kind of file operations (deleting, copying, moving, renaming etc.);
  • sending and receiving updates emails;
  • installing software updates;
  • checking links on other websites;
  • optimize databases;
  • many more;

The mentioned crontab is a file that usually has the following content:

01 * * * * interpreter-1 script_path-1
02 4 * * * interpreter-2 script_path-2
22 4 * * 0 interpreter-3 script_path-3
42 4 1 * * interpreter-4 script_path-4

In the example above the first 5 items in each line are the time scheduling parameters. First number stands for the minute field, second – for the hour field, third – for the day field, fourth – for the month field, fifth – for the weekday field. The possible values and field descriptions are shown in the table below:

class="border" width="560" cellpadding="2">bgcolor="#dddddd" width="101">Fieldbgcolor="#dddddd" width="93">Valuebgcolor="#dddddd" width="356">Descriptionvalign="top" width="101">minutevalign="top" width="93">0-59valign="top" width="356">The exact minute that the command is executed.valign="top" width="101">hourvalign="top" width="93">0-23valign="top" width="356">The hour of the day that the command is executed.valign="top" width="101">dayvalign="top" width="93">1-31valign="top" width="356">The day of the month that the command is executed.valign="top" width="101">monthvalign="top" width="93">1-12valign="top" width="356">The month of the year that the command is executed.valign="top" width="101">weekdayvalign="top" width="93">0-6valign="top" width="356">The day of the week that the command is executed. Sunday=0, Monday = 1, Tuesday = 2, and so forth.

Working with precise time and dates may be somewhat uncomfortable. Therefore, a special syntax is introduced: “*” – stands for any (so called wildcard symbol). If you want to configure the script to run at 15th minute of every hour you simply use the following sequence:

15 * * * *

If you want the script to run every 15 minutes you use the sequence like this:

15/* * * * *

The interpreter item in this example defines the shell or program you want to run the script with. For example, to a run a regular shell script daily with the bash shell, you use the following line:

* * 1/* * * /bin/bash /scripts/script1.sh

Here “/scripts/script1.sh” (script path from the cron tab file example) is an absolute path to the file your script is located in.

To edit crontab, use the command “crontab –e“in console. Editing crontabs in the console as well as using console itself is usually very inconvenient for regular users. Therefore, almost every hosting control panel contains a graphical user interface (GUI) for working with crontabs and configuring crons.

Crontab GUIs

The number of hosting control panels is quite large today, but there are a few most widely used by hosting companies due to their friendly interface and good integration with operating systems. Let’s take a look at how to configure the crontab in the two very popular panels: cPanel and DirectAdmin.

The GUIs of these two panels use the same concept as a regular cron table file. The time scheduling parameters remain the same, as well as their syntax. To specify months, days, hours and minutes, you still have to use numbers and the approach described earlier in the text. The interpreter and script path are defined in a separate field in these GUIs. This is also true for the email address for sending logs (available only for cPanel, while DirectAdmin will send the logs to the default e-mail address of the user). If you want no logs sent at all, you need to add the following command after the script path: “>/dev/null 2>&1“. DirectAdmin’s panel has a special button for this, as shown on the screenshots below.

cPanel Cron Job Menu

To get to the crontab GUI, find the Cron Job button in the home of your href="http://www.cpanel.net/">cPanel:
class="border" alt="cPanel " src="http://www.webhostingsecretrevealed.net/images/2010/0303-1.jpg" width="750" />
style="font-size: xx-small;">Figure 1: cPanel “Cron jobs” menu icon

class="border" alt="cPanel Cron Jobs menu fields" src="http://www.webhostingsecretrevealed.net/images/2010/0303-2.jpg" width="750" />
style="font-size: xx-small;">Figure 2: cPanel Cron Jobs menu fields

DirectAdmin Cron Job Menu

Locate the Conjobs button to enter the href="http://www.directadmin.com/">DirectAdmin crontab configuration GUI:

class="border" alt="DirectAdmin CronJobs" src="http://www.webhostingsecretrevealed.net/images/2010/0303-3.jpg" width="750" />
style="font-size: xx-small;">Figure 3: DirectAdmin Cronjobs menu

Cron scripts

When you know how to correctly configure the crontab scheduling, all you need to do to run a script. You can either write one yourself, using a regular text editor (like notepad on Windows), or you can search for it on the web. What’s good about cron scripts is that they can easily be modified and can be used with almost any software installed on your server. Writing your own scripts requires knowledge of the commands of the specific interpreter you are planning to use; thereby, before searching for specific instructions, decide which interpreter you’ll use. C shell is the most widely used interpreter, so if you are interested in creating your own scripts, then the best place to start is href="http://www.freeos.com/guides/lsst/">Linux Shell Scripting Tutorial.

Bottom Line

Automating tasks performed for a hosting account can save you a lot of time and ensure the safety of hosted data. One of the best and easiest ways to schedule autonomous execution of various actions is to use the cron job utility available on most Unix, Linux and FreeBSD servers.

Great advantage of cron jobs is the fact that tasks are performed without any intervention from the user’s side. Distinctive features of cron jobs include easiness of configuration, solid execution reliability and ability to use any installed interpreter for scripts. Additional comfort comes with the use of hosting control panels, as they provide GUIs for scheduling tasks. For more information on crons check the crontab href="http://linuxmanpages.com/man5/crontab.5.php">manual page.

About Sergey Smirnov.

Smirnovi4 graduated from Ukrainian National Aerospace University in 2010 with Master’s degree in “Propulsion systems and power installations of spacecrafts”. Currently he enjoys working as a writer for the href="http://svhostingblog.com/">SiteValley official blog and occasionally assists with marketing tasks. Author likes extreme sports (skateboarding, snowboarding, parachuting and scuba diving particularly). Music of his choice is punk and indie rock. His free time he dedicates to reading news of IT industry and music. Two of Smirnovi4’s greatest weaknesses are Dr. Pepper and flight simulators.


Page 29 – Web Hosting Secret Revealed




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



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.

class="border" width="100%" cellspacing="0" cellpadding="1">bgcolor="#DDDDDD" width="200"> Crontab filename bgcolor="#DDDDDD">Install filename as your crontab file.valign="top" width="200">crontab -evalign="top">Edit your crontab file.valign="top" width="200">crontab -lvalign="top">Show your crontab file.valign="top" width="200">crontab -rvalign="top">Remove your crontab file.valign="top" width="200">MAILTO=user@domain.comvalign="top">Emails 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)

class="border" width="100%" cellspacing="0" cellpadding="1">bgcolor="#dddddd" width="100"> Fieldbgcolor="#dddddd" width="100">Valuebgcolor="#dddddd">Descriptionvalign="top" width="100">Minutevalign="top" width="100">0-59valign="top">Defines the exact minute the command will execute.valign="top" width="100">Hourvalign="top" width="100">0-23valign="top">Defines the hour of the day the command will execute.valign="top" width="100">Dayvalign="top" width="100">1-31valign="top">Defines the day of the month the command will execute.valign="top" width="100">Monthvalign="top" width="100">1-12valign="top">Defines the month of the year the command will execute.valign="top" width="100">Weekdayvalign="top" width="100">0-6valign="top">Defines the day of the week the command will execute.
Sunday=0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6valign="top" width="100">Commandvalign="top" width="100">Specialvalign="top">The 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 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 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 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="" />

  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.


Page 26 – Web Hosting Secret Revealed




Mastering the Cron Job and Automating Basic Server Tasks



alt="Mastering the Cron Job and Automating Basic Server Tasks" src="http://whsr.webrevenueinc1.netdna-cdn.com/wp-content/uploads/2015/04/default-image-500x308_c.jpg" />

Web hosting is designed to be easy, straightforward, and perfect for the professional who’s on the go and simply cannot dedicate every hour of every day to managing their server software and associated tasks. To that end, almost every server based on Unix or Linux offers an automated task manager known as the "href="http://www.wisegeek.com/what-is-a-cron-job.htm" target="_blank">Cron Job" or "Crontab." This software utility is much like the task scheduler in Microsoft Windows, in that it can be told to do certain things at certain times, saving a user from performing these tasks manually. This is especially important when one considers that many routine server maintenance, backup, and communications procedures can take hours and hours to complete. Most people simply want to go home after a day in the office and relax, rather than spend their evening backing up files and managing contacts.

Those people are in luck, as every task can be scheduled on a daily, weekly, or monthly basis — or even further apart, in some cases, and specific instructions can be given to the server within the Crontab utility on how to perform the task, how to know when it’s complete, and what to do when the task at hand has actually been finished. Users will be able to relax while their server does all of the heavy lifting for hours; or they can simply schedule the work to be completed while they’re hard at work in their own offices. Because it’s automated, a Cron job can occur at virtually any hour of the day.

Learning to Tell Time Using a Cron Job’s Format

src="http://www.webhostingsecretrevealed.com/images/2012/0412-1.jpg" class="border" />

A Cron job isn’t exactly an easy thing to master; in fact, it’s widely considered one of the most advanced configurations available on a standard href="http://gizbuzz.co.uk/2006/linux-explained/">Linux or href="http://staff.washington.edu/corey/unix-intro+man.html">Unix web server. This is partially because the language used to program these jobs is so archaic and, in some cases, completely backwards. When it comes to telling a certain time to the Cron job or Cron tab in question, things are certainly backwards, upside down, and a little confusing.

The format for telling time via a Cron job is such:

MINUTE HOUR DAY-OF-MONTH MONTH DAY-OF-WEEK COMMAND

It’s all one line, and even number and command is side-by-side in one uniform contraction. It’s enough to make most developers and server operators wince and, in fact, many of them do until they get a hang for developing an efficient Cron job. It’s important to note that every aspect of the Cron job’s time is numerical; there are no month names day names, or other words used throughout the development of the time during which a task should be performed.

Therefore, let’s set a Cron job up for 10:30 a.m. on July 7th in order to get a feel for how a Cron job’s time looks when it’s turned into strictly numbers.

30 10 07 07 *

The example above states that the job should be completed at the 30th minute of the tenth hour on the seventh day of the seventh month. All numbers are two digits long, even when the month or day is only a single digit. This is important to remember, as a single digit will often cause the Cron job to be invalid and it simply will not be performed, ever. At the end of the con structure, an asterisk indicates that the job should be performed on any day of the week. This is important, as setting it to 03 for Wednesday would mean that the task would only be performed on July 7th if that day was a Tuesday. That’s likely to happen once every seven or eight years, which is a little absorb for most developers to consider.

Another important consideration to make when setting up a Cron job is that the hour format is 24-hour military time rather than 12-hour civilian time. To change the time of Cron job to 10 p.m., the hour would be changed to 22 instead of the current 10.

src="http://www.webhostingsecretrevealed.com/images/2012/0412-2.jpg" alt="Cron Job Examples" class="border" />

Finally, if a user merely wants to complete a task on a daily, monthly, or even yearly basis, they can skip the process of setting a specific time entirely. Instead, the Cron job process provides for simply using variables which determine when a job is done on these frequent intervals. These include:

  • @daily
  • @monthly
  • @yearly

Because the time cannot be strictly controlled and determined using hours and minutes, these jobs will happen at exactly midnight, according to the server’s internal time, at the interval requested. That means that the @monthly interval will occur at exactly midnight on the first day of every month. The @yearly interval will occur at exactly midnight on the first day of every year; and the @daily interval will occur at exactly midnight every single day of the year.

This is far easier than setting a specific date, time, and day of the week, but certain tasks being performed at midnight on the first day of the month or year may have some drawbacks for certain customers. Always keep the needs of both administrators and site visitors in mind when scheduling tasks that occur at exactly midnight.

Grasping the "COMMAND" Variable and What to Do with It

As can be seen in the example above, constructing an actual Cron job is relatively easy. The date must be first defined and then the function of the Cron job is immediately defined afterward. That function can be literally anything, including running a PHP script or running a customized backup script that stores the server’s files and data in either a remote or local file. For clarification, we’ll build on the example that was previously used and instruct the Cron job to run a PHP script on July 7th at 10:30 in the morning. This PHP script will be called "backup.php" and we’ll assume that the PHP file is a full backup script that gathers, compresses, and stores site files on the seventh of the month when instructed to do so by the server. Here is what it looks like:

30 10 07 07 * http://your-domain-name.com/backup-scripts/backup.php

When this Cron job is entered into the list of the server’s Cron tabs, it will be automatically executed at 10:30 a.m. every July 7th of every year. It will run the PHP backup script that is located in the "backup-scripts" directory, and that’s where the true genius of the Cron job setup works.

Instead of requiring advanced commands of its users to perform things like site backups and cache flushes, it simply gives users the ability to execute existing scripts at a specified interval or on a specific date at a specific time. This means no advanced programming knowledge is needed beyond what the user already knows. PHP is perfectly capable of backup site files, as are much more advanced files and programming languages that the more sophisticated user might develop for their Linux server.

This simple setup can be used to do virtually anything, then, so long as a pre-written script provides for the execution of those actions independent of user input. This does mean that any script executed by a Cron job within a Cron tab must be fully automated and able to perform on its own, however. For example, it would be impossible (and simply illogical) to tell the Cron job to execute a WordPress index or theme file every day at the same time. There are simply no actions or automated processes defined and, while the Cron job would certainly execute the file, it would do nothing and would sit static until user input was provided in another way.

For this reason, if coding or downloading backup scripts or others to work with Cron jobs, always make sure that they require exactly zero user input to perform their functions successfully. An automated task scheduler must be paired with an automated process within the file it executes. There are no exceptions to this rule.

Mastering the Overall Cron Tab File on a Typical Server

Every specific Cron job which is specified for execution lies within a larger file which is known as a Cron tab. Some servers have multiple Cron tabs for multiple types of applications and automated inputs, but this is rare and largely reserved for the most advance server operators and owners. Those who have just one Cron tab file can use the commands below to edit, delete, or view the file in its entirety, with each of their specific automated tasks listed for viewing within the file.

crontab -r

This command removes (thus "r") or deletes the entire Cron tab file itself. This will effectively purge it of all commands and automated scripts, and restore it to a blank file that can be rebuilt. This is a suitable option for those who have managed to corrupt the file or somehow misappropriate tasks and times. Sometimes, it’s simply easier to start all over.

crontab -e

In this case, "e" stands for "edit." Those users who want to edit the functions described in a Cron tab rather than delete the file entirely can use this command to be taken to the command-line editor which will allow them to add new tasks, remove old ones, or change the applicable schedule times for each of the Cron jobs listed within the Cron tab document.

crontab -l

In this case, it’s easy enough to remember by associating the "L" with "look. This command allows a server administrator to simply view the full contents of their Cron tab file without removing it from the server entirely and without having the ability to edit the contents. This read-only display of Cron tab content is perfect for remembering which tasks are scheduled for which times, and for verifying the integrity of the file itself.

Why it’s Important to Master the Cron Job an Cron Tab Automation Files

id='floatright'>src="http://www.webhostingsecretrevealed.com/images/2012/0412-3.jpg" alt="Cron Job Examples" />

Generally speaking, the only thing automated about a server is the spinning of its hard disk and the performance of its hardware features. Beyond that, though, the server must the trained and instructed to do routine and extraordinary tasks that go above and beyond merely displaying a software control panel or upgrading the installation of PHP or Perl that a user has placed on the hard disk.

It’s important to remember that one of the most essential functions a server can execute on a regular basis is the creation of a site backup of data and settings. There is virtually no way to automate this process without a Cron job and, due to the nature of the internet and all of the malicious visitors which pass through a site daily, failure to automate this process on a daily or weekly basis could result in significant and catastrophic data loss.

Above and beyond that, though, there are a number of tasks that should be automated using the Cron job process. These tasks include purging any site caches which might display outdated images or printed content to site visitors; it also includes deleting old files, cleaning up old directories and images, and making sure that everything stored on a server’s hard disk drive is current and uncorrupted.

Just like a healthy personal computer automatically schedules disk maintenance, antivirus and malware scans, software updates, and file deletions, a healthy server must be configured to take care of itself and remain in good standing. Otherwise, it becomes vulnerable to crashes, hacking attempts, and data loss that will lead to lost profits, advertising, content creation, and even search engine ranking status.

Easy to Learn and Easier to Deploy

Creating a Cron job within a standard Cron tab is one of the easiest things a server administrator can do. The process lies within the standard command line and breaks down every segment of time into a two-digit code. Because it requires no additional knowledge of any new programming language in order to automate tasks, the service essentially builds on the existing programming and server operations knowledge that an administrator already possesses. With this being the case, there is simply no reason not to begin automating essential server functions and ensuring the integrity of site data and operations.

For further reference and more code samples, do check out Robert’s previous post: href="http://www.webhostingsecretrevealed.com/web-hosting-knowledge/how-to-save-time-with-cron-basic-guide-and-sample-codes/">How to save time with cron: Basic guide and sample codes as well as visiting this site: href="http://livecronjobs.com/" target="_blank">Live Cron Jobs.


Page 24 – Web Hosting Secret Revealed