Monday, October 06, 2008

Extract a tar.gz File in a Single Command [Unix]

The obvious way to extract a tar.gz archive is using the following two commands:
sharfah@starship:~> gunzip foo.tar.gz
sharfah@starship:~> tar xvf foo.tar
This is how you can achieve the same thing, but in a single command:
sharfah@starship:~> gunzip -c foo.tar.gz | tar xvf -
or alternatively:
sharfah@starship:~> gunzip < foo.tar.gz | tar xvf -
Both commands write the data produced from the gunzip command to standard out (-c in the first example and using < in the second). The output is then piped into the tar command. The "-" represents standard input.

Creating a tar.gz:
A tar.gz file is normally created using two commands as follows:

sharfah@starship:~> tar cvf foo.tar foodir
sharfah@starship:~> gzip foo.tar
Again, there is a single command to do the same thing:
sharfah@starship:~> tar cvf - foodir | gzip > foo.tar.gz

Friday, October 03, 2008

Checking CPU Utilisation on Linux

Here are a few commands which can be used to investigate CPU utilisation on Linux:

top
The top program provides a dynamic real-time view of a running system. It can display system summary information as well as a list of tasks currently being managed by the Linux kernel. The CPU usage shows the task's share of the elapsed CPU time since the last screen update, expressed as a percentage of total CPU time.

top - 18:12:28 up 40 days, 18:35,  1 user,  load average: 0.13, 0.03, 0.01
Tasks:  98 total,   1 running,  96 sleeping,   0 stopped,   1 zombie
Cpu(s):  0.2% us,  0.0% sy,  0.0% ni, 99.4% id,  0.4% wa,  0.0% hi,  0.0% si
Mem:   8002512k total,  3845332k used,  4157180k free,    64624k buffers
Swap:  9437144k total,   771048k used,  8666096k free,  1831288k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
    1 root      16   0   640   80   48 S  0.0  0.0  43:04.18 init
    2 root      RT   0     0    0    0 S  0.0  0.0   0:00.70 migration/0
    3 root      34  19     0    0    0 S  0.0  0.0   0:00.02 ksoftirqd/0
    4 root      RT   0     0    0    0 S  0.0  0.0   0:00.58 migration/1
    5 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/1
    6 root      RT   0     0    0    0 S  0.0  0.0   0:00.40 migration/2
    7 root      34  19     0    0    0 S  0.0  0.0   0:00.00 ksoftirqd/2
    8 root      RT   0     0    0    0 S  0.0  0.0   0:00.46 migration/3

sar
The sar command can be used to display a history of CPU usage:
10:00:01   CPU     %user     %nice   %system   %iowait   %idle
10:10:01   all     34.45      2.04     30.03      0.05   33.43
10:20:01   all     34.13      1.77     29.85      0.08   34.17

mpstat
The mpstat command can be used to show the percentage of CPU usage for each processor:
> mpstat -P ALL

18:02:42     CPU   %user   %nice %system %iowait    %irq
18:02:42     all   24.77   17.51   21.56    1.19    0.01
18:02:42       0   25.00   17.92   20.27    0.94    0.00
18:02:42       1   23.81   17.03   20.87    0.88    0.00
18:02:42       2   26.28   16.44   22.23    1.54    0.01
18:02:42       3   24.00   18.65   22.86    1.39    0.01
Order ps output
The following command displays the top 10 CPU users on your system. It involves listing the processes using ps and then sorting them by CPU usage:
> ps -eo pcpu,pid,user,cmd | sort -k 1 -r | head -10

%CPU   PID USER     CMD
 0.1    13 root     [events/3]
 0.1   103 root     [kswapd0]
 0.1   102 root     [kswapd1]
 0.0     9 root     [ksoftirqd/3]
 0.0     8 root     [migration/3]
 0.0  8982 root     [lockd]
 0.0  8981 root     [rpciod]
 0.0     7 root     [ksoftirqd/2]
 0.0    75 root     [kblockd/3]