News: 24 November 2018 - TP Docs is now updated to Version 1.6.1...!

Docs Navigation

Search Articles


Search in titles
Search in article texts





Author Topic: What Is A Cron Job?  (Read 7421 times)

Offline Xarcell

  • Full Member
  • ***
  • Posts: 137
What Is A Cron Job?
« on: April 25, 2008, 04:13:17 AM »
What is cron?

Actually it is called 'cron daemon'. Cron is an automatic task machine. You will use it on your Unix or Linux operating systems for doing some tasks at specific intervals with out your intervention every time. You set the clock and forget. The cron daemon runs the work for you.

What is cron tab?

'Cron tab(CRON TABLE)' is a text file that contains a series of cron functions.

What cron will do for you?

If you want to send your email sources to your subscribers at 11.30 night, you will set the cron job on your server.And your cron manager sends the one email every day at 11.30 until all the emails will be finished.

If you want to send them on Sundays, you can schedule it with your cron.

You can schedule it to delete your website members with expired accounts.

You can schedule it to receive an update on your subscribers from your mailing list manager.

How Do I create a Cron?
First you will need telnet/ssh access. I reccommend using PuTTY telnet or OpenSSH.

Once logged on, here are a few basic commands along with their meanings that you can use.

  • crontab -e
    Edit your crontab file, or create one if it doesn't already exist.
  • crontab -l
    Display your crontab file.
  • crontab -r
    Remove your crontab file.
  • crontab -v
    Display the last time you edited your crontab file. (This option is only available on a few systems.)


OPERATORS :
There are several ways of specifying multiple values in a field:

The comma (',') operator specifies a list of values, for example: "1,3,4,7,8"
The dash ('-') operator specifies a range of values, for example: "1-6", which is equivalent to "1,2,3,4,5,6"
The asterisk ('*') operator specifies all possible values for a field. For example, an asterisk in the hour time field would be equivalent to 'every hour'.
The slash ('/') operator can be used to skip a given number of values. For example, "*/3" means to skip to every third value. So for example "*/3" in the hour time field is equivalent to "0,3,6,9,12,15,18,21"; "*" specifies 'every hour' but the "/3" means that only the first, fourth, seventh...and such values given by "*" are used.

FIELDS:
The first five fields of the line are the date and time field which specify how frequently and when to execute a command.

Field no.DescriptionPermitted values 
1 minute0-59
2 hour0-23
3day of the month 1-31
4month1-12
5day of the week 0-7


* For day of the week, both 0 and 7 are considered Sunday. The time is based on that of the server running cron.

The sixth and subsequent fields (i.e., the rest of the line) specify the command to be run.

OUTPUT:
The output from commands is determined by the last entries on the line. By default (i.e. if you don't specify anything at all) regular and error output will be e-mailed to you. According to "man cron," when executing commands, any output is mailed to the owner of the crontab (or to the user named in the MAILTO environment variable in the crontab, if such exists). The MAILTO variable may be set in the crontab file.

You can redirect the standard output and the errors wherever you like by using ">". When you use ">" without a number before it, it defaults to "1>", and this the 1st output is the standard (non-error) output. When you use "2>" you are saying what to do with the error output. So, for example, ">/my_file.txt" would redirect standard output to a file called "my_file.txt", and "2>/my_errors.txt" would redirect the errors to a file called "my_errors.txt".


Examples Start


Example 1
:
This will run a command at 4:10pm, and email you the regular and error output:
Code: [Select]
10 16 * * * /home/someone/bin/mycoolscript.pl
Example 2:
This will run a command at 2:00am on Saturday, and only email you errors:
Code: [Select]
0 2 * * 6 /home/me/weekly/weekly-pruning.sh >/dev/null
Example 3:
This will run at midnight on newyears, and not tell you anything:
Code: [Select]
0 0 1 1 0 /home/you/happy.new.years.pyc >/dev/null 2>&1
In that example I used a special redirection. '2>&1' sends the second output (error output, stderr) to the same place that regular output goes (standard out, stdout). In place of that you could have given anything, such as a error log: 2>/home/me/cron.errors.log, or the bit bucket again: 2>/dev/null.

* It may be necessary for you to use two digit minute and hour fields. One user reported as having to do this to get cron to accept the entry. For example:
Code: [Select]
01 02 * * * perl /home/me/awstats
Example 4:
This will run a php page called cron.php every hour:
Code: [Select]
0 * * * * wget http://www.somedrupaldomain.com/cron.php
Example 5:
This will run a script every 15 minutes:
Code: [Select]
*/15 * * * * /usr/local/bin/perl /home/myuser/mydomain.com/myscript.pl

Examples End




Doc Written By: Xarcell
« Last Edit: August 16, 2018, 02:15:55 PM by illori »