Skip to content


Cron Jobs


What is cron?

Actually its called ‘crond daemon’. Cron is an automatic task/scheduler mechanism. You will use it on your Unix or Linux operating system for doing some tasks at specific intervals. If you set or schedule any task and forget the task. The cron daemon will run that task for you.

What is cron tab?

Cron tab is a text file that contains a Cron Job or your scheduled applications information.

On most of Linux distributions crond is automatically installed and entered into the start up scripts. To find out if it’s running do the following command to check crond service is running or not:-

[root@p-root]$ ps aux | grep crond

root 331 0.0 0.7 1124 123 ? s Nov11 0:00 crond

p-root 8660 3.9 2.1 1132 345 tty1 s 11:11 0:00 grep crond

The top line shows that crond is running, and the bottom line is the search we just run.

Crontab file contain following informations :-

1: minutes (Value is 00 – 59. exact minute the cron executes)
2: hour (Value is 00 – 23. hour of the day the cron executes. 0 means midnight)
3: day (Value is 01 – 31. day of the month the cron executes)
4: month (Value is 01 – 12. month of the year the cron executes)
5: weekday (Value is 00 – 06. day of the week the cron executes. Sunday=0,Monday=1…)

field    allowed values
           -----        --------------
           minute       0-59
           hour         0-23
           day of month  1-31
           month        1-12 (or names, see below)
           day of week  0-7 (0 or 7 is Sun, or use names)

Examples :-

If you have installed a script in your public_html/ directory called mail.sh.

If you want to run this file each night at 11.30 PM every day:

30 23 * * * /home/username/public_html/mail.sh
30 – represents the minute of cron work
23 – represents the hour of the day
The * represent every day, month, and weekday.

If you want to set the cron job every sunday at midnight 11.30 PM:

30 23 * * 0 /home/username/public_html/mail.sh
0 – represents the Sunday.

If you want the cron job to run at 1:00 and 2:00 A.M:

* 1,2 * * * /home/username/public_html/mail.sh

This runs your cron at 1A.M and 2A.M every day, every month and every week.

If you want to run the above task only from Monday to Friday and at 11.30 P.M:

30 23 * * 1-5 /home/username/public_html/mail.sh

This runs your cron from Monday to Friday and everyday at 11.30 PM.

Multiuser cron :-

As Linux or Unix is a multiuser OS, Some of the applications have to be able to support multiple users, cron is one of these. Each user can have their own crontab file. Which can be created/edited/removed by the command crontab. This command creates an individual crontab file and although this is a text file, as the /etc/crontab is, it should not be edited directly. The crontab file is often stored in /var/spool/cron/crontabs/username (Unix/Slackware etc..) , and /var/spool/cron/username (Redhat , CentOS, Fedora) . But might be kept elsewhere depending on what flavor you are using.

To edit or create your crontab file, use command crontab -e and this will open in editor.

Cron Commands:

crontab filename Install filename as your crontab file

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.

For more information please check man page of cron.

====================================================================

Enjoy Linux !!! J

Posted in Linux tutorials.

Tagged with , , , , .


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.