Linux ps command tutorial

What is ps command

ps(process) command in linux is used to report the active process status.

Syntax of ps command

ps [a][-a] [-A] [-d] [-e]..<options>

Example of ps Command

ps

Output would be

gaurav@ubuntu:~$ ps 
 PID TTY TIME CMD
 1916 pts/1 00:00:00 bash
 2764 pts/1 00:00:00 ps
gaurav@ubuntu:~$

Some Other Common Example

How to find whether java application is listening on IPv4 or IPv6

Step 1 : # First find pid of process

#ps -ef | grep <java_application_name>

or

#pidof java

Then execute lsof -i command

#lsof -i | grep <pid>

Fifth column in the output will show whether its listening on ipv4 or ipv6.

 

Find files used/opened by a particular process

Step 1 : Find process id

#ps -ef | grep <process_name>

or

#ps -ef | grep osi | grep -v grep | awk '{print $2}' | head -1

Step 2 : Now execute below command

#ls -ltr /proc/<pid>/fd/*

You will be able to see all files opened by the process.

Know which process is using a particular file.

fuser filename

e.g

#fuser onsboot.log
onsboot.log:          4551
#ps -ef | grep 4551

Output will be process name and process Id. Here 4551 is pid of process.s

2) Know the start time of process

ps -eo pid,cmd,etime | grep processname | grep -v grep

e.g

ps -eo pid,cmd,etime | grep java | grep -v grep
15612 java -cp /opt/oss/NPS-mf_sw  5-20:54:15

24540 java -Djava.endorsed.dirs=/  3-01:38:51

24977 /usr/lib/jvm/java-1.7.0-ope  3-01:38:39

25920 /usr/lib/jvm/java-1.7.0-ope  3-01:38:31

30862 java -classpath .:.:/opt/cp  5-13:20:57

32189 java -cp /opt/oss/NPS-mf_sw  5-18:11:36

where 3rd column indicates start time sonce.

first process  15612 is running since 5 days 20 hrs 54 mins and 15 secs

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.