Monthly Archives: February 2010

crontab syntax: Using the crontab in Linux

Here is my overview for: crontab sytanx, crontab and cronjobs…

Alright so you have a script that you want to automate, you will need to know a little about crontab syntax. To do so you will also need to understand a few things about the Linux crontab and cronjob. I will give a very high level explanation regarding crontab syntax, crontab and cronjob.  I have only tested this method on REDHAT based systems (REDHAT, CENTOS and FEDORA CORE). You will also require experience with the text editor program “vi.” You will use the following commands:

i = insert, this allows you to edit the crontab
esc = removes you from insert mode,
:w! = save changes
:q! = quit without saving changes (if any were made)
:wq! = “w” tells the editor that you want to “write” and “quit” (w = write, q = quit), “!” execute the command forcefully

Each user has a crontab, in the crontab you can schedule cronjob’s. The cronjob’s can run at multiple times through out the day, week, month and year. To list your current crontab type the following:

[yourusername@server ~]$ crontab -l
no crontab for yourusername

The above message indicates that I have no scheduled cronjob’s in my crontab. If I want to automate a script I would have to add a cronjob to the crontab. To add a cronjob type the following at the command line: (you will need to use commands from the text editor “vi”)

[yourusername@server ~]$ crontab -e

The above code will open the crontab for editing, you can now create a cronjob. Below is a break down of how the crontab works – an overview of how you can schedule automation:

*     *   *  *   *  command to be executed
–     –    –   –  –
|     |     |   |    |
|     |     |   |    +—– day of week (0 – 6) (Sunday=0)
|     |     |   +——- month (1 – 12)
|     |     +——— day of month (1 – 31)
|     +———– hour (0 – 23)
+————- min (0 – 59)

Now what the above means is that you can schedule your cronjob by minute/hour/day-of-month/month/day-of-week. So if I wanted to schedule my “backup.bsh” script to run every day at 7PM I would enter the following:

0 19 * * * /home/yourusername/bin/backup.bsh

Once I have entered the above cronjob I can save my changes and exit. The cronjob is now setup to run via the crontab every night at 7PM. If I saved the crontab properly and I do a listing of my crontabs cronjobs I should see:

[yourusername@server ~]$ crontab -l
19 0 * * * /home/yourusername/bin/backup.bsh

If you want you can add comments to the crontab, this will help you understand what each cronjob is doing. To add a comment run the “crontab -e” command again and place the comment above the cronjob, like so:

[yourusername@server ~]$ crontab -l
# This script will run at 7PM every day, it runs backup.bsh
19 0 * * * /home/yourusername/bin/backup.bsh

There now we are done! As always if you have questions or feel as though I missed something please leave a comment, I would be happy to learn of different and even better ways to use the crontab and cronjob.