Labels

Tuesday, June 19, 2012

Daily most Useful Unix commands

CPU Usage
The prstat command displays information about active processes on the system and resource consumption. By default, prstat displays information about all processes sorted by CPU usage.

The prstat -s cpu -n 5 command is used to list the five processes that are consuming the most CPU resources. The -s cpu flag tells prstat to sort the output by CPU usage (This is a default flag set, so can be skipped). The -n 5 flag tells prstat to restrict the output to the top five processes.

Adding the -a option to any prstat command will identify how many processes each user is using, what percent of the CPUs, and how much memory, they are using on a system

prstat -a 

The ps command displays information about currently running processes. Pipe the output to search for specific processes

ps -ef  | grep java

/usr/ucb/ps -auxwww | grep {pid} 

This command gives the complete classpath and start parameters of the process mentioned in pid

Example:

/usr/ucb/ps -auxwww | grep 19138 


user 19138 0.4 2.31272184732136 ? S Jun 01 342:11 /resolve/j2sdk1.4.2_03/bin/java -Xmx1024m -Xms1024m -XX:NewSize=341m -XX:MaxNewSize=341m -XX:+PrintGCDetails -XX:+PrintGCTimeStamps -XX:SurvivorRatio=8 -XX:TargetSurvivorRatio=90 -XX:+DisableExplicitGC -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationConcurrentTime -XX:+PrintGCApplicationStoppedTime -Xloggc:/resolve/CPEE3112/composer/log/pa3.gc -Drmiport=61050 -Dg2config.home=/resolve/CPEE3112/composer/conf -Dg2config.baseipport=61020 -Dg2config.instance=Engine14 -Dg2.processid=3 -cp /resolve/lib/any_custom_libs.jar:/resolve/lib/G2.jar:/resolve/lib/oracle_10_2_0_1_0jdbc.jar com.xyz.platform.Main -recover &




prstat -L -p {pid}

The above cmd gives the summary of lightweight processes which make up the process. More is at an example I gave here

The top command provides an overview of CPU and memory utilization, and a list of the top consumers of CPU, it updates it's display every few seconds so you can monitor continuously. Use http://www.groupsys.com/top/display/ to view details of the stats shown

=== Find User ===

fuser displays the PIDs of processes using the specified files or file systems.
fuser
 command is very useful in getting rid of the .nfs files created when 
processes are killed. The .nfs* names refer to files that have been 
deleted but are still being held open by a running process.

You can find .nfs files using ls -la 

rm does not work in removing .nfs files - use fuser to find the process that's locking the file and then kill the process

 /usr/sbin/fuser {filename} 

 kill -9 {pid} 

If you stop the processes that have them open, the files will be tidied-up and removed by the file server.


If you're interested a bit of general background follows:

Most
 operating systems, including UNIX, operate a policy of not actually 
removing a deleted file (and freeing up it's data blocks) until the last
 process that has the file open closes it. So, if a running process has a
 file open and you use the rm(1) command to delete the file the data 
blocks on the disk will not be freed up by the OS until the process that
 has the file open closes it.

On a UNIX host using local disk 
store this behaviour can manifest itself it some seemingly confusing 
situations. For example, you may wish to free up some space on a file 
system that's used 900 MB of it's 1GB quota. You have a large file, 
200MB say, named myjava.jar that you believe is no longer required, but 
is actually currently open in your WebLogic server. Not knowing this you
 delete myjava.jar and do an ls(1) command to see that the file is no 
longer listed. However, when you use the df(1) command it still reports 
that 900 MB of it's 1GB quota is used because your WebLogic server still
 has the file open. When you shutdown the WebLogic server or the server 
closes the file the disk space will be released and df(1) will report 
700MB of it's 1GB is used.

If the file that is removed is on NFS 
mounted store then it is possible for a file to be deleted on one client
 whilst still being open on another client. In this situation the same 
rule of not actually deleting the file until the last process with it 
open closes it still applies. However, in order for the NFS file handle 
used by the client that still has the file open not to be broken a 
filename reference must be maintained. In order to achieve this and 
remove the files name from the directory ( so it doesn't show up in an 
ls command output) the NFS file server renames the deleted file to a 
name beginning '.nfs'. These are the files you are seeing. When the last
 process with these files open dies or closes them they will be tidied 
up and removed. Trying to delete them before they are closed will only 
result in the file being renamed again. 



=== Find Class within Jar file ===

To find a class within a binary jar file

 for i in `ls *.jar`; do (jar tf $i  grep '{classname}' ) && echo $i;done   

If you want to search within all subdirectories use this

 for i in `find . -name ‘*.jar’`; do (jar tf $i  grep '{CLASSNAME}' ) && echo $i;done 

=== Find string within file of name ===


find . -name *.xml -exec grep  {} \;

example

find . -name web.xml -exec grep -i servlet {} \;



=== Grep within zip file without unzipping it ===


gzip -c -d {file}.gz | grep {string}

Example

gzip -c -d admin_access.log0001_4Dec08_0147.gz | grep -i adq | wc -l







=== Find process using the port ===

The easy way to do this is using netstat passing the port number 

netstat -a | grep 61014

If that does not help getting a pid, run this line below


for i in `ls /proc`; do pfiles $i | grep AF_INET | grep 61014 ; done


Output:

pfiles: permission denied: 12363
sockname: AF_INET 0.0.0.0 port: 61014
pfiles: permission denied: 12384

this shows the process appearing in between pids 12363 and 12384 uses that port.

ls /proc
gives the PIDs in order as .. 12363, 12369, 12384 ..


ps -ef  grep 12369
wlsuser 12369 7852 0 01:07:40 ? 4:42 /opt/bea/jdk142_05/bin/java -server -DresKBD -Xms512m -Xmx512m -XX:MaxPermSize=

gave the process details which was using the port


sar -s


Memory Usage
vmstat 5 vmstat reports virtual memory statistics regarding kernel, thread, virtual memory, and disk, trap, and CPU activity. vmstat also helps calculate the average page scan rate.


Below is the vmstat output

vmstat 5



kthr memory page disk faults cpu

r b w swap free re mf pi p fr de sr s0 s1 s2 s3 in sy cs us sy id

0 0 0 11456 4120 1 41 19 1 3 0 2 0 4 0 0 48 112 130 4 14 82

0 0 1 10132 4280 0 4 44 0 0 0 0 0 23 0 0 211 230 144 3 35 62

0 0 1 10132 4616 0 0 20 0 0 0 0 0 19 0 0 150 172 146 3 33 64

0 0 1 10132 5292 0 0 9 0 0 0 0 0 21 0 0 165 105 130 1 21 78

Note: The first line of vmstat shows a cumulative value and must be ignored. sr is the pages scanned by clock algorithm.



On Unix, to find out how much RAM is present on a machine, use
prtconf grep 'Memory size:'


On Linux, the same is done using
free -m


Disk Space
df -ek

du -h
 
 
 
 
=== Find User ===

fuser displays the PIDs of processes using the specified files or file systems.
fuser
 command is very useful in getting rid of the .nfs files created when 
processes are killed. The .nfs* names refer to files that have been 
deleted but are still being held open by a running process.

You can find .nfs files using ls -la 

rm does not work in removing .nfs files - use fuser to find the process that's locking the file and then kill the process

 /usr/sbin/fuser {filename} 

 kill -9 {pid} 

If you stop the processes that have them open, the files will be tidied-up and removed by the file server.


If you're interested a bit of general background follows:

Most
 operating systems, including UNIX, operate a policy of not actually 
removing a deleted file (and freeing up it's data blocks) until the last
 process that has the file open closes it. So, if a running process has a
 file open and you use the rm(1) command to delete the file the data 
blocks on the disk will not be freed up by the OS until the process that
 has the file open closes it.

On a UNIX host using local disk 
store this behaviour can manifest itself it some seemingly confusing 
situations. For example, you may wish to free up some space on a file 
system that's used 900 MB of it's 1GB quota. You have a large file, 
200MB say, named myjava.jar that you believe is no longer required, but 
is actually currently open in your WebLogic server. Not knowing this you
 delete myjava.jar and do an ls(1) command to see that the file is no 
longer listed. However, when you use the df(1) command it still reports 
that 900 MB of it's 1GB quota is used because your WebLogic server still
 has the file open. When you shutdown the WebLogic server or the server 
closes the file the disk space will be released and df(1) will report 
700MB of it's 1GB is used.

If the file that is removed is on NFS 
mounted store then it is possible for a file to be deleted on one client
 whilst still being open on another client. In this situation the same 
rule of not actually deleting the file until the last process with it 
open closes it still applies. However, in order for the NFS file handle 
used by the client that still has the file open not to be broken a 
filename reference must be maintained. In order to achieve this and 
remove the files name from the directory ( so it doesn't show up in an 
ls command output) the NFS file server renames the deleted file to a 
name beginning '.nfs'. These are the files you are seeing. When the last
 process with these files open dies or closes them they will be tidied 
up and removed. Trying to delete them before they are closed will only 
result in the file being renamed again. 



=== Find Class within Jar file ===

To find a class within a binary jar file

 for i in `ls *.jar`; do (jar tf $i  grep '{classname}' ) && echo $i;done   

If you want to search within all subdirectories use this

 for i in `find . -name ‘*.jar’`; do (jar tf $i  grep '{CLASSNAME}' ) && echo $i;done 

=== Find string within file of name ===


find . -name *.xml -exec grep  {} \;

example

find . -name web.xml -exec grep -i servlet {} \;



=== Grep within zip file without unzipping it ===


gzip -c -d {file}.gz | grep {string}

Example

gzip -c -d admin_access.log0001_4Dec08_0147.gz | grep -i adq | wc -l







=== Find process using the port ===

The easy way to do this is using netstat passing the port number 

netstat -a | grep 61014

If that does not help getting a pid, run this line below


for i in `ls /proc`; do pfiles $i | grep AF_INET | grep 61014 ; done


Output:

pfiles: permission denied: 12363
sockname: AF_INET 0.0.0.0 port: 61014
pfiles: permission denied: 12384

this shows the process appearing in between pids 12363 and 12384 uses that port.

ls /proc
gives the PIDs in order as .. 12363, 12369, 12384 ..


ps -ef  grep 12369
wlsuser 12369 7852 0 01:07:40 ? 4:42 /opt/bea/jdk142_05/bin/java -server -DresKBD -Xms512m -Xmx512m -XX:MaxPermSize=

gave the process details which was using the port
 

No comments:

Post a Comment