Linux has many great tools built in that help maintain the system without user intervention. One such tool is Cron.

On my Ubuntu 8.10 system, there are many things that are set to run each day: locate database updates, misc cleanup utilities, automatic package updates, log rotations, etc. All of these are managed by the Cron system.

For a while, I needed to manually run the updatedb command to update the locate database, and I never thought about why. The problem is that my daily, weekly, and monthly Cron jobs never run. The reason for this is that these Cron jobs are scheduled to run very early in the morning, when my system is off. Thus, these job schedules never run.

The solution for this is easy. I simply need to change the times these run at to times when my system is on.

Some of you may be unfamiliar with Cron and how to set up jobs for Cron to execute, so let’s look at my /etc/crontab file quickly to see what the problem is.

# /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.SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# 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 four lines at the bottom are the parts of interest. This looks like a jumbled mess, but it’s easy to understand once you break it down section piece by piece. Each part is as follows:

  • Minute the command is to be run at
  • Hour the command is to be run at
  • Day of the month the command is to be run at
  • Month the command is to be run at
  • Day of the week the command is to be run at
  • User to run the command as
  • The actual command to run

Given this information and the fact that * means all, the last line means: at 6:52am on the first day of each month, have the root user run “test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )“.

So, the four lines at the bottom of my /etc/crontab file have my system run all the commands in /etc/cron.hourly every hour of every day at 17 minutes past the hour, all the commands in /etc/cron.daily every day at 6:25am, all the commands in /etc/cron.weekly on the 7th day of each week at 6:47am, and all the commands in /etc/cron.monthly on the first day of each month at 6:52am, respectively.

Since the specific Ubuntu machine that I am talking about is my office machine and I’m rarely in the office before 7am, these Cron jobs don’t get a chance to run like they normally should. It should be noted that they run at the specific times they do so that users who start to access the machines at regular school or office times will have a system that very recently ran these commands, providing fresh logs, a recently updated locate database, etc without bogging the system down as they begin to access it.

I want to change the time to something later but not so late that I don’t get to benefit from the commands until late in the day. The best time that meets these requirements for my use is changing 6am to 8am for each of the last three commands.

The following is my modified /etc/crontab file:

# /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.SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

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

Notice that all I had to do is change the last three lines’ second number from a 6 to an 8. All I have to do now is save the file, close it, and wait until tomorrow for the commands to be run starting at 8:25am.

If you want to modify your /etc/crontab file, keep in mind that you will need root access to do this.

You can open the file in Vi using:

[chris@home ~]$ sudo vi /etc/crontab

If you are more familiar with graphical rather than command line editors, you can open up the file for editing in Gedit with the following command:

[chris@home ~]$ gksu gedit /etc/crontab

Now you can enjoy making use of Cron even if you don’t have your Ubuntu machine on 24/7.

Did I help you? Send me a tip.