How-To

Automating Routine Tasks in Linux

"It is certainly a tool that every system administrator should be aware of and use to automate mundane routine tasks such as backups and taking applications offline."

It's common to need an action performed at regular intervals on Linux, for example to clean up unused files (which may be done weekly), to stop a process so backups can be performed (which may be done daily) or to send reports out (which may be done hourly).

Cron is a scheduling utility for Linux that helps make this possible. This feature is not a new one -- it was released about 50 years ago in 1975 with early releases of Unix -- and over the years has been ported to Linux and other OSes.

Commands or scripts that are carried out by cron are referred to as "cron jobs," and can be either on the system-wide or individual level. These jobs are stored in a crontab file, which contains the action to be taken and specifies when it should be executed.

I will be using Ubuntu 18.04 for this article, which has cron installed by default. The commands mentioned below may be slightly different for other Linux distributions. First, I became root and verified that it was installed by entering:

  sudo bash
  service –status-all | grep cron
  systemctl status cron
[Click on image for larger view.]

The "+" in front of cron indicated that it was installed and running. The systemctl also meant that it was running.

If you find that cron isn't already installed, it's possible to do so all common Linux distributions using their package manager. If cron is installed but not running, you can enable it by entering sudo systemctl enable cron.

Creating Cron Job
s To create a cron job, enter crontab -e. The first time you run this command, you will be asked which editor you would like to use.

[Click on image for larger view.]

This command creates a file in the /var/spool/cron/crontabs directory where cron jobs are located. Each user will have their own file in this directory. These files should not be edited directly but instead by using crontab.

[Click on image for larger view.]

The format of a cron job indicates when the job should run. Its format is as follows: minute (m), hour (h), day of month (dom), month (mon) and day of week (dow); or, alternatively, use "*" in these fields to indicate "any."

I created jobs that would write the date to a file every minute, at 2:30 and 3:35 every day; at 1:05 and 1:30 on Saturday (7); and at 2:00 and 4:00 on Sunday (0). This example demonstrates how the "," can be used to include additional values in the same field.

  * * * * * date >> /tmp/mydate0.txt
  30 2 * * * date >> /tmp/mydate1.txt
  35 3 * * * date >> /tmp/mydate2.txt
  5,35 1 * * 7 date >> /tmp/mydate3.txt
  0 2,4 * * 0 date >> /tmp/mydate4.txt

To see the entries in cron, I entered crontab -l.

[Click on image for larger view.]

After a few minutes, I verified that my cron jobs were working by printing out the contents of the files.

[Click on image for larger view.]

Cron also has shortcuts that can be used to run commands at the top of the hour, and at the start of each day, week, month or year. These shortcuts are @hourly, @daily, @weekly, @monthly and @yearly, respectively. To have a job start after rebooting the machine, use @reboot.

If you make illegal time entries for the jobs, you will be alerted when you exit the editor.

[Click on image for larger view.]

For more information on cron, you can read the manual on cron tab by entering man crontab and man cron.

For me, the point of using cron is to turn off my web server Monday through Saturday at midnight for one hour, and from midnight to 1 p.m. on Sunday, for maintenance and backups. To do this, I created the following entries:

  @daily  systemctl stop apache2
  @weekly  systemctl stop apache2
  0 1 * * 1,2,3,4,5,6,7 systemctl start apache2
  0 13 * * 0 systemctl start apache2
  @reboot  systemctl start apache2

To verify that my cron entries were correct, I used Scoutbees (a synthetic monitoring tool) by ControlUp (a company that I work for) to monitor the web site. The dashboard showed that it was unavailable during the times that I specified for maintenance and updates.

[Click on image for larger view.]

The failures coincided with the times when the web site was scheduled for maintenance.

Conclusion
Cron has been around for just about half a century and has proven to be a rock-solid way to schedule tasks to be performed at a set schedule. It is certainly a tool that every system administrator should be aware of and use to automate mundane routine tasks such as backups and taking applications offline.

About the Author

Tom Fenton has a wealth of hands-on IT experience gained over the past 30 years in a variety of technologies, with the past 20 years focusing on virtualization and storage. He previously worked as a Technical Marketing Manager for ControlUp. He also previously worked at VMware in Staff and Senior level positions. He has also worked as a Senior Validation Engineer with The Taneja Group, where he headed the Validation Service Lab and was instrumental in starting up its vSphere Virtual Volumes practice. He's on X @vDoppler.

Featured

Subscribe on YouTube