Categories
commands How to's

Convert Dos/mac Files to Unix format

If you open unix text files in windows there are strange characters appearing instead of text. The same happens if you open dos text files in unix you will see strange characters like ^M at the end of each line. you can use dos2unix program to convert these dos files into unix text file format.
usage

$dos2unix file.txt

converts file.txt from Dos format to unix format and replaces it. You can also use fromdos to same the thing

$fromdos file.txt

There is another command unix2dos to reverse the task

$unix2dos file.txt

converts file.txt from unix format to dos format and replaces it… you can use todos to do the same task as

$todos file.txt

Categories
commands Info

Files used by a program

how to find which process is using a file?

You can view the process using the file or filesystem using lsof  & grep  command.

lsof  lists all files in use in the system.

if you want to find the application which is using the file sag.pdf  use

$lsof |grep sag.pdf

and you will get the output as

ld-linux. 4220      nandam   30r      REG        3,1   869300    136163 /home/nandam/tldp books/sag.pdf

you can also use lsof with grep to find the files used by a process
like

$lsof|grep bash
Categories
commands Info

Counting words & lines in a File

wc is used to count no of words and lines in a text file
To count no of characters

$wc -m filename

To count no of words

$wc -w filename

to count no of lines in the file use

$wc -l filename

refer wc manual for more info on wc or

$man wc

Categories
commands Info

Taking screenshots in linux

Taking Screenshots in Gnome..
One can take screenshots using gnome-screenshot tool.
open console and enter
gnome-screenshot and it will take screenshot of whole screen. If you need to take a delayed screen show enter

$gnome-screenshot -d 10

The above command delays the screenshot by 10 secs.. This is useful if you
want to take a screenshot when the system menus are open. When you open
Applications menu and press on printscreen button the menu gets closed
or you cannot take a screenshot of the opened menu. If you use above
command you can take screenshot of the opened menu.

if you need to take screenshot of a window enter

$gnome-screenshot -w -d 5

and select the wind which you need to take a screenshot. Or you can press alt+printscreen.

You can also use gimp for taking screenshots. Open gimp and goto
 file–>acquire–>screenshot and you will see a small window
with options to take screenshot of an active window or to take
screenshot of full screen or of a selected area.. Select the option of your choice and click on snap button to take screen shot.

Categories
commands Info

The Sort Command

The sort Command in Linux is one of the most ignored(?) yet  powerful command.
Sort is used to sort data or records.
if you use with other utils like ls it can give  you a wide variety of data.
let’s say the ls -l output of a directory is

-rw-r--r-- 1 nandam nandam  785280 2008-05-27 22:26 gdocs-1.0.4.oxt
-rw-r--r-- 1 nandam nandam 4997184 2008-05-27 22:16 Sun_ODF_Template_Pack_en-US.oxt
-rwxr-xr-x 1 nandam nandam  360756 2008-01-18 19:30 WriterTemplates.oxt
-rwxr-xr-x 1 nandam nandam   48019 2008-01-18 19:30 WriterTools.oxt

Now if you want to sort the above data according to column five (size of each file) you have to enter

$ls -l |sort -k 5

and here is what you get

-rwxr-xr-x 1 nandam nandam  360756 2008-01-18 19:30 WriterTemplates.oxt
-rwxr-xr-x 1 nandam nandam   48019 2008-01-18 19:30 WriterTools.oxt
-rw-r--r-- 1 nandam nandam 4997184 2008-05-27 22:16 Sun_ODF_Template_Pack_en-US.oxt
-rw-r--r-- 1 nandam nandam  785280 2008-05-27 22:26 gdocs-1.0.4.oxt

-k —> instructs sort to sort records by column 5 in ascending order.
But, sort considers the fifth column as a text string. to sort the data considering it as a number we must pass -n as below

$ls -l|sort -k 5 -n

and now you will get the correct format as below

-rwxr-xr-x 1 nandam nandam   48019 2008-01-18 19:30 WriterTools.oxt
-rwxr-xr-x 1 nandam nandam  360756 2008-01-18 19:30 WriterTemplates.oxt
-rw-r--r-- 1 nandam nandam  785280 2008-05-27 22:26 gdocs-1.0.4.oxt
-rw-r--r-- 1 nandam nandam 4997184 2008-05-27 22:16 Sun_ODF_Template_Pack_en-US.oxt

But the size is in ascending order. If you want the listing in descending order use -r option

ls -l|sort -k 5 -n -r

or

ls -l|sort -k 5 -nr

now the output will be in descending order like this

