Lsof linux commands

From Knowledge Center
Jump to: navigation, search

lsof linux commands

Get The Pid of the Process:

A couple of example commands we can use to determine the pid of a process:

$ ps -fC mysqld
UID        PID  PPID  C STIME TTY          TIME CMD
mysql    12734 12529  0 Jan31 ?        10:02:00 /usr/libexec/mysql55/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --user=mysql --log-error=/var/log/mysqld

or:

$ lsof $(which mysqld) | grep -v "COMMAND" | awk '{print $2}'
12734

or:

$ lsof -i :3306
COMMAND   PID  USER   FD   TYPE    DEVICE SIZE/OFF NODE NAME
mysqld  12734 mysql   10u  IPv4 252168144      0t0  TCP localhost:mysql (LISTEN)

or:

$ netstat -tulpn | grep -E '(mysql|Active|Proto)'
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address               Foreign Address             State       PID/Program name
tcp        0      0 127.0.0.1:3306              0.0.0.0:*                   LISTEN      12734/mysqld

See what files are being in use for the process:

Example to see what files are being used for a specific process

$ lsof -np 12734
COMMAND   PID  USER   FD   TYPE             DEVICE  SIZE/OFF      NODE NAME
mysqld  12734 mysql  cwd    DIR              202,1      4096    264308 /var/lib/mysql
mysqld  12734 mysql  rtd    DIR              202,1      4096         2 /
mysqld  12734 mysql  txt    REG              202,1   9956560    548221 /usr/libexec/mysql55/mysqld
mysqld  12734 mysql  mem    REG              202,1     27512    396133 /lib64/libnss_dns-2.17.so
mysqld  12734 mysql  DEL    REG               0,10           252168093 /[aio]
mysqld  12734 mysql    0r   CHR                1,3       0t0      5422 /dev/null
mysqld  12734 mysql    1w   REG              202,1     76696    264310 /var/log/mysqld.log
mysqld  12734 mysql    2w   REG              202,1     76696    264310 /var/log/mysqld.log
mysqld  12734 mysql    3uW  REG              202,1 149946368    264473 /var/lib/mysql/ibdata1
mysqld  12734 mysql    4u   REG              202,1         0        13 /tmp/ibVi9YRU (deleted)
mysqld  12734 mysql    5u   REG              202,1         0       116 /tmp/ibWZBhJz (deleted)
mysqld  12734 mysql    6u   REG              202,1         0       120 /tmp/ibnLdAAe (deleted)
mysqld  12734 mysql    7u   REG              202,1         0      1010 /tmp/ibMznTky (deleted)
mysqld  12734 mysql    8uW  REG              202,1   5242880    264474 /var/lib/mysql/ib_logfile0
mysqld  12734 mysql    9uW  REG              202,1   5242880    264475 /var/lib/mysql/ib_logfile1
mysqld  12734 mysql   10u  IPv4          252168144       0t0       TCP 127.0.0.1:mysql (LISTEN)
mysqld  12734 mysql   11u   REG              202,1         0      1022 /tmp/iblte8gd (deleted)
mysqld  12734 mysql   12u  unix 0xffff880004051880       0t0 252168145 /var/lib/mysql/mysql.sock
mysqld  12734 mysql   13u   REG              202,1      2048    264386 /var/lib/mysql/mysql/host.MYI
mysqld  12734 mysql   14u   REG              202,1         0    264387 /var/lib/mysql/mysql/host.MYD
mysqld  12734 mysql   15u   REG              202,1      2048    264389 /var/lib/mysql/mysql/user.MYI
mysqld  12734 mysql   16u   REG              202,1      1400    264390 /var/lib/mysql/mysql/user.MYD
mysqld  12734 mysql   85u   REG              202,1      4096    264434 /var/lib/mysql/mysql/proc.MYI
mysqld  12734 mysql   86u   REG              202,1      4312    264435 /var/lib/mysql/mysql/proc.MYD

How many file descriptors is opened for a process:

How many file descriptors is opened for our process

$ ls -l /proc/12734/fd | wc -l
87

Some other LSOF Commands Usage:

$ lsof -i :portNumber
$ lsof -i tcp:portNumber
$ lsof -i udp:portNumber
$ lsof -i :80 | grep LISTEN