Page 1 of 1

Using Cron how to use cron to run commands/scripts on certain times Rate Topic: -----

#1 Anarion   User is offline

  • The Persian Coder
  • member icon

Reputation: 387
  • View blog
  • Posts: 1,663
  • Joined: 16-May 09

Post icon  Posted 12 December 2009 - 05:30 AM

Definition of Cron
Cron is the name of program that enables unix users to execute commands or scripts (groups of commands) automatically at a specified time/date. A common use for cron today is connecting to the internet and downloading your email.

In this tutorial we will work with Vixie Cron, a version of cron authored by Paul Vixie.

You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use
crontab if your name does not appear in the file /usr/lib/cron/cron.deny.
If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab. The allow/deny files consist of one user name per line.

How to Start Cron
Cron is a daemon, meaning it needs to be run once. The cron daemon, or crond, stays dormant until a time specified in one of the config files, or crontabs.
On most Linux distributions crond is automatically installed and entered into the start up scripts. To find out if it's running do the following:
ps -e | grep cron
for me the output is: 4031 ? 00:00:00 cron which indicates my cron daemon is running. For you, If it's not running then either you killed it since the last time you rebooted, or it wasn't started at all. To fix this, simply add the line cron to one of your startup scripts and it will be run from the next reboot; until that time, issue cron to run it now.

When you edit the configuration files of cron, it may be reloaded in the next boot in some cron versions, but Vixie Cron reloads the files after they have been edited.

Using Cron
There are a few different ways to use cron. In the /etc directory you will probably find some sub directories called cron.hourly, cron.daily, cron.weekly and cron.monthly. If you place a script into one of those directories it will be run either hourly, daily, weekly or monthly, depending on the name of the directory.
If you want more flexibility than this, you can edit a crontab (the name for cron's config files). The main config file is normally /etc/crontab. A crontab looks something like this:
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.


# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

the SHELL part tells which shell cron will run under.
PATH contains the directories which will be in the search path for cron.
Now for the more complicated second part of a crontab file. An entry in cron is made up of a series of fields. There are normally seven fields in one entry. The fields are:
minute hour dom month dow user cmd
minute: what minute of the hour the command will be run on, between 0 and 59
hour: what hour the command will be run on, between 0 and 23 (meaning it's in the 24hour clock format)
dom: what day of the month the command will be run on, for example, 19 means on the 19th day of the month
month: the month the command will be run on, can be specified numerical (0-12) or with the name
dow: the day of week the command will be run on, can be numeric (0-7) or name of the day (e.g. sun).
user: the user who runs the command
cmd: the command itself to be run on the specified date/time
Note: - place a * for any value you don't want to specify
- Under dow 0 and 7 are both Sunday.
- If both the dom and dow are specified, the command will be executed when either of the events happen.

Vixie Cron also accepts lists in the fields. Lists can be in the form, 1,2,3 (meaning 1 and 2 and 3) or 1-3 (the same meaning). OK, now look at this sample line for crontab:
0 21 28 * * root mybackupscript
This tells: run the "mybackupscript" at hour 21:00, on 28th day of all months as root user :D simple, not? Just note that If both the dom and dow are specified, the command will be executed when either of the events happen.

Cron also supports step values, for example */5 as the value for minute, means every 5 minutes. I love this feature of cron so much :wub:.
Now let's see another example using steps: * 12 10-16/2 * * root backup
This one will exactly do the same as * 12 10,12,14,16 * * root backup. Amazed, right? :rolleyes:

Final note in this section of tutorial: When using the names of weekdays or months, it isn't case sensitive, but only the first three letters should be used, e.g. Mon, sun or Mar, jul.

Multi-User Crontabs
In addition to editing the main crontab file located at /etc/crontab, you can have a separate crontab for each user. To create/edit your personal crontab file, use the command crontab -e which brings up the default editor for editing the crontab file. If you are like me and prefer your own editor for using, first, issue export EDITOR=vim ( :wub: me lovez vim :wub: ). This tells to use the vim editor instead. Now execute crontab -e which brings a vim instance for editing your personal crontab file.
*Note* in these kindof crontabs there is no place to write which user runs the specified command, because it is the file's owner ofcourse.

And finally, to list your crontabs, issue crontab -l.
This was the tutorial on using cron and crontab files. Have a nice day running commands at specified times!!! ;)

Is This A Good Question/Topic? 0
  • +

Page 1 of 1