Being on hardy, I had developed this evil habit of “sudo apt-get update” which I ‘d do now and then, hoping for a better updated, greater hardy or hoping for some miraculous update which would fix all my annoying hardy bugs. 😛
Even otherwise, we do miss our nifty Windows task scheduler. Or how about shutting down your internet connection at 8 in morning to save bandwidth. So here is the linux cute thing.
Cron is a simple scheduler that runs by default on most Linux distros, including Ubuntu. The daemon “crond” is responsible for reading tasks from a simple user file “crontab” (stored in /var/spool/cron/crontabs).
So lets create some useful automated tasks. Creating a task is as simple as:
crontab -e -u
This will start editing, the user specific crontab file using system default editor which is nano in Ubuntu. You edit your own crontab, omit -u
crontab -e. To run commands which might require sytem level permissions, edit root crontab,
sudo crontab -e -u root
To use a different editor you could simply use place
export EDITOR=vim in your .bashrc. Or you could even do
EDITOR=vim && crontab -e. You can also change system wide default editor you should update the link for /usr/bin/editor -> to whatever editor you want.
Ok now to add some tasks, The first line looks something like this:
# m h dom mon dow command
m -> minutes (0-59)
h -> hour (0-24)
dom -> date of month (1-31)
mon -> month (1-12)
dow -> day of week (0-6) [I think sunday is 0]
You can use ‘,’ ‘-‘ or ‘*’ as
You could use * as wild char for skipping an attribute. For e.g. I might add following line to stop my music player automatically at 3:00 am in morning every day.
0 3 * * * killall audacious
0 3 * * * amarok -s
To start a command on UI, add
export DISPLAY=:0 to the command. Example :
export DISPLAY=:0 && amarok mms://some_radio_show : Starts some music show at some scheduled time automatically.
Some other useful commands ::
1. For updating database used by locate everynight at 3(should be added to root –
sudo crontab -e, and no sudo in the command) :
0 3 * * * updatedb
2. For keeping your hardy packages updated and getting the update notifications as soon as possble (Finally I got rid of my habit where it all started :)). This would check for updates every hour at 10th minute. I know that’s kinda harsh but that’s how much I want to be updated 🙂 :
10 * * * * apt-get update
3. For shutting down internet connection at 7:15 in morning everyday for bandwidth limitations.
15 7 * * * ifconfig eth1 down
4. For synchronising my ipod (my favourite) with my music:
15 7 * * * rsync -van --exclude=*.mp4 --delete --max-size=20m iPod_Control/Music/ /media/Vimpudeo/iPod_Control/Music/
5. Or running root-kit scan at 6 in morning(To be added to root crontab):
01 6 * * * chkrootkit
You can have your own complex scripts and run them at scheduled times. For e.g. backup scripts, synchronization scripts.
So happy time scheduling.