Skip to content

Archive for May, 2009

30
May

Free Up Cache Memory in Ubuntu

Ever wondered how to remove the cache memory in Ubuntu? Linux kernels 2.6.16 and newer provide a mechanism to clear the inode, page, and dentry caches on demand. All you have to do is echo a value to the proc filesystem, and you’re done. Follow the recipe below:

As this is a non-destructive operation and dirty objects are not freeable, run:
sync

To free pagecache:
sudo sh -c "echo 1 > /proc/sys/vm/drop_caches"

To free dentries and inodes:
sudo sh -c "echo 2 > /proc/sys/vm/drop_caches"

To free pagecache, dentries and inodes:
sudo sh -c "echo 3 > /proc/sys/vm/drop_caches"

Hope this works for you all.

26
May

How To Check Which Software Package Is Using More Space

If you’re running out of disk space and you want to quickly see what packages are using the most space on your hard drive, use the following command

dpkg-query --show --showformat='${Package;-50}\t${Installed-Size}\n' | sort -k 2 -n | grep -v deinstall | awk '{printf "%.3f MB \t %s\n", $2/(1024), $1}'

That will sort the packages by size, putting the largest ones on the bottom. If you only want to see the top few, you can type

tail -n 10

at the end, because in all likeliness you have a *lot* of packages installed

26
May

Using Terminal To Check The Size Of A Directory

You can also use a Terminal command to check the size of an individual directory. For instance, if you wanted to check the size of your Documents directory, you would use this command:

du -hs Documents

This will give you the combined size of your Documents folder, along with any subfolders.

14
May

Adding Public Key For Ubuntu Repositories

Following recipe will help to add a public key to ‘apt’ for repositories not already included in /etc/apt/sources.list. If you get an error ‘NO_PUBKEY XXXXXXXXXXXX’ , just use XXXXXXXXXXXX public key name in the following code.

gpg --keyserver hkp://subkeys.pgp.net --recv-keys XXXXXXXXXXXXXXXXX
sudo gpg --export --armor "XXXXXXXXXXXXXXXXXXX" | sudo apt-key add -
sudo apt-get update

13
May

Enabling Spell Checking In Kile Using Aspell

Kile is a top-notch KDE frontend LaTeX editor. Enabling spell checking for it requires a small trick if you’re running Ubuntu, which is Gnome based. Several options, including spell checking, for KDE applications are configured within the KDE control center. To get the job done, proceed as follows:

1. First, you need to install the KDE control center
sudo apt-get install kcontrol

2. Then, open KDE control center by running the kcontrol command, navigate to KDE Components -> Spell Checker and set the spell checking Client to ASpell

3. Spell checking will now be enabled next time you start Kile!

12
May

Backup Subversion Repositories And Projects

After I decided to manage my PhD thesis and projects with subversion, the most important thing to consider was a proper backup strategy. I decided to go a bit overboard with it as it might save me some day. There are several ways to backup subversion and I decided to use all of them!

a. Using crontab and rsync to backup subversion

Read my earlier post on crontab if you are not familiar with it. Someday I will write about rsync as well. But for now read this if you are new to rsync. I use the following code to backup the entire subversion repository to my backup directory.

rsync -av --delete --progress /source_directory/subversion/ /destination_directory/backups/subversion_backup/

I used crontab to schedule the above job to run at midnight every day. In case something happens to the original subeversion repository, it is easy to retrieve data from this backup – just copy it to the original location!

b. Using the backup script from COLLABNET (the subversion guys)

The next technique it to use the python based backup script from subversion called hot-backup.py. This script can be used to make a backup of the latest version of the subversion repository. I used the following code to download the latest version from COLLABNET website and install it on my system.

wget http://svn.collab.net/repos/svn/trunk/tools/backup/hot-backup.py.in
mv hot-backup.py.in hot-backup.py

Once that is done, I had to make some configuration changes in the file. The following lines were changed

# Path to svnlook utility
svnlook = r"@SVN_BINDIR@/svnlook"
# Path to svnadmin utility
svnadmin = r"@SVN_BINDIR@/svnadmin"

to look like this

# Path to svnlook utility
svnlook = r"/usr/bin/svnlook"
# Path to svnadmin utility
svnadmin = r"/usr/bin/svnadmin"

The next step is to run the script check if everything is fine. I used the code below:

python hot-backup.py --archive-type=zip --num-backups=10 ~/source_directory/subversion_repository /destination_directory/subversion_backup/

The above code will make a hotcopy of the latest version of the subversion repository into the backup directory in zip format. A total of 10 backups will be maintained at a time, oldest one being deleted when new ones are added.

Once the backup was properly inspected I created a cron job to run this command at midnight every day.

c. Using the subversion ‘hotcopy’ command

This is a very simple technique as it uses the inbuilt ‘hotcopy’ command to backup the svn repository. Follow the command below to make a reliable incremental backup of your svn repositories.

svnadmin hotcopy /source_directory/subversion_repository /destination_directory/subversion_backup/

