Log In | Register | January 30, 2023


Linux - October 19, 2011

Linux CronTabs Tutorial

Setting up linux crontabs is alot easier then it appears. In order to setup linux crontabs successfully you need to understand how the units of time work.

Here is a table in the order of priority from top to bottom with a break down of the values you can use.

minute (0 – 59)
hour (0 – 23)
day of the month (1 – 31)
month (1 – 12) OR jan, feb, mar, apr…
day of the week (0 – 6) OR sun, mon, tue, wed, thu, fri, sat

Using the asterisk * in place of any of those values would mean that the cron needs to run for all values within the given column. For instance, if you had an asterisk in the minute column, that would mean to apply the cron for every minute.

Crontabs for every user are stored within the /var/spool/cron directory.
System wide cron jobs are stored in the /etc/crontab file.

So now lets go ahead and create a scheduled task of our own. What we are going to do is store the output of the linux utility uptime to a file inside the user root’s home directory. In order for us to do this we will need to set the crontab under the user root.

To accomplish this we will use the command crontab as such. “Make sure you are logged in as the user root”

crontab -e

As root you can specify other users crontabs to edit by using the -u option followed by the users username.

After you run the above command you will be brought into the systems default editor, which is usually VIM. Now that we are in the editor lets construct our first cron job.

* * * * * /usr/bin/uptime >> /root/uptime.log
The format is:   minute   hour   day   month   day_of_the_week   command_to_run

After you finish typing the above, save and exit the editor.  What we did was put the asterisk * for every column meaning that this cron will run for every minute of every hour of every day of every month for every day of the week.

It is also good practice to use the absolute path to the application you wish to run. You can obtain the direct path by using the which utility.

which uptime

We also used append redirection to create our file if it does not exist and automatically append each result from the crontab to the specified file.

Thats it for the lesson on CronTabs, also known as Scheduled Tasks. Let me know if you have any questions by commenting below.

Post By: | FavoriteLoadingAdd to favorites


Leave a Comment

Need Help? Ask a Question

Ask anything you want from how to questions to debug. We're here to help.

You Must Be Logged In To Post A Question.

Log In or Register