Friday, April 10, 2009

Cronjobs

I recently updated my Ubuntu OS from Intrepid to Jaunty. Since the official release is mere weeks away, there have been updates to the kernel and packages almost daily. I figured I should probably save myself some time and screen-staring by setting up a cronjob to do it for me. Cron, as you may know, is a tool to schedule commands to be given from a script, at any date and time, without the need for you to actually be anywhere near the computer. If you run Linux and would like to schedule a cronjob to update it automatically, then simply follow the instructions below.

First, you will need to edit your crontab file. Simply type

sudo crontab -e

in a terminal. You will be asked which editor you wish to use. I choose vim simply because I like it, but you will have other choices, including the gedit graphical editor (in Ubuntu, anyway). Once the crontab file appears in your editor, just type

0 0 * * 0 root (apt-get update && apt-get -y -d upgrade) > /dev/null

and save it. That's it- the cronjob will run at midnight every night. If you want to specify a different time/date, just replace the first 0 with the minute (0-59), the second 0 with the hour (0-23), and the last 0 with the three-letter day (sun, mon, tue, etc.). The day of month and month can be specified by replacing the asterisks with the proper numbers- 1-31 for the first asterisk and 1-12 for the second. The rootpart just tells cron to run the following commands as root. The -y option tells cron to automatically answer 'yes' to the prompts, so it doesn't just hang waiting for your response. The -d option tells it to just download the files, but not install them until you tell it. This option can be removed, but it's not very safe- you always risk the very slim possibility that the update will cause something to stop working. So review the list of packages to be installed before you actually install them. Lastly, the > /dev/null redirect tells cron to direct the output to the black hole that is /dev/null, keeping your terminal free of lengthy output.

There are, of course, various other commands and options for using cron. Simply type man cron or cron --help for a manual or list of commands. And enjoy your up-to-date Linux distribution!

No comments:

Post a Comment

Do you have an opinion? Tell me what you think!