Create a cron job to schedule it.

Once you have your backup system in place, rest assured that your data are safe.

11
May

EXT3 File System On A USB Stick

This how-to will create USB memory stick with ext3 file system. First of all, use sudo fdisk -l to get the device handle of USB stick. It might look something like this:

Disk /dev/sdd: 4041 MB, 4041211392 bytes
125 heads, 62 sectors/track, 1018 cylinders
Units = cylinders of 7750 * 512 = 3968000 bytes
Disk identifier: 0xddeb26e6
Device Boot Start End Blocks Id System
/dev/sdd1 1 61664 3944719 b W95 FAT32

Now unmount it by typing:

sudo umount /dev/sdd1

Afterwards, format the USB stick using the following code. Remember to spell the USB device properly. This command is quite dangerous, it will erase everything on the drive specified.

sudo mkfs.ext3 /dev/sdd

Now that the USB stick is formatted, a new partition has to be create on it. Type:

sudo fdisk /dev/sdd

to create a partition on the USB stick. This will be the new sdd1 partition. After this is done it has to be formatted again using:

sudo mkfs.ext3 /dev/sdd1

to get the ext3 file system on it.

Now then, label the new ext3 USB stick using the command:

sudo e2label /dev/sdd1 usb_backup

Automatically mounting the USB stick when the server reboots

After the usual backup of the /etc/fstab, edit it to include a new line at end:

/dev/sdd1 /media/usb-backup ext3 user,rw 0 0

Create a new directory ‘usb-backup’ and make it readable and writable by the user:

sudo mkdir /media/usb-backup
sudo chmod 755 /media/usb-backup

Use sudo mount -a to mount the drive.

This USB stick can now act as a backup disk.

11
May

Inspecting Network Cards In Ubuntu

I was planning to upgrade my home network into Gigabit. Before I start to spend money on it, I wanted to check the capabilities of the network cards on my laptop. Here is how I did it.

Install ‘hwinfo’. Its quite an useful tool as it can check the configuration of any hardware. Refer the manpage of more information.

sudo apt-get install hwinfo

To display details of the network cards on the system, type:

sudo hwinfo --netcard

Alternatively one can use

lshw -C network

to display details of the network card. Read manpage for more on lshw.

8
May

Scheduling Commands With Crontab In Ubuntu

The following applies to any linux distribution

Crontab

Crontab is an integral part of most Linux distributions.It is a simple text file that holds a list of commands that are scheduled to be run at specified times. These commands, and their relative run times, are controlled by the ‘cron’ daemon and are executed in the system background.

Each user on a Linux system has their own crontab which contains the schedule of commands. In order to edit or create a crontab, one must use the text editor that is specified by the system, ‘nano’ being the default on my system. To create a crontab open a terminal and type:

crontab -e

‘nano’ will open with a blank window for the job schedule to be entered. Each line represents a seperate cron job entry – also known as a ‘cron job’.

Creating a ‘cronjob’

Each section in a cronjob is separated by a space, with the final section having one or more spaces in it. No spaces are allowed within sections 1-5, only between them. Sections 1-5 are used to indicate when and how often the task has to be executed. This is how a cron job is layed out:

minute (0-59), hour (0-23, 0 = midnight), day (1-31), month (1-12), weekday (0-6, 0 = Sunday), command

Example 1

01 04 1 1 1 /usr/bin/somedirectory/somecommand

The above example will run /usr/bin/somedirectory/somecommand at 4:01am on any Monday which falls on January 1st. An asterisk (*) can be used so that every instance (every hour, every weekday, every month, etc.) of a time period is used. Code:

Example 2

01 04 * * * /usr/bin/somedirectory/somecommand

The above example will run /usr/bin/somedirectory/somecommand at 4:01am on every day of every month.

The “/usr/bin/somedirectory/somecommand” text in the above examples indicate the task that will be run at the specified time. It is recommended to use the full path to the desired command as shown in the above examples. Crontab will echo any mistakes found in a cronjob. The crontab will start running as soon as it is properly edited and saved.

Other crontab options

crontab -l option causes the current crontab to be displayed on standard output.
crontab -r option causes the current crontab to be removed.
crontab -e option is used to edit the current crontab using the editor specified by the EDITOR environment variables
($EDITOR=vi crontab -e).

Note: The double-ampersand (&&) can also be used in the “command” section to run multiple commands consecutively.

7
May

Monitoring Real-time User Logins In Ubuntu 8.04 Server

‘whowatch’ is a console, interactive users and process monitoring tool. It displays information about the users currently logged on to the machine, in real-time. Besides the standard informations such as login name, tty, host and the user’s processes, the type of the connection (ie. telnet or ssh) is also shown. A particular user can be selected and his processes tree may be viewed. Trees are displayed with an additional column that shows the owner of each process. In the process tree mode SIGINT and SIGKILL signals can be sent to the selected process. Killing processes is just as simple and fun as deleting lines on the screen [information from somewhere on the www]. To install ‘whowatch’:

sudo apt-get install whowatch