-rw-r--r-- 1 nandam nandam 4997184 2008-05-27 22:16 Sun_ODF_Template_Pack_en-US.oxt
-rw-r--r-- 1 nandam nandam  785280 2008-05-27 22:26 gdocs-1.0.4.oxt
-rwxr-xr-x 1 nandam nandam  360756 2008-01-18 19:30 WriterTemplates.oxt
-rwxr-xr-x 1 nandam nandam   48019 2008-01-18 19:30 WriterTools.oxt

anyway the above is only for illustration.. the same can be obtained by command ls

$ls -lS

by default, it sort takes space as delimiter. to set another character as delimiter use -t

$sort -t : xxxxxx

Now you don’t want to sort already sorted data. Use -c to check for sorted data and -u to show only unique records.
-c options report the first line if the given data is not sorted. To stop it from displaying the first record use -C.

sample input files.txt

-rw-r--r-- 1 nandam nandam 4997184 2008-05-27 22:16 Sun_ODF_Template_Pack_en-US.oxt
-rw-r--r-- 1 nandam nandam 4997184 2008-05-27 22:16 Sun_ODF_Template_Pack_en-US.oxt
-rw-r--r-- 1 nandam nandam  785280 2008-05-27 22:26 gdocs-1.0.4.oxt
-rwxr-xr-x 1 nandam nandam  360756 2008-01-18 19:30 WriterTemplates.oxt
-rwxr-xr-x 1 nandam nandam   48019 2008-01-18 19:30 WriterTools.oxt

$sort -k 6,7 files.txt

sort the data first by column 6 and then by column 7
and the above command gives the files by last modified date and then by time.

Of course, if you want to list the files by last modification date you can use

$ls -lt

which gives the same output as above command
Let us use another command in conjugation with sort.
like df

$df

Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/hda1             19542436   4209216  15333220  22% /
tmpfs                   242092         0    242092   0% /lib/init/rw
udev                     10240       104     10136   2% /dev
tmpfs                   242092         0    242092   0% /dev/shm
/dev/hda6             58133024  53965408   4167616  93% /mnt/win
/dev/sdb1              3936736    897564   3039172  23% /media/KINGSTON
/dev/hdc               4588298   4588298         0 100% /media/cdrom0
/dev/sda1            488384000 270011588 218372412  56% /media/disk

$df |sort -k 2 -n

gives the size of partitions in ascending order.

Filesystem           1K-blocks      Used Available Use% Mounted on
udev                     10240       104     10136   2% /dev
tmpfs                   242092         0    242092   0% /dev/shm
tmpfs                   242092         0    242092   0% /lib/init/rw
/dev/sdb1              3936736    897564   3039172  23% /media/KINGSTON
/dev/hdc               4588298   4588298         0 100% /media/cdrom0
/dev/hda1             19542436   4209228  15333208  22% /
/dev/hda6             58133024  53965408   4167616  93% /mnt/win
/dev/sda1            488384000 270011588 218372412  56% /media/disk

Suppose if I want to know which drive have largest percent of space used I have to enter

$df | sort -k 5 -n

Filesystem           1K-blocks      Used Available Use% Mounted on
tmpfs                   242092         0    242092   0% /dev/shm
tmpfs                   242092         0    242092   0% /lib/init/rw
udev                     10240       104     10136   2% /dev
/dev/hda1             19542436   4209228  15333208  22% /
/dev/sdb1              3936736    897568   3039168  23% /media/KINGSTON
/dev/sda1            488384000 270011588 218372412  56% /media/disk
/dev/hda6             58133024  53965408   4167616  93% /mnt/win
/dev/hdc               4588298   4588298         0 100% /media/cdrom0

and for disk with most free space

$df | sort -k 4 -n

if you want to see processes using large memory use

$ps -A v |sort -k 9 -nr|more

3341 ?        S      0:08     76  1001 81054 35696  7.3 python /usr/bin/comix
2674 ?        S      0:17     83  1154 79481 32204  6.6 nautilus –no-default-window –sm-client-id default2
3325 ?        Sl     0:07    152    58 96405 30412  6.2 /usr/bin/../lib/kompozer/kompozer-bin
2671 ?        S      0:07     58   459 36640 20896  4.3 gnome-panel –sm-client-id default1
3141 ?        Sl     0:02     56   226 41329 19844  4.0 gnome-terminal
2487 tty7     Ss+    0:53     65  1591 54696 16044  3.3 /usr/bin/X :0 -audit 0 -auth /var/lib/gdm/:0.Xauth -nolisten tcp vt7
2766 ?        Sl     0:00      3    37 35430 14420  2.9 /usr/lib/gnome-applets
--More--

Anyway, you know now how sort is helpful.

