Schedule Tasks with Cron

I tell people who are curious about Linux that you really don’t need to open a terminal to run a Desktop. The terminal is a scary thing for noobs. They get the willies when you start talking about it. While it is true that modern desktops provide pretty much all the point-and-click functionality you could ever need, between you and me, I know that the real power of Linux lives on the command line. You can do amazing things with just a few keystrokes if you have a bit of knowledge under your belt. One of those amazing things is that you can automate just about anything using Cron.

Cron is already working on your machine even if you’ve never heard of it. It works totally in the background, stealthily running commands provided to it by different programs that are installed on your computer. It mainly handles rather mundane maintenance tasks but any user can tap into its functionality and make Cron do their bidding.

The video goes into great detail and I go through how to setup up cron jobs using the crontab command. Toward the end of the video, I show you a nifty little GUI tool that does the same thing. As I said in the video, I advise the novice to watch the whole thing so they have a good understanding of what the GUI tool is doing. I also touch on Bash Scripting here because Cron becomes monster truck powerful when you ask it to run a script on a regular basis. The mind reels when you think of all the stuff you can do!

A couple of notes:

In my first example, I talk about using BleachBit along with cron to periodically clear your system of junk files and clean out browser caches. I think a closer look at just how that would work is in order.

BleachBit is available for lots of Linux distributions and it should be in the repositories. In Ubuntu and all of Ubuntu’s children, you can install it with this command:

sudo apt-get install BleachBit

BleachBit will put two entries in your menu. One will open BleachBit and prompt you for your sudo password and will run as root. BleachBit will clear cached packages and remove unneeded packages while it will clear out the trash, clean browser, and other caches and more when it is run by a user.  Once you figure out what boxes you want to be checked, you can launch BleachBit from a terminal and it will do its thing without opening the GUI. It goes by whatever you set in the GUI when it runs from a terminal, so make sure you go through the setup there before launching it in a terminal. (I wasn’t completely sure about this in the video but I got it for sure now.) The command to run BleachBit and have it clear out junk files is:

BleachBit -c

This makes keeping things neat and tidy on you system a breeze and if you have lots of users you can simply su into everyone’s account and run BleachBit from a terminal. To make the process totally automated, you could configure BleachBit for each account on the machine and handle it completely automatically.

Anytime you schedule a command to run in Cron, you’ll want to figure out where to send its output. By default, Cron sends output from the commands it runs to system email folders. No, I’m not talking about Yahoo or Gmail here. We’re talking about the mail spool for users that lives in the var directory. This functionality is rather archaic these days. Most casual Linux users probably don’t even know it exists let alone how to check system e-mail. You can direct the output to a file in your home folder or just send it into the black hole known as /dev/null. I tell you how to do that in the video.

I talked about writing scripts to accomplish more complex tasks in Cron and I used a script that will automatically update any Ubuntu based Linux system. There are several ways to skin this cat, so feel free to improve upon it as you see fit. This one works quite well, though, and it just might be the ticket for that media server or mail server you got running in the corner. Here’s the script:

#!/bin/bash
apt-get update > /home/joe/update.log
apt-get dist-upgrade -y >> /home/joe/update.log &&
echo "All done!" >> /home/joe/update.log

I explain the logic behind this script in great detail in the video.

After going through all the command line stuff, I introduced folks to a nifty little tool that will do all of this from a GUI. It’s called Gnome Schedule and it’s part of the Gnome Desktop suite.  There are several to choose from but I like the simplicity of Gnome Schedule. You can install it on your Ubuntu system with this command:

sudo apt-get install gnome-schedule

Whether you work with crontab at a terminal or use a tool like Gnome Schedule to setup jobs for Cron, it all comes out the same in the end. Cron gives you the power to automate just about anything. Feel free to play around with it. Who knows what you might come up with?

There’s a lot more to Cron than what I got to in the video. If you’re super curious about all its crazy possibilities, do check out the Ubuntu Community Help Wiki article about it:

https://help.ubuntu.com/community/CronHowto

Have fun!

 

Photo by Inha Leex Hale

Also check out...

Joe Collins
Joe Collins worked in radio and TV stations for over 20 years where he installed, maintained and programmed computer automation systems. Joe also worked for Gateway Computer for a short time as a Senior Technical Support Professional in the early 2000’s and has offered freelance home computer technical support and repair for over a decade.

Joe is a fan of Ubuntu Linux and Open Source software and recently started offering Ubuntu installation and support for those just starting out with Linux through EzeeLinux.com. The goal of EzeeLinux is to make Linux easy and start them on the right foot so they can have the best experience possible.

Joe lives in historic Portsmouth, VA in a hundred year old house with three cats, three kids and a network of computers built from scrounged parts, all happily running Linux.

Leave a Reply and sign in below


Wordpress Register | Wordpress Login

Be the First to Comment!

Notify of

wpDiscuz