file descriptor count

I am trying to write a script which will only show me the file descriptor count for a process/pid. My script will return me the count only not the whole output. For example, I would like my script to return the output 23 this case, not the whole output.

Can anybody please help me how do I get the last count only?

Thanks in advance who replies.

 
pfiles 18877
 
 
............................................. 
............................................ 
............................................ 
20: S_IFSOCK mode:0666 dev:373,0 ino:47575 uid:0 gid:0 size:0
      O_RDWR FD_CLOEXEC
        SOCK_STREAM
        SO_SNDBUF(49152),SO_RCVBUF(49152),IP_NEXTHOP(0.0.192.0)
        sockname: AF_INET 10.66.36.73  port: 40117
        peername: AF_INET 10.66.36.73  port: 41625
23: S_IFSOCK mode:0666 dev:373,0 ino:31179 uid:0 gid:0 size:0
      O_RDWR|O_NONBLOCK
        SOCK_STREAM
          SO_KEEPALIVE,SO_SNDBUF(49152),SO_RCVBUF(49640),IP_NEXTHO     P(0.0.193.232)
        sockname: AF_INET 10.66.36.152  port: 41627
        peername: AF_INET 10.66.36.174  port: 10000

for my process_id "29141"

$ pfiles 29141 | awk '/file descriptors/ {print $3}'
256
$
 pfiles $PID | egrep  '[0-9]+: '|wc -l

or using lsof

echo $(($(lsof -p $PID |wc -l )-1))

Hi Skrynesaver,

I ran your command but it is not giving me right count... pfiles 18877 has count 23 not 22.

pfiles 18877 | egrep  '[0-9]+: '|wc -l
      22

All the command is doing is counting the number of lines which begin " nnn: " (ie start of file descriptor record). Are you sure the number of open files is a constant?

Note in the records you posted above that 21 and 22 are not active FDs for the process. Given FD 0 is the starting point, if all the other potential values are used, then you have 22 FDs for the process.

Here is the command I am trying to run, but it is not working as expected.

If I run pfiles 437 it gives me follwing output and I just want to pick 27 from there...so I ran following command but it prints me the last line instead of the number. Can you see what is wrong with my command?

 
pfiles 437 | nawk '{print $1}' | nawk '/[0-9]/ {field=$NF} END {print field}' | sed 's/://g'
 
...........................................
..........................................
...........................................
27: S_IFSOCK mode:0666 dev:373,0 ino:52020 uid:0 gid:0 size:0
      O_RDWR FD_CLOEXEC
        SOCK_STREAM
        SO_SNDBUF(49152),SO_RCVBUF(49152),IP_NEXTHOP(0.0.192.0)
        sockname: AF_INET 0.0.0.0  port: 30901
 
 
pfiles 437 | nawk '{print $1}' | nawk '/[0-9]/ {field=$NF} END {print field}' | sed 's/://g'
SO_SNDBUF(49152),SO_RCVBUF(49152),IP_NEXTHOP(0.0.192.0)

Try this...

pfiles 437 | awk -F":" '/^[0-9]*:/{val=$1} END{print val}'

--ahamed

---------- Post updated at 10:54 AM ---------- Previous update was at 10:54 AM ----------

If solaris, please use nawk!

--ahamed

Hi Ahamed,

thanks for your reply. I ran the command but it bounced back with the pid

 
 pfiles 437 | awk -F":" '/^[0-9]*:/{val=$1} END{print val}'
437

Can you paste the output of pfiles 437

--ahamed

Even with the nwak (solraris 10) it returned the pid ....

 
pfiles 437 | nawk -F":" '/^[0-9]*:/{val=$1} END{print val}'
437

---------- Post updated at 02:03 PM ---------- Previous update was at 01:58 PM ----------

Hi Ahamed,

Here it is...

 
 pfiles 437
 
437:    /xxx/xxx/xxx/xxx -STITUS.HTTP 30900 N xxx.ini 
  Current rlimit: 65536 file descriptors
   0: S_IFCHR mode:0666 dev:366,0 ino:6815752 uid:0 gid:3 rdev:13,2
      O_RDWR|O_LARGEFILE
      /devices/pseudo/mm@0:null
   1: S_IFCHR mode:0666 dev:366,0 ino:6815752 uid:0 gid:3 rdev:13,2
      O_RDWR|O_LARGEFILE
      /devices/pseudo/mm@0:null
   2: S_IFCHR mode:0666 dev:366,0 ino:12582924 uid:0 gid:0 rdev:24,4
      O_RDWR|O_NOCTTY|O_LARGEFILE
      /devices/pseudo/pts@0:4
   3: S_IFREG mode:0664 dev:256,65546 ino:39261 uid:69533 gid:7490 size:65150
      O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE
      /xxx/xxx/xx/trc/tls2.trc
   4: S_IFDOOR mode:0444 dev:375,0 ino:56 uid:0 gid:0 size:0
      O_RDONLY|O_LARGEFILE FD_CLOEXEC  door to nscd[170]
      /var/run/name_service_door
   5: S_IFCHR mode:0000 dev:366,0 ino:51047 uid:0 gid:0 rdev:42,545
      O_RDWR|O_LARGEFILE
      /devices/pseudo/tcp@0:tcp
   6: S_IFREG mode:0664 dev:256,65546 ino:175027 uid:69533 gid:7490 size:431787
      O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE
      /xxx/xxx/xx/trc/TitusAmtHeld.437.trc
   7: S_IFSOCK mode:0666 dev:373,0 ino:33789 uid:0 gid:0 size:0
      O_RDWR FD_CLOEXEC
        SOCK_STREAM
        SO_SNDBUF(49152),SO_RCVBUF(49152),IP_NEXTHOP(0.0.192.0)
        sockname: AF_INET 127.0.0.1  port: 54394
        peername: AF_INET 127.0.0.1  port: 30101
   8: S_IFSOCK mode:0666 dev:373,0 ino:52021 uid:0 gid:0 size:0
      O_RDWR FD_CLOEXEC
        SOCK_STREAM
        SO_SNDBUF(49152),SO_RCVBUF(49152),IP_NEXTHOP(0.0.192.0)
        sockname: AF_INET 0.0.0.0  port: 40105
   9: S_IFSOCK mode:0666 dev:373,0 ino:43123 uid:0 gid:0 size:0
      O_RDWR FD_CLOEXEC
        SOCK_STREAM
        SO_SNDBUF(49152),SO_RCVBUF(49152),IP_NEXTHOP(0.0.192.0)
        sockname: AF_INET 127.0.0.1  port: 40105
        peername: AF_INET 127.0.0.1  port: 54395
  10: S_IFIFO mode:0000 dev:364,0 ino:163363578 uid:69533 gid:7490 size:0
      O_RDWR
  11: S_IFIFO mode:0000 dev:364,0 ino:163363578 uid:69533 gid:7490 size:0
      O_RDWR
  12: S_IFIFO mode:0000 dev:364,0 ino:163363579 uid:69533 gid:7490 size:0
      O_RDWR
  13: S_IFIFO mode:0000 dev:364,0 ino:163363579 uid:69533 gid:7490 size:0
      O_RDWR
  14: S_IFSOCK mode:0666 dev:373,0 ino:5360 uid:0 gid:0 size:0
      O_RDWR|O_NONBLOCK
        SOCK_STREAM
        SO_KEEPALIVE,SO_SNDBUF(49152),SO_RCVBUF(49640),IP_NEXTHOP(0.0.193.232)
        sockname: AF_INET xx.xx.xx.xxx  port: 54501
        peername: AF_INET xx.xx.xx.xxx  port: 10000
  15: S_IFSOCK mode:0666 dev:373,0 ino:8737 uid:0 gid:0 size:0
      O_RDWR|O_NONBLOCK
        SOCK_STREAM
        SO_KEEPALIVE,SO_SNDBUF(49152),SO_RCVBUF(49640),IP_NEXTHOP(0.0.193.232)
        sockname: AF_INET xx.xx.xx.xxx  port: 54503
        peername: AF_INET xx.xx.xx.xxx  port: 10000
  16: S_IFSOCK mode:0666 dev:373,0 ino:11137 uid:0 gid:0 size:0
      O_RDWR|O_NONBLOCK
        SOCK_STREAM
        SO_KEEPALIVE,SO_SNDBUF(49152),SO_RCVBUF(49640),IP_NEXTHOP(0.0.193.232)
        sockname: AF_INET xx.xx.xx.xxx  port: 54505
        peername: AF_INET xx.xx.xx.xxx  port: 10000
  17: S_IFSOCK mode:0666 dev:373,0 ino:11720 uid:0 gid:0 size:0
      O_RDWR|O_NONBLOCK
        SOCK_STREAM
        SO_KEEPALIVE,SO_SNDBUF(49152),SO_RCVBUF(49640),IP_NEXTHOP(0.0.193.232)
        sockname: AF_INET xx.xx.xx.xxx  port: 54506
        peername: AF_INET xx.xx.xx.xxx  port: 10000
  18: S_IFSOCK mode:0666 dev:373,0 ino:15509 uid:0 gid:0 size:0
      O_RDWR|O_NONBLOCK
        SOCK_STREAM
        SO_KEEPALIVE,SO_SNDBUF(49152),SO_RCVBUF(49640),IP_NEXTHOP(0.0.193.232)
        sockname: AF_INET xx.xx.xx.xxx  port: 54508
        peername: AF_INET xx.xx.xx.xxx  port: 10000
  19: S_IFSOCK mode:0666 dev:373,0 ino:53475 uid:0 gid:0 size:0
      O_RDWR|O_NONBLOCK
        SOCK_STREAM
        SO_KEEPALIVE,SO_SNDBUF(49152),SO_RCVBUF(49640),IP_NEXTHOP(0.0.193.232)
        sockname: AF_INET xx.xx.xx.xxx  port: 54510
        peername: AF_INET xx.xx.xx.xxx  port: 10000
  20: S_IFSOCK mode:0666 dev:373,0 ino:53854 uid:0 gid:0 size:0
      O_RDWR|O_NONBLOCK
        SOCK_STREAM
        SO_KEEPALIVE,SO_SNDBUF(49152),SO_RCVBUF(49640),IP_NEXTHOP(0.0.193.232)
        sockname: AF_INET xx.xx.xx.xxx  port: 54512
        peername: AF_INET xx.xx.xx.xxx  port: 10000
  21: S_IFSOCK mode:0666 dev:373,0 ino:20535 uid:0 gid:0 size:0
      O_RDWR|O_NONBLOCK
        SOCK_STREAM
        SO_KEEPALIVE,SO_SNDBUF(49152),SO_RCVBUF(49640),IP_NEXTHOP(0.0.193.232)
        sockname: AF_INET xx.xx.xx.xxx  port: 54514
        peername: AF_INET xx.xx.xx.xxx  port: 10000
  22: S_IFSOCK mode:0666 dev:373,0 ino:33008 uid:0 gid:0 size:0
      O_RDWR|O_NONBLOCK
        SOCK_STREAM
        SO_KEEPALIVE,SO_SNDBUF(49152),SO_RCVBUF(49640),IP_NEXTHOP(0.0.193.232)
        sockname: AF_INET xx.xx.xx.xxx  port: 54516
        peername: AF_INET xx.xx.xx.xxx  port: 10000
  23: S_IFSOCK mode:0666 dev:373,0 ino:33433 uid:0 gid:0 size:0
      O_RDWR|O_NONBLOCK
        SOCK_STREAM
        SO_KEEPALIVE,SO_SNDBUF(49152),SO_RCVBUF(49640),IP_NEXTHOP(0.0.193.232)
        sockname: AF_INET xx.xx.xx.xxx  port: 54518
        peername: AF_INET xx.xx.xx.xxx  port: 10000
  24: S_IFIFO mode:0000 dev:364,0 ino:163363580 uid:69533 gid:7490 size:0
      O_RDWR
  25: S_IFIFO mode:0000 dev:364,0 ino:163363580 uid:69533 gid:7490 size:0
      O_RDWR
  26: S_IFSOCK mode:0666 dev:373,0 ino:24528 uid:0 gid:0 size:0
      O_RDWR FD_CLOEXEC
        SOCK_STREAM
        SO_REUSEADDR,SO_SNDBUF(49152),SO_RCVBUF(49152),IP_NEXTHOP(0.0.192.0)
        sockname: AF_INET 0.0.0.0  port: 30900
  27: S_IFSOCK mode:0666 dev:373,0 ino:52020 uid:0 gid:0 size:0
      O_RDWR FD_CLOEXEC
        SOCK_STREAM
        SO_SNDBUF(49152),SO_RCVBUF(49152),IP_NEXTHOP(0.0.192.0)
        sockname: AF_INET 0.0.0.0  port: 30901

Try this...

pfiles 437 | awk -F":" '/^ *[0-9]*:/{val=$1} END{print val}'

Note there is a slight change made in the code!

--ahamed

Perfect it works fine...
Thank you for your help Ahamed.