above examples are on piping the output of one tool to other tool using ‘ | ‘References

Categories
cheat sheets commands Info

Dpkg Cheat Sheet


Dpkg Cheat Sheet.

Dpkg is used to manage software management in debian or to install .deb packages. One can view what packages are installed on the system or get information about a package using dpkg. It is very simple and straight forward. Dpkg is like rpm (redhat package manager ) but used in debian like systems. Here is a small list of useful options that make you life peaceful while dealing with installation of .deb packages.


Dpkg Cheat Sheet.

Syntax Description example
dpkg
-i package-name.deb
Install
a new package or upgrade an existing package
dpkg
-i AdobeReader.deb
dpkg -r package-name remove/delete a package
leaving configuration files
dpkg -r d4x
dpkg -P package-name remove/delete a package
and all the configuration files
dpkg -P d4x
dpkg
-R directory
Install
packages from a particular directory recursively.
dpkr
-R /root/debpackages/
dpkg -l lists all packages
installed with description and version information
dpkg -l

dkpg -l|more

dpkg -l|less

dpkg -l *d4x*

dpkg -l package-name List individual installed
packages, along with package version,status of the package and short
description. The output format is same as dpkg -l
dpkg -l d4x
dpkg -L package-name Lists where the files that
come with the package are installed.
dpkg
-L d4x
dkpg -c package.deb List all the files that
are copied or installed in the system with the place where they are
installed
dpkg -c gimp-1.1_i386.deb
dpkg
-S /path-to-file
Lists which package owns
the file.
dpkg
-S /usr/bin/acroread
dpkg -s package-name

or

dpkg -s package| grep Status

gives the status of the
package and other information like

Package, Status, Priority, Section (category of package),
Installed-Size, Maintainer, Architecture, Version, Dependencies
& Description

dpkg -s d4x

or

dpkg -s d4x|grep Status

dpkg -p package-name Gives the information about a package.Package,
Priority, Section (category of package),
Installed-Size, Maintainer, Architecture, Version, Dependencies
&
Description

dpkg -p d4x

Output for dkpg -l|more

Desired=Unknown/Install/Remove/Purge/Hold

|
Status=Not/Inst/Cfg-files/Unpacked/Failed-cfg/Half-inst/trig-aWait/Trig-pend

|/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err:
uppercase=bad)

||/
Name               Version            Description

+++-==============-================================-=============================

ii 
acpi              
0.09-4             displays
information on ACPI devices

ii 
acpid             
1.0.6-8            Utilities
for using ACPI power management

ii 

adduser           
3.107              add
and remove users and groups

ii  adobereader-enu   
8.1.2              Adobe
Reader allows you to view navigate and

ii 
alacarte         

 0.11.3-1           easy
GNOME menu editing tool

ii 
alsa-base          1.0.16-1.1        
ALSA driver configuration files

ii 
alsa-utils         1.0.16-1           ALSA
utilities

Return to top

Output for dpkg -l *d4x*
the output for dpkg -l d4x also will be like below but only
information for d4x package is displayed

Desired=Unknown/Install/Remove/Purge/Hold

|
Status=Not/Inst/Cfg-files/Unpacked/Failed-cfg/Half-inst/trig-aWait/Trig-pend

|/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err:
uppercase=bad)

||/
Name       
   

 Version       Description

+++-===========-======================-===================================

ii 
d4x              2.5.7.1-5     graphical
download manager

ii 
d4x-common       2.5.7.1-5     graphical
download manager - common files

Return to top

Output for dpkg -L d4x
will be like

/.

/usr

/usr/share

/usr/share/pixmaps

/usr/share/pixmaps/nt.xpm

/usr/share/pixmaps/nt.png

/usr/share/locale

/usr/share/locale/ca

/usr/share/locale/ca/LC_MESSAGES

/usr/share/locale/ca/LC_MESSAGES/d4x.mo

/usr/share/locale/zh_CN

/usr/share/locale/zh_CN/LC_MESSAGES

/usr/share/locale/zh_CN/LC_MESSAGES/d4x.mo

/usr/share/locale/de

/usr/share/locale/de/LC_MESSAGES

/usr/share/locale/de/LC_MESSAGES/d4x.mo

/usr/share/locale/ru

/usr/share/locale/ru/LC_MESSAGES

/usr/share/locale/ru/LC_MESSAGES/d4x.mo

/usr/share/locale/bg

/usr/share/locale/bg/LC_MESSAGES

–More–

Return to top

output for dpkg -S
/usr/bin/acroread

adobereader-enu:
/usr/bin/acroread

output for dpkg
-s d4x
 

Package: d4x
Status: install ok installed
Priority: optional
Section: net
Installed-Size: 2028
Maintainer: Debian QA Group Architecture: i386
Version: 2.5.7.1-5
Depends:
libao2 (>= 0.8.8), libatk1.0-0 (>= 1.20.0), libc6 (>=
2.6.1-1), libcairo2 (>= 1.4.0), libfontconfig1 (>= 2.4.0),
libgcc1 (>= 1:4.2.1), libglib2.0-0 (>= 2.14.0), libgtk2.0-0
(>= 2.12.0), libpango1.0-0 (>= 1.18.2), libssl0.9.8 (>=
0.9.8f-1), libstdc++6 (>= 4.2.1), libx11-6, libxcomposite1 (>=
1:0.3-1), libxcursor1 (>> 1.1.2), libxdamage1 (>= 1:1.1),
libxext6, libxfixes3 (>= 1:4.0.1), libxi6, libxinerama1, libxrandr2
(>= 2:1.2.0), libxrender1, d4x-common (= 2.5.7.1-5)
Description: graphical download manager
 Downloader for X is a powerful graphical download manager.
 It supports both HTTP(S) and FTP protocols and has nice graphical
 user interface, though some actions can also be performed using
 the command line.
 .
 Among others, its key features include proxy and SOCKS5 support,
 recursive downloading, wildcard matching, download scheduler,
 multiple download queues and more…
 .
 Homepage: http://www.krasu.ru/soft/chuchelo/

Return to top

Output for dpkg -p comix

Package: comix
Priority: optional
Section: x11
Installed-Size: 1416
Maintainer: Emfox Zhou
Architecture: all
Version: 3.6.4-1.1
Depends: gconf2 (>= 2.10.1-2), python (>= 2.3), python-gtk2 (>= 2.8), python-imaging (>= 1.1.4)
Suggests: libjpeg-progs, unrar
Size: 234988
Description: GTK Comic Book Viewer
 Comix is a comic book viewer. It reads zip, rar, tar, tar.gz and
 tar.bz2 archives (often called .cbz, .cbr and .cbt) as well as
 normal image files. It is written in Python and has a simple user
 interface using PyGTK.
 .
 Main Features:
 .
   * Fullscreen mode.
   * Double page mode.
   * Fit-to-screen mode.
   * Zooming and scrolling.
   * Rotation and mirroring.
   * Magnification lens.
   * Changeable image scaling quality.
   * Image enhancement.
   * Can read right-to-left to fit manga etc.
   * Caching for faster page flipping.
   * Bookmarks support.
   * Customizable GUI.
   * Archive comments support.
   * Archive converter.
   * Thumbnail browser.
   * Standards compliant.
   * Translated to English, Swedish, Simplified Chinese, Spanish,
     Brazilian Portuguese and German.
   * Reads the JPEG, PNG, TIFF, GIF, BMP, ICO, XPM and XBM image formats.
   * Reads ZIP and tar archives natively, and RAR archives through the unrar
     program.
   * Runs on Linux, FreeBSD, NetBSD and virtually any other UNIX-like OS.
   * More!

Return to top

Categories
commands system monitoring

ps command

The ps command for system monitoring.
Not All process can be spotted by top command. If you want to catch a hanged appplication’s PID you need ps. ps is very powerful and has a large no of options like any other linux tools.
suppose you need pid of mplayer which hanged the type

$ps -ef | egrep mplayer

and you will get the PID of mplayer

nandam    3359     1  1 06:55 ?        00:00:00 gmplayer
nandam    3383  3364  0 06:56 pts/9    00:00:00 egrep mplayer

now you can kill the mplayer using kill command

$kill 3359

Here are the most common usages of ps command

To see every process on the system using standard syntax us any one of the following:
          ps -e
          ps -ef
          ps -eF
          ps -ely

To see every process on the system using BSD syntax:
          ps ax
          ps axu

-e   —–> Select all processes
-f    —–> full format listing
-F   —–> extra full format
-l   ——> long format. The -y option is often useful with this
-y  ——> Do not show flags; show rss in place of addr. This option can only be used with -l.

Here is sample output for each of above commands

$ps -e

  PID TTY          TIME CMD
    1 ?        00:00:01 init
    2 ?        00:00:00 migration/0
    3 ?        00:00:00 ksoftirqd/0
    4 ?        00:00:00 migration/1
    5 ?        00:00:01 ksoftirqd/1
    6 ?        00:00:00 events/0
    7 ?        00:00:00 events/1
    8 ?        00:00:00 khelper
    9 ?        00:00:00 kthread
   13 ?        00:00:00 kblockd/0
   14 ?        00:00:00 kblockd/1
   15 ?        00:00:00 kacpid
   92 ?        00:00:00 kseriod
  134 ?        00:00:00 pdflush
  135 ?        00:00:00 pdflush
  136 ?        00:00:00 kswapd0
  137 ?        00:00:00 aio/0
  138 ?        00:00:00 aio/1
  282 ?        00:00:00 kirqd
  547 ?        00:00:00 khubd

$ps -ef

UID        PID  PPID  C STIME TTY          TIME CMD
root         1     0  0 03:53 ?        00:00:01 init [2] 
root         2     1  0 03:53 ?        00:00:00 [migration/0]
root         3     1  0 03:53 ?        00:00:00 [ksoftirqd/0]
root         4     1  0 03:53 ?        00:00:00 [migration/1]
root         5     1  0 03:53 ?        00:00:01 [ksoftirqd/1]
root         6     1  0 03:53 ?        00:00:00 [events/0]
root         7     1  0 03:53 ?        00:00:00 [events/1]
root         8     1  0 03:53 ?        00:00:00 [khelper]
root         9     1  0 03:53 ?        00:00:00 [kthread]
root        13     9  0 03:53 ?        00:00:00 [kblockd/0]
root        14     9  0 03:53 ?        00:00:00 [kblockd/1]
root        15     9  0 03:53 ?        00:00:00 [kacpid]
root        92     9  0 03:53 ?        00:00:00 [kseriod]
root       134     9  0 03:53 ?        00:00:00 [pdflush]
root       135     9  0 03:53 ?        00:00:00 [pdflush]
root       136     9  0 03:53 ?        00:00:00 [kswapd0]
root       137     9  0 03:53 ?        00:00:00 [aio/0]
root       138     9  0 03:53 ?        00:00:00 [aio/1]
root       282     1  0 03:53 ?        00:00:00 [kirqd]
root       547     9  0 03:53 ?        00:00:00 [khubd]
root       754     9  0 03:53 ?        00:00:00 [scsi_eh_2]

$ps -eF

UID        PID  PPID  C    SZ   RSS PSR STIME TTY          TIME CMD
root         1     0  0   510   720   1 03:53 ?        00:00:01 init [2] 
root         2     1  0     0     0   0 03:53 ?        00:00:00 [migration/0]
root         3     1  0     0     0   0 03:53 ?        00:00:00 [ksoftirqd/0]
root         4     1  0     0     0   1 03:53 ?        00:00:00 [migration/1]
root         5     1  0     0     0   1 03:53 ?        00:00:01 [ksoftirqd/1]
root         6     1  0     0     0   0 03:53 ?        00:00:00 [events/0]
root         7     1  0     0     0   1 03:53 ?        00:00:00 [events/1]
root         8     1  0     0     0   1 03:53 ?        00:00:00 [khelper]
root         9     1  0     0     0   1 03:53 ?        00:00:00 [kthread]
root        13     9  0     0     0   0 03:53 ?        00:00:00 [kblockd/0]
root        14     9  0     0     0   1 03:53 ?        00:00:00 [kblockd/1]
root        15     9  0     0     0   1 03:53 ?        00:00:00 [kacpid]
root        92     9  0     0     0   0 03:53 ?        00:00:00 [kseriod]
root       134     9  0     0     0   1 03:53 ?        00:00:00 [pdflush]
root       135     9  0     0     0   0 03:53 ?        00:00:00 [pdflush]
root       136     9  0     0     0   1 03:53 ?        00:00:00 [kswapd0]
root       137     9  0     0     0   0 03:53 ?        00:00:00 [aio/0]
root       138     9  0     0     0   1 03:53 ?        00:00:00 [aio/1]
root       282     1  0     0     0   1 03:53 ?        00:00:00 [kirqd]
root       547     9  0     0     0   1 03:53 ?        00:00:00 [khubd]
root       754     9  0     0     0   1 03:53 ?        00:00:00 [scsi_eh_2]
root       755     9  0     0     0   1 03:53 ?        00:00:02 [usb-storage]
root       768     9  0     0     0   1 03:53 ?        00:00:00 [scsi_eh_3]

$ps -ely

S   UID   PID  PPID  C PRI  NI   RSS    SZ WCHAN  TTY          TIME CMD
S     0     1     0  0  75   0   720   510 -      ?        00:00:01 init
S     0     2     1  0 -40   -     0     0 migrat ?        00:00:00 migration/0
S     0     3     1  0  94  19     0     0 ksofti ?        00:00:00 ksoftirqd/0
S     0     4     1  0 -40   -     0     0 migrat ?        00:00:00 migration/1
S     0     5     1  0  97  19     0     0 ksofti ?        00:00:01 ksoftirqd/1
S     0     6     1  0  70  -5     0     0 worker ?        00:00:00 events/0
S     0     7     1  0  70  -5     0     0 worker ?        00:00:00 events/1
S     0     8     1  0  70  -5     0     0 worker ?        00:00:00 khelper
S     0     9     1  0  70  -5     0     0 worker ?        00:00:00 kthread
S     0    13     9  0  72  -5     0     0 worker ?        00:00:00 kblockd/0
S     0    14     9  0  70  -5     0     0 worker ?        00:00:00 kblockd/1
S     0    15     9  0  75  -5     0     0 worker ?        00:00:00 kacpid
S     0    92     9  0  71  -5     0     0 serio_ ?        00:00:00 kseriod
S     0   134     9  0  75   0     0     0 pdflus ?        00:00:00 pdflush
S     0   135     9  0  75   0     0     0 pdflus ?        00:00:00 pdflush
S     0   136     9  0  70  -5     0     0 kswapd ?        00:00:00 kswapd0
S     0   137     9  0  76  -5     0     0 worker ?        00:00:00 aio/0
S     0   138     9  0  77  -5     0     0 worker ?        00:00:00 aio/1
S     0   282     1  0  75   0     0     0 -      ?        00:00:00 kirqd
S     0   547     9  0  70  -5     0     0 hub_th ?        00:00:00 khubd
S     0   754     9  0  71  -5     0     0 scsi_e ?        00:00:00 scsi_eh_2
S     0   755     9  0  70  -5     0     0 -      ?        00:00:02 usb-storage

As you can see the output varies with each options.

You can also try

$ watch -n 10 ps -eF

which runs ps -eF for every 10 secs.
 in the way you can monitor the system and you can use sort command to sort the report in desc or ascending order of various columns.

Apart from ps there are othe utilities that are helpful in system monitoring.
pstree
top
disk monitoring utilities

References
ps manual
watch manual

Categories
commands system monitoring

The Top command

top command for system monitoring.
TOP is one of the system load monitoring command.Others are ps ,iostat and vmstat.
using top command you can kill the process causing high load on the system.

$top

enter top and you will see the following screen

top - 05:44:25 up  6:32,  2 users,  load average: 0.06, 0.18, 0.15
Tasks:  90 total,   1 running,  89 sleeping,   0 stopped,   0 zombie
Cpu(s):  0.8%us,  0.0%sy,  0.0%ni, 99.0%id,  0.0%wa,  0.2%hi,  0.0%si,  0.0%st
Mem:    484188k total,   476212k used,     7976k free,    63728k buffers
Swap:   489940k total,      104k used,   489836k free,   214956k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND           
 3663 nandam    15   0 36852  15m 9.8m S    1  3.2   0:00.32 gnome-terminal    
 2493 root      15   0 63456  22m 8716 S    0  4.7   5:55.30 Xorg              
 2668 nandam    15   0 15328 2272 1436 S    0  0.5   0:11.07 gnome-screensav   
    1 root      15   0  2036  720  620 S    0  0.1   0:01.28 init              
    2 root      RT   0     0    0    0 S    0  0.0   0:00.00 migration/0       
    3 root      34  19     0    0    0 S    0  0.0   0:04.44 ksoftirqd/0       
    4 root      RT   0     0    0    0 S    0  0.0   0:00.00 migration/1       
    5 root      34  19     0    0    0 S    0  0.0   0:00.48 ksoftirqd/1       
    6 root      10  -5     0    0    0 S    0  0.0   0:00.12 events/0          
    7 root      10  -5     0    0    0 S    0  0.0   0:00.00 events/1          
    8 root      10  -5     0    0    0 S    0  0.0   0:00.00 khelper           
    9 root      10  -5     0    0    0 S    0  0.0   0:00.00 kthread           
   13 root      12  -5     0    0    0 S    0  0.0   0:00.00 kblockd/0         
   14 root      10  -5     0    0    0 S    0  0.0   0:00.00 kblockd/1         
   15 root      15  -5     0    0    0 S    0  0.0   0:00.00 kacpid            
   92 root      10  -5     0    0    0 S    0  0.0   0:00.00 kseriod           
  134 root      15   0     0    0    0 S    0  0.0   0:00.19 pdflush  
         

You can see the process are listed in descending order of CPU usage. Top always displays a dyanamic list of processes. In the top portion of the report it displays the total uptime of the system, no of users , no of processes running and the total CPU,memory  adn swap space usage of the system.

You can sort the output in the descending order of memory usage by hitting M. to return to intial report hit M again.
if dont want the idle processes to be displayed hit i. To return to initial output press i again.

If you want to see the process of  single user, press u and enter the user name and to revert back press u again and donot enter any username.

You can kill the process by pressing K and enter the process id .

For More info refer top manual

to exit from TOP command press ‘q’ or ctrl+c.

Categories
commands

Crontab !!

Contents

Crontab Basics

crontab is most powerful yet very simple.this little tool is used to set or schedule periodic or one time jobs in linux..

it is not that complicated..

Here is a summary of commands which are used to configure jobs with crontab

crontab -e (edit user’s crontab)
crontab -l (list user’s crontab)
crontab -r (delete user’s crontab)
crontab -i (prompt before deleting user’s crontab)

to create a new periodic or schedule task you need to edit the crontab file .
enter

$crontab -e

the configuration file will be opened in nano

ok these are the fields you have to fill.. any way

# m h dom mon dow command

m —— minute (0-59)
h —— hour (0 to 23)
dom —— day of month (1to31)
mon —— month (1-12)
dow —— day of week (0 to 6 , 0 –> sunday )
command ——- program to be run

note:- all fields are mandatory
Go to Top

Alarm

lets write a script that plays an audio file on evoking it..
like below

#!/bin/sh
mplayer audiofile.mp3

lets save this as file alarm

as owner of file you can make the file executable by entering

$ chmod a+x alarm

ps: both file alarm & audiofile.mp3 must be in same directory

ok now you want it to ring at every morning 7:45
Then here is the line here you have to enter

# m h dom mon dow command
45 7 * * * ~/alarm

you have an alarm now free of cost!!

Other utilities?
Go to Top

Upgrading System

take this script for example;

#!/bin/sh
apt-get update
apt-get -y upgrade
apt-get clean
apt-get autoclean

save above script in auto-upgrade.sh make it an executable like before and save it in /root directory

now i set my pc’s root crontab as

# m h dom mon dow command
00 00 * * 6 /root/auto-upgrade.sh

You can see my auto upgrade program runs every saturday at midnight and keep my system uptodate!!
Go to Top
if you can mix shell scripting and crontab you do a lot of cool things..

Website Backup

you can also take a periodic backup of your website using wget and crontab.

#!/bin/sh
dir=`date -d now +%d-%m-%y`
mkdir $dir
cd $dir
wget -r http://link-to-your-website

save it as websitebackup.sh

and set it in crontab to every sunday night

# m h dom mon dow command
00 00 * * 0 /home/myuserid/websitebackup.sh

well you have your website weekly backup now for you.!!
any way you can see now how powerful this simple tool can be.

You can also schedule a periodic back up of your important files in the above manner.
Go to Top
References

crontab command manual
wget command manual
date command manual
apt-get manual

Categories
commands

pstree command

pstree command show the processes and parent-child relationship
It just shows the running process in a tree..
here is an example of output what you get when you run this command…

$pstree

init-+-NetworkManager---{NetworkManager}
|-NetworkManagerD
|-acpid
|-atd
|-avahi-daemon---avahi-daemon
|-bonobo-activati---{bonobo-activati}
|-cron
|-cupsd
|-2*[dbus-daemon]
|-dhcdbd
|-epiphany-browse---{epiphany-browse}
|-events/0
|-events/1
|-exim4
|-gconfd-2
|-gdm---gdm-+-Xorg
| `-x-session-manag-+-gnome-panel
| |-gnome-settings----{gnome-settings-}
| |-metacity
| |-nautilus
| |-nm-applet
| |-seahorse-agent
| |-ssh-agent
| `-{x-session-manag}
|-gedit
|-6*[getty]
|-gnome-keyring-d
|-gnome-screensav
|-gnome-terminal-+-bash---su---bash---pstree
| |-bash
| |-gnome-pty-helpe
| `-{gnome-terminal}
|-gnome-vfs-daemo
|-gnome-volume-ma
|-hald---hald-runner-+-hald-addon-acpi
| |-hald-addon-inpu
| `-2*[hald-addon-stor]
|-inetd
|-khelper
|-kirqd
|-klogd
|-ksoftirqd/0
|-ksoftirqd/1
|-kthread-+-aio/0
| |-aio/1
| |-kacpid
| |-kblockd/0
| |-kblockd/1
| |-khubd
| |-kmirrord
| |-kpsmoused
| |-kseriod
| |-kswapd0
| |-2*[pdflush]
| |-reiserfs/0
| |-reiserfs/1
| |-scsi_eh_0
| |-scsi_eh_1
| `-2*[usb-storage]
|-mapping-daemon
|-migration/0
|-migration/1
|-mixer_applet2---{mixer_applet2}
|-mount.ntfs-3g
|-pcscd---{pcscd}
|-portmap
|-realplay---realplay.bin-+-2*[realplay.bin]
| `-8*[{realplay.bin}]
|-rpc.statd
|-syslogd
|-system-tools-ba---dbus-daemon
`-udevd

if you need the command with which the program is invokes use -a & if you need process id (PID) of the process use -p.
you can combine both the commands..

enter and you will see the output as below.


$pstree -ap

init,1
|-NetworkManager,2275 --pid-file /var/run/NetworkManager/NetworkManager.pid
| `-{NetworkManager},2282
|-NetworkManagerD,2283 --pid-file /var/run/NetworkManager/NetworkManagerDispatcher.pid
|-acpid,1846 -c /etc/acpi/events -s /var/run/acpid.socket
|-atd,2352
|-avahi-daemon,2194
| `-avahi-daemon,2195
|-bonobo-activati,3427 --ac-activate --ior-output-fd=16
| `-{bonobo-activati},3430
|-cron,2371
|-cupsd,1888
|-dbus-daemon,1858 --system
|-dbus-daemon,3400 --fork --print-address 25 --print-pid 27 --session
|-dhcdbd,2215 --system
|-epiphany-browse,3494 file:///home/nandam/pstree.html
| `-{epiphany-browse},3496
|-(events/0,6)
|-(events/1,7)
|-exim4,2154 -bd -q30m
|-gconfd-2,3318 14
|-gdm,2308
| `-gdm,2309
| |-Xorg,3321 :0 -audit 0 -auth /var/lib/gdm/:0.Xauth -nolisten tcp vt7
| `-x-session-manag,3338
| |-gnome-panel,3418 --sm-client-id default1
| |-gnome-settings-,3401
| | `-{gnome-settings-},3404
| |-metacity,3415 --sm-client-id=default0
| |-nautilus,3419 --no-default-window --sm-client-id default2
| |-nm-applet,3434 --sm-disable
| |-seahorse-agent,3390 --execute x-session-manager
| |-ssh-agent,3384 /usr/bin/seahorse-agent --execute x-session-manager
| `-{x-session-manag},3402
|-getty,2387 38400 tty1
|-getty,2388 38400 tty2
|-getty,2389 38400 tty3
|-getty,2390 38400 tty4
|-getty,2391 38400 tty5
|-getty,2392 38400 tty6
|-gnome-keyring-d,3395
|-gnome-screensav,3414
|-gnome-terminal,3525
| |-bash,3530
| | `-pstree,3551 -ap
| |-gnome-pty-helpe,3529
| `-{gnome-terminal},3531
|-gnome-vfs-daemo,3429
|-gnome-volume-ma,3435 --sm-disable
|-hald,2225
| `-hald-runner,2226
| |-hald-addon-acpi,2254
| |-hald-addon-inpu,2246
| |-hald-addon-stor,2260
| `-hald-addon-stor,2262
|-iceape-bin,3503 -edit
| |-{iceape-bin},3507
| |-{iceape-bin},3509
| `-{iceape-bin},3528
|-inetd,2183
|-(khelper,8)
|-(kirqd,282)
|-klogd,1678 -x
|-(ksoftirqd/0,3)
|-(ksoftirqd/1,5)
|-(kthread,9)
| |-(aio/0,137)
| |-(aio/1,138)
| |-(kacpid,15)
| |-(kblockd/0,13)
| |-(kblockd/1,14)
| |-(khubd,542)
| |-(kmirrord,1527)
| |-(kpsmoused,1163)
| |-(kseriod,92)
| |-(kswapd0,136)
| |-(pdflush,134)
| |-(pdflush,135)
| |-(reiserfs/0,735)
| |-(reiserfs/1,736)
| |-(scsi_eh_0,615)
| |-(scsi_eh_1,631)
| |-(usb-storage,628)
| `-(usb-storage,632)
|-mapping-daemon,3448
|-(migration/0,2)
|-(migration/1,4)
|-mixer_applet2,3455 --oaf-activate-iid=OAFIID:GNOME_MixerApplet_Factory --oaf-ior-fd=19
| `-{mixer_applet2},3468
|-mount.ntfs-3g,2558 /dev/sdb1 /media/disk -o rw,nosuid,nodev,uhelper=hal,locale=en_US.UTF-8
|-pcscd,2337
| `-{pcscd},2344
|-portmap,1612
|-rpc.statd,2171
|-syslogd,1669
|-system-tools-ba,2320
| `-dbus-daemon,2321 --session --print-address --nofork
`-udevd,831 --daemon

pstree manual