Productivity Tips : cron, at, scripting

I originally was going to do this in separate posts, but with my schedule getting crazier, I decided to combine a couple things into one post.

Every user should learn how to use cron and at. These are daemons that automate tasks; the main difference between them being that cron is used for repeating events while at is better suited for executing individual tasks (and in situations where timing of execution isn’t of importance). These are not just tools found in Linux and the BSDs, though some Linux advocates pretend that it’s a selling point for Linux. Windows, too, has schedulers to automate the running of tasks; if you’re running anti-virus software, it should probably be set up to run automatically instead of when you feel like it.

What can be automated via cron and at? Everything you do. I use cron as my alarm clock, to clean up old files, to make back ups, to download news and aggregate it for me so I have one file to look at in the morning (with links I can check later), to download podcasts, to rename files, to update software (I keep vim patched weekly via cron), etc. Whatever you do with any repetition can be automated. This can be directly or via scripts. Most of my crontab consists of execution of scripts, some of which execute other scripts (“nested”). Some things, like my alarm, are specific individual commands.

The second part of this post is about how learning to write scripts helps make life easier. This goes hand in hand with using cron, but scripts can also be used any time to make complex (or even a series of easy) tasks easier to manage. Scripts can be written in any language: BASH is probably the most utilitarian because it’s the prevalent default shell in Linux. Other languages are also suitable. These include python, lua, ruby, Perl, guile (and other scheme/LISP-oriented languages), tcl, and so on. Each language has its own syntax and nuances so it pays to find something that appeals to your tastes and your way of thinking. My own tastes fall somewhere between scheme (notice I use a lot of parentheses? it’s so natural for me) and ruby. You may find Perl or BASH more to your liking and more convenient since they both are found on Linux systems. The important thing is learning to use a language, not advocating the superiority of one or another. Each language has its place, its adherents, and its own set of pluses and minuses.

Below is an example of a script I use to download content via cron. It’s a very short, simple ruby script. This is a very easy one because the content on this site doesn’t change names — it uses a suffix of _current.mp3 for all the latest versions. I use a simple array of the names of the podcasts I want and then run them through a loop so wget executes for each one. Total of seven lines, two of which are empty. This runs in middle of the night once a week. I use similar scripts to download other content on more and less frequent bases.

I use another cron job to clean these up so I don’t overwrite them in the event I don’t get a chance to listen to them. After a few days, it will execute and change their names from _current.mp3 to -YYWW.mp3 (two digit year and week number of the year). Then I can choose what to keep without worrying about things getting overwritten.

The result is no-hassle downloading and management of things I want; this means I don’t have to fire up a browser and sit there and click on links to get anything. The same goes for updating software by setting up cron jobs to check for updates (via CVS or SVN or GIT) and take care of things for you. It can be as elaborate as simple as your needs are, not to mention that it’s not really above your skill level because you don’t have to get too carried away to benefit. Fortunately, many projects exist to make it even easier for you by providing scripts that can be run from cron (such as bashpodder). There are many of these across the Internet so you can get ideas how to do what you need to do if any of them doesn’t already suit your needs.

So with two relatively simple things — tools like cron and at, a little exposure to scripting — you can automate tasks to make your life easier. And with your computer doing all the work, you’re more productive.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: