logo
  • Home
  • About Us

Category Archives: Tips&Tricks

Linux-Some handy commands

How to check which process is using up the most disk resources
iotop -Use the “input output top” command to investigate which process is doing the most IO in real time.

#iotop

Total DISK READ: 0.00 B/s | Total DISK WRITE: 15.57 K/s
TID PRIO USER DISK READ DISK WRITE SWAPIN IO> COMMAND
946 be/3 root 0.00 B/s 3.89 K/s 0.00 % 0.07 % [jbd2/dm-4-8]
27526 be/4 root 0.00 B/s 3.89 K/s 0.00 % 0.00 % rotatelogs /opt/www/logs/webserver/apache/access_log-%Y-%m-%d-%H 86400
27527 be/4 root 0.00 B/s 3.89 K/s 0.00 % 0.00 % rotatelogs /opt/www/logs/webserver/apache/mod_jk.log-%Y-%m-%d-%H 86400

Capture OS level data to show CPU and Memory usage Thread wise

#top -b -n 1 -H

Or for a specific Process

#top -b -n 1 -H <Process_PID>

Top 10 CPU consuming Process

#ps -eo pcpu,pid,user,args | sort -k 1 -r | head -10

Comparison of CPU utilization

# sar -u 5 10 ( 5 sec of interval & 10 times )

Ho to check Linux machine is Physical or Virtual remotely?

We can use the command :-

[[email protected] ~]# dmidecode -s system-product-name
ProLiant DL360 G7

[[email protected] ~]# dmidecode -s system-product-name
VMware Virtual Platform

[[email protected] ~]# dmidecode -s system-manufacturer
Red Hat

First is HP ProLiant, second one is VM ware and Third one is KVM Instance

Install local rpm files using yum

We can use the command yum localinstall /path/to/file.rpm file. This command will install the local rpm file and search for required rpms (dependencies) on RHN or other repositories that are configured and install it for the user.

#yum localinstall /path/to/RPM_FILE.rpm

Some awk & sed

Awk is very useful for field like cut and stripping in a more powerful way. One-liners for awk for some cool examples.

awk ‘{ print $2, $1 }’ file               # Print and inverse first two columns
awk ‘{printf(“%5d : %s\n”, NR,$0)}’ file  # Add line number left aligned
awk ‘{print FNR “\t” $0}’ files           # Add line number right aligned
awk NF test.txt                           # remove blank lines (same as grep ‘.’)
awk ‘length > 50’                         # print line longer than 50 char

Some sed commands

sed ‘s/string1/string2/g’                 # Replace string1 with string2
sed -i ‘s/wroong/wrong/g’ *.txt           # Replace a recurring word with g
sed ‘s/\(.*\)1/\12/g’                     # Modify anystring1 to anystring2
sed ‘/ *#/d; /^ *$/d’                     # Remove comments and blank lines
sed ‘s/[ \t]*$//’                         # Remove trailing spaces (use tab as \t)
sed ‘s/^[ \t]*//;s/[ \t]*$//’             # Remove leading and trailing spaces

Redirects-Pipes and Special Variables

Redirects and Pipes for bash and sh:

# cmd 1> file   # Redirect stdout to file.
# cmd 2> file   # Redirect stderr to file.
# cmd 1>> file  # Redirect and append stdout to file.
# cmd &> file   # Redirect both stdout and stderr to file.
# cmd >file 2>&1   # Redirects stderr to stdout and then to file.
# cmd1 | cmd2      # pipe stdout to cmd2
# cmd1 2>&1 | cmd2 # pipe stdout and stderr to cmd2

The command line arguments are

$0, $1, $2, … # $0 is the command itself
$# # The number of arguments
$* # All arguments (also [email protected])

Special Variables
$$ # The current process ID
$? # exit status of last command

Difference between /dev/null and /dev/zero files

/dev/zero and /dev/null are two pseudo files which are useful for creating empty files.

/dev/zero: This file is used to create a file with no data but with required size(A file with all zero’s).
In other words this will create a data file with all zeros in the file which will give the size to a file.

Create a file with /dev/zero file

#dd if=/dev/zero of=/opt/file.txt bs=4096 count=1000

So we conclude that /dev/zero is a file use full for creating a file with some required size without any meaning to the data.

/dev/null: This is one more Pseudo file which is useful in many places like redirecting unwanted output/error etc to this file.
This file acts as a black hole(Which eat up everything and do not show any output).
So whenever you feed some data to this file, you can not retrieve the data which is fed to it. This file even useful for creating files with zero size.

Extract just one File from RPM

If you need to extract just one file from an rpm without reinstalling the whole package, you can do this with rpm2cpio.

For example, to extract just the config file from the httpd rpm you would use the following statement:

‪#‎rpm2cpio‬ httpd-2.0.51-1.i386.rpm |cpio -ivd etc/httpd.conf

Delete all the files except few

Delete all the files except few

Some times there are thousands of files and you want to delete all the files except .conf and .html files.

You can do –

‪#‎rm‬ !(*.conf|*.html)

Want to install a package other than default directory

Want to install a package other than default directory 

Use :-
1.  rpm -ivh –prefix==<directory-name> <rpmname>
e.g rpm –prefix=/home/chroot/ mysql-server*.rpm
2.  yum –installroot=<directory-name> install <package-name>
e.g yum –installroot=/opt/linux/ install mysql-server

Note- Packages that can be installed on diff directory are called “relocatable packages”. Above commands works only for these type of packages.and not for all.

CPU‬ Load in Linux

The load average represents the work being done by the system. The three numbers show the load averages for the last minute, 5 minutes and 15 minutes, respectively. A load average of 1 reflects the full workload of a single processor on the system. A load of 2 on a system with two CPUs means that those CPUs were working at maximum. On a system with four CPUs, that 2 reflects a workload using about half of the available processing power.

‪#‎w‬
load average: 1.45, 0.80, 3.07

load average over the last 1 minute: 1.45

load average over the last 5 minutes: 0.80

load average over the last 15 minutes: 3.07

To understand the load average number, you need to know how many CPUs your system has. A load average of 7.05 would indicate a system with a single CPU was massively overloaded, but it would be fine on a computer with 8 CPUs.

Command to get today’s CPU load average statistics:

‪#‎sar‬ -q | less

Next Posts

Like Us

Archives

  • December 2015 (4)
  • October 2015 (12)
  • September 2015 (2)

Categories

  • Apache
  • Info
  • Java
  • JBoss
  • Linux
  • Tips&Tricks
  • About Us