How to Use Linux Command Line Crontab
History
The cron/crontab system came to life in version 7 of the AT&T UNIX. The only problem the original system had was that it demanded too much from the machine’s resources because it could only be run as the root user. So, the root user was having to handle all cron jobs for every user on the system. The second iteration of cron was changed so that the root user handled root processes and each user could install their own cron jobs. This was immediately successful and became the cron we know today.
How Cron Works
The cron system works by reading a crontab files found in various directories on the system. The following directories house root and system crontabs:
/etc
/etc/cron.d
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly
As the root user you can directly edit the /etc/crontab file, but using the crontab system is a much better way to create new cron jobs.
Once a cron job is created with the crontab system, that job will be executed at the precise time configured in the entry. Now this brings up an all-to-often confusing issue - time. The cron system looks at time like this:
Minute(0-59)
Hour (0-23)
Day of the month (1-31)
Month (1-12)
Day of the week (0-6 with Sunday being 0)
Where you need to configure a section of time, you would use the numerical value. If there was not a value to configure, an “*” would be put in its place. So if you needed to configure a cron job to be executed at 7:01 am on the th of October you would enter:
01 7 30 10 *
But that is only part of the entry. The next portion would be the command that needed to run. So, if you had a script you had written(we’ll call it script and place it in home directory of the user jlwallen) at this time the entry would look like:
01 7 30 10 * /home/jlwallen/script
Using crontab
As stated earlier, the root user can always manually edit the /etc/crontab file. But, the better way to add cron jobs (and the only way a standard user has of adding jobs) is to use the crontab tool. To invoke crontab, you only have to issue the command crontab from a command line prompt. There are three arguments to choose from: -e, -l, -r. If you issue the command crontab -e, you will be placed in edit mode where you will edit your user crontab with your chosen editor. (This will vary with what is on your system. I recommend Alpine.) The crontab -l command will list your current crontab entries. The crontab -r will remove all crontab entries from the user issuing the command.
So, if using the Alpine editor and you want to enter a cron job that will execute the /home/jlwallen/script command at 7:01 am Oct 30 you would issue the command crontab -e and then enter the following text:
01 7 30 10 * /home/jlwallen/script
Once the text is entered hit the Control key and the X key together to save the crontab file. You could then enter crontab -l which will return a listing of the crontab entries like so:
# m h dom mon dow command
01 7 30 10 * /home/jlwallen/script
Final Thoughts
Crontab is an incredibly useful tool to automate tasks in Linux. Once you get beyond understanding how cron sees time listings, you will have no problems creating crontab entries to make your administration life easier.
This post is part of the series: Linux Command Line
If you ever plan on doing any administration on a Linux machine, you would be well served to get to know the command line interface. In this Bright Hub series you will be introduced to various concepts surrounding one of the most powerful admin tools around.
- Linux Command Line: Introduction
- Linux Command Line: ls
- Linux Command Line: cd
- Linux Command Line: mkdir
- Linux Command Line: df
- Linux Command Line: ln
- Linux Command Line: top
- Linux Command Line: mount/umount
- Linux Command Line: Cron/Crontab
- Linux Command Line: chmod
- Linux Command Line: wget
- Linux Command Line: cat
- Linux Command Line: grep
- Linux Command Line: dd
- Linux Command Line: sudo
- Linux Command Line: startx
- Linux Command Line: adduser
- Linux Command Line: at
- Linux Command Line: aterm
- Linux Command Line: nano
- Linux Command Line: hostname