There are many maintenance commands on Linux that you need to run on a regular basis. Backups are one example, along with regular removal of “orphaned” packages etc. These commands will have different schedules. Some will be monthly, some weekly, some daily. You might have preferred times and unique rules such as “not on weekdays” etc. Linux has a marvelous system of “cron” which allows you to schedule these tasks using a simple interface. Let’s take a look at how to do this.
Running the “yum -y autoremove” Command
As an example, here’s a command that I use regularly to clean out orphaned packages on Linux and save disk space:
yum -y autoremove
Running the above gives the following output:
It identifies the orphaned packages that were previously installed by yum (or apt-get) to resolve a dependency, and those dependencies don’t exist anymore – probably because the original package was removed. Now let’s see how we can create a cron job to run this command on a regular basis.
Preparation: Allow NumLock on PuTTy
In case you’re connecting to a Linux server over SSH using the PuTTY client, there’s one tweak that will make it easier for you to enter cron jobs using the default “vi” editor. By default, PuTTy runs so that any input on the numeric pad sends an escape sequence to the terminal. We often need to type asterisks for creating cron jobs, and this behavior of PuTTy makes it inconvenient. To resolve this, open the PuTTy application, go to the “Terminal” tab o the left and click “Features”. Now enable the checkbox marked “Disable application keypad mode” as shown here:
Save this change for your pre-configured setups and you’re done. We’re now ready to easily edit cron!
Using crontab to Create a New Cron Task
In your terminal window, type:
This will open a temporary file using the default text editor (vi in the case of putty). If this is your first cronjob, it’ll be empty. To run the “yum -y autoremove” command once every week, add the following line:
00 00 * * 1 yum -y autoremove
Here’s the breakdown of what it means:
- The first “00” refers to the minute (from 00 – 59). I want the task to run on the hour at 00 minutes;
- The second “00” refers to the hour in 24 hours time. “00” means I want it to run at midnight;
- The next “*” refers to the day of the month. An asterisk (*) means it doesn’t matter which day it is;
- The fourth holds the month of the year from 1-12. The asterisk means it doesn’t matter which month;
- The next number refers to the day of the week from 0 to 6. “1” means I want it to run on a Monday;
- The final part of the line is the command itself.
Here’s what it looks like when entered into the vim editor after typing “crontab -e”:
When you save these changes, the crontab tool will transfer your entry into the actual files from which the cron schedule is read. These files are not meant to be updated manually. After exiting and saving the text editor, you should get a message that the changes have been installed like this:
Verify Cron History
If you’ve created a cron job, you might want to verify whether or not it’s running as planned. To do this, open this file: “/var/log/cron” like this:
Whenever a cron job runs, it outputs a line to this file. It can get quite long, so you can skip to the last line of the file by typing a capital “G” into the vi text editor. To demonstrate this, I created another cron job that runs every minute. Here’s the output in /var/log/cron:
You can see that the command was executed on the first second of the minute as expected. You can customize your cron jobs to almost any extent. There are options to specify multiple times and rangers. Just read the references online and you’re good to go!