Posts Tagged ‘time based scheduling program’

Crons — A Simple Tool To Automate Your Business

Sunday, March 16th, 2008

How Crons can be an asset to  your business.

What is Cron?

Cron is the name of a time based scheduling program that enables Unix users to execute scripts, or commands, automatically at a specified time/date. It is generally used for system administration commands, for running a backup script, or for connecting to the internet and downloading your email.  Now you’re interested, right?  Email is something that almost everyone, certainly every business owner, can relate to. 

So how can you use Crons? 

Quite simply, If you want to automate something on your computer that runs more than once, you can use a cron.

But wait, we’re getting ahead of ourselves.  Let’s take a step back and examine exactly what a cron is. 

A cron is a daemon, yep another tech term – sorry.  A daemon is a program that only needs to be started once and then it will sit quietly waiting until it is required.  Most Linux distributions, versions of Linux designed from the original Linux base code called a kernel, come with Crons automatically installed and entered into the startup script. 

For example, you can write a cron to delete your temporary files.  You can designate it to delete them hourly, daily, weekly, or monthly. 

Even more useful for an internet business owner might be the ability send email messages to subscribers every morning at 5:00am.  You can set the cron job on your server to handle that every day.  You can even set your cron to send one email every day until all your email messages have been delivered.  You can schedule it to delete your website members with expired accounts, to receive an update on your subscribers from your mailing list manager and you can investigate your links on other websites if participate in link exchange programs.  Pretty useful, right? 

Another term you’re likely to come upon as you get into crons is the term Cron tab.  This is basically the command that creates the list of commands on your system and is not editable.  However, the crontab file, I know, the creators of the terminology must get a good laugh from confusing folks, the cron tab file is the actual list of commands and that is capable of being edited with a few commands.  Each user can have their own cron tab file. 

What does a Cron Look like?

The good news is that it is fairly simple.  You have a few fields you will use including:

Minute – denotes the minute of the hour that the command will run, anything between 0 and 59.

Hour – denotes the hour the command will run on and will be between 0 for midnight and 23 for 11pm.

Dom – this is the day of the month you want your command to run on.  For example the first day of the month would be denoted as 1.

Month – what month do you want to run the command, choices are 0-12 or you can write the name of the month out.  For example, October

Dow – stands for day of the week and you can choose 0-7 or write the day as it is normally abbreviated.  Example Sat would mean Saturday

User – is the person that runs the command.  You can specify a user but you do not have to.  You can also deny a user from using a cron with a simply deny command.

Cmd – is the command that you want to run.

Here’s what a typical cron command will look like.
 
Let’s imagine that you have installed a cgi script called task.cgi in your cgi-bin directory.  If you want to run this program each morning at 5:15am on Sunday, here’s what it will look like:

15 5 * * 0 /home/username/www/cgi-bin/task.cgi

30–represents the minute of cron work
23–represents the hour of the day
0–represents the Sunday.
The * represent every day, month, and weekday.

Scenarios you may face

Many distributions offer pre-configured cron jobs.  Different versions of UNIX store cron and support files in different locations and may use a slightly different format however if you have a Linux system you may be able to view these with the following commands:

• /var/spool/cron               main cron directory
• /var/spool/cron/cron.allow    grant access to the cron facility
• /var/spool/cron/cron.deny     revoke access to the cron facility

To set up a cron job your system may have a cron tab function on your web panel.  This is a simple interface that will allow you to click on a few buttons and set your cron job. 

The last option is that you will have to upload a cron job into your ‘cron.txt’ file.  There are four basic steps involved and they include Create cron.txt using your notepad application, uploading it using your telnet server, installing the txt file as cron file with the command ‘crontab cron.txt’, and then checking it to see if it works. 

Tips in setting up your cron jobs include:

• Occasionally check your cron to make sure if it is working.  For example you set your cron to send your ezine at 11.30 PM. Then you subscribe yourself to see it is working or not. Later you can unsubscribe yourself.

• If you are creating cron file first time and uploading it to your server, do it in ASCII mode.

• If you are setting the script path in your schedule, the path SHOULD be from your hosts server root. NOT your domain path.  Ask your host about the procedure if you are not sure of something.

• Script names are case sensitive on Unix. So be careful in entering the script name.
If you are creating cron.txt file and uploading, you should not allow spaces between them however you should have a space after your command.

Wrapping it up

It is easy to get excited about the possibilities of crons and automating your business and computer to work while you sleep without the extra cost of additional software.  Before you get carried away take the time to understand your server’s set up and capabilities.  Some systems are significantly more intuitive and programming crons is easy.  Others will require more attention and a longer learning curve.   This article merely scratches the surface of what crons are capable of and how to implement them. 

Share