Task Scheduling in Linux

By abhishek | Sun, 08/31/2008 - 18:45

In recent past i use to do certain tasks at some scheduled time on the day which also included shutting down one of my machine at 8 PM. Then I came to know that this could be effectively done automatically using cron and so I am discussing cron in this post. Cron is a daemon which schedules recurring tasks to be executed at a predefined time and date . It is very easy to schedule a job to be run at a particular time using cron. Cron daemon is typically started at boot time and runs continuously in the background. Cron maintains a table of tasks in a file named crontab which is usually in /etc directory. Each user can have separate cron file In order to access the current cron table, the crontab command is used as follows :

$ crontab -e

The above command will load the user's crontab in an editor - usually 'vi '- for editing by the user.

The crontab file contains 6 fields which are as follows :

Min Hours day-of-month month day-of-week command-to-run

And the values of these fields can take the following form:

Min - 0-59 Hours - 0-23 day of month - 1-31 month - 1-12 or Jan-Dec day of week - 0-7 or Sun-Sat

Fields in a crontab may be separated by any number of tabs or spaces. And a '*' symbol in a field represent all valid values. Suppose I am logged in as root and want to modify a crontab file of a particular user. Then I use the '-u' switch : # crontab -u username -e And to list the crontab,

$ crontab -l

You can remove the crontab using the -r switch:

$ crontab -r

Restrict or allow user access to cron

Using the two files, /etc/cron.allow and /etc/cron.deny, root can allow or restrict a user from using cron.

System crontab files

/etc/crontab - Master crontab file /etc/cron.d/ - directory containing additional system crontab files.

The syntax of the system crontab file is slightly different from the user crontab file explained above. In the system crontab file, the sixth field is a username which will be used to execute the command in the seventh field.

Below is the listing of my system crontab file - /etc/crontab

# File: /etc/crontab SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # run-parts 01 * * * * root run-parts /etc/cron.hourly 00 4 * * * root run-parts /etc/cron.daily 22 4 * * 0 root run-parts /etc/cron.weekly 42 4 1 * * root run-parts /etc/cron.monthly

As seen above, run-parts is a shell script which takes one argument, a directory name, and invokes all of the programs in that directory. The directories cron.hourly, cron.daily, cron.weekly and cron.monthly contain executables which are run by the master crontab file /etc/crontab . Thus at 4:00 every morning, all of the executables in the /etc/cron.daily directory will be run as root.

If you view the /etc/cron.daily directory, you can see a lot of executables which are run daily at a predefined time as specified in the /etc/crontab file.

In my case I added the following lines

00 20 * * * root /sbin/halt (To shut down system @ 8 PM daily)

00 13 * * 0 root /sbin/halt (To shut down system @ 1 PM on sundays)

using this I was able to automatically shutdown machines once the clocked said its time to close.

Thunderbird Signatures/Buttons

Configuring DHCP Server on Ubuntu

By abhishek | Sat, 08/23/2008 - 17:38

Ubuntu works the apt-get way and hence makes it easy to install packages on it for confuguring Ubuntu as a DHCP server install the server software using the following command

$ sudo apt-get install dhcp3-server

this would install the required packages

after the package is installed backup the default configuration files using the following

$ sudo cp /etc/default/dhcp3-server dhcp3-server.back

dhcp3-server is a file where you specify the interface DHCP should server (wlan0 in mycase)

edit the file using

$ sudo vim /etc/default/dhcp3-server





(wlan0 might be eth0 or eth1 in your case)

save and close the file

only one more file editing is required now create a backup for mail dhcpd.conf using

$ sudo cp /etc/dhcp3/dhcpd.conf /etc/dhcp3/dhcpd.conf.back

edit the file using

$ sudo vim /etc/dhcp3/dhcpd.conf


# option definitions common to all supported networks…
option domain-name “example.org”;
option domain-name-servers ns1.example.org, ns2.example.org;

default-lease-time 600;
max-lease-time 7200;


#option domain-name “example.org”;
#option domain-name-servers ns1.example.org, ns2.example.org;
#default-lease-time 600;
#max-lease-time 7200;


# A slightly different configuration for an internal subnet.
#subnet netmask {
# range;
# option domain-name-servers ns1.internal.example.org;
# option domain-name “internal.example.org”;
# option routers;
# option broadcast-address;
# default-lease-time 600;
# max-lease-time 7200;


# A slightly different configuration for an internal subnet.
subnet netmask {
option domain-name-servers;
option domain-name “mydomain.local”;
option routers;
option broadcast-address;
default-lease-time 600;
max-lease-time 7200;

thats it

restart the dhcp server using

$ sudo /etc/init.d/dhcp3-server restart

this server will start allocating IP dynamically.

Patching The Kernel

By abhishek | Sun, 08/03/2008 - 01:04

Is Patching a Good Idea?

In most of the recent Linux distros available today we find Kernel Version 2.6.9+ which by itself supports all latest devices, but still there might me some improvements done to the installed kernel first option is to download the latest kernel available from http://www.kernel.org which would require a download of 40+Mbs or the other option is to patch your existing kernel.
90-95% of the kernal source remains the same and downloading the entire source for this 5-10% is never a good idea. For this reason, kernel patches are released Kernel patches contain only the files that have changed since the last kernel, hence making it less pain to upgrade.
Steps for applying a patch
Step 1 ) Take Backup of the existing kernel Source (/usr/src/linux2.6.24 in my case)
Step 2 ) Download the patch from http://www.kernel.org
remember if you want to upgrade your kernel to 2.6.26 from 2.6.24 you require 2 patches i.e. 2.6.25 and 2.6.26 patching a step by step process, you cant jump.
The current stable version of kernel is 2.6.26
& the patch available is
Step 3) un-zip the patch file into the kernel sorce directory
# tar -xvf patch-2.6.25.tar
# tar -xvf patch-2.6.26.tar
# mv patch-2.6.* /usr/src/linux2.6.24
Step 4) Change to Source Directory and apply patch
# cd /usr/srclinux2.6.24
# patch -p1 < patch-2.6.25
# patch -p1 < patch-2.6.26
for more info on patch command use man patch
this command will provide lot of output just have a look at it,
patch command uses diff in background and changes approprate parts of the source.
After these steps are done you need to recompile the kernal using the same process as discussed in my blog earlier