Linux: get info regarding a process ID

With the following, you can get some information regarding a given process ID. It displays the following:

  • working directory
  • environment variables
  • cpu usage
  • memory usage
  • TCP connections
  • UDP connections
  • List of open file handles
Here an example of the ouput for the rpcbin process:

# processinfo.sh 3822
Information about process 3822

—– Working Directory —–

/

—– Command line —–

/sbin/rpcbind CONSOLE=/dev/console SELINUX_INIT=YES ROOTFS_FSTYPE=ext3 SHELL=/bin/sh TERM=linux ROOTFS_FSCK=0 crashkernel=128M-:64M LC_ALL=POSIX instmode=cd INIT_VERSION=sysvinit-2.86 REDIRECT=/dev/tty1 COLU

—– Environment —–

CONSOLE=/dev/console
SELINUX_INIT=YES
ROOTFS_FSTYPE=ext3
SHELL=/bin/sh
TERM=linux
ROOTFS_FSCK=0
crashkernel=128M-:64M
LC_ALL=POSIX
instmode=cd
INIT_VERSION=sysvinit-2.86
REDIRECT=/dev/tty1
COLUMNS=96
PATH=/bin:/sbin:/usr/bin:/usr/sbin
vga=0x314
DO_CONFIRM=
RUNLEVEL=3
PWD=/
SPLASHCFG=/etc/bootsplash/themes/SLES/config/bootsplash-800×600.cfg
PREVLEVEL=N
LINES=33
HOME=/
SHLVL=2
splash=silent
SPLASH=yes
ROOTFS_BLKDEV=/dev/sda2
_=/sbin/startproc
DAEMON=/sbin/rpcbind

—– Resource usage —–

CPU : 0.0%
MEMORY: 0.0%

—– TCP connections —–

Status: LISTEN local: 0.0.0.0:111 remote: 0.0.0.0:*
Status: LISTEN local: :::111 remote: :::*

—– UDP connections —–

Local: 0.0.0.0:605 remote: 0.0.0.0:*
Local: 0.0.0.0:111 remote: 0.0.0.0:*
Local: :::605 remote: :::*
Local: :::111 remote: :::*

—– Open files —–

COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
rpcbind 3822 root cwd DIR 8,2 4096 2 /
rpcbind 3822 root rtd DIR 8,2 4096 2 /
rpcbind 3822 root txt REG 8,2 56536 123002 /sbin/rpcbind
rpcbind 3822 root mem REG 8,2 61467 466970 /lib64/libnss_files-2.11.1.so
rpcbind 3822 root mem REG 8,2 19114 466959 /lib64/libdl-2.11.1.so
rpcbind 3822 root mem REG 8,2 39712 466992 /lib64/libgssglue.so.1.0.0
rpcbind 3822 root mem REG 8,2 108213 466964 /lib64/libnsl-2.11.1.so
rpcbind 3822 root mem REG 8,2 1661454 466953 /lib64/libc-2.11.1.so
rpcbind 3822 root mem REG 8,2 135646 466979 /lib64/libpthread-2.11.1.so
rpcbind 3822 root mem REG 8,2 160248 467049 /lib64/libtirpc.so.1.0.10
rpcbind 3822 root mem REG 8,2 42016 467021 /lib64/libwrap.so.0.7.6
rpcbind 3822 root mem REG 8,2 149797 466946 /lib64/ld-2.11.1.so
rpcbind 3822 root 0u CHR 1,3 0t0 2372 /dev/null
rpcbind 3822 root 1u CHR 1,3 0t0 2372 /dev/null
rpcbind 3822 root 2u CHR 1,3 0t0 2372 /dev/null
rpcbind 3822 root 3r REG 8,2 0 361286 /var/run/rpcbind.lock
rpcbind 3822 root 4u sock 0,6 0t0 8357 can’t identify protocol
rpcbind 3822 root 5u unix 0xffff880233c4a3c0 0t0 8319 /var/run/rpcbind.sock
rpcbind 3822 root 6u IPv4 8321 0t0 UDP *:sunrpc
rpcbind 3822 root 7u IPv4 8325 0t0 UDP *:soap-beep
rpcbind 3822 root 8u IPv4 8326 0t0 TCP *:sunrpc (LISTEN)
rpcbind 3822 root 9u IPv6 8328 0t0 UDP *:sunrpc
rpcbind 3822 root 10u IPv6 8330 0t0 UDP *:soap-beep
rpcbind 3822 root 11u IPv6 8331 0t0 TCP *:sunrpc (LISTEN)

And here’s the script. It’s not too spectacular, just gathering data from /proc/xxx, ps, netstat and lsof:

#!/bin/sh

if [ $# -ne 1 ]; then
echo Usage: $0 PID
exit 1
fi

PID=$1

if kill -0 $PID; then
echo Information about process $PID
echo
echo —– Working Directory —–
echo
readlink /proc/$PID/cwd
echo
echo —– Command line —–
echo
ps eho command -p $PID
echo
echo —– Environment —–
echo
strings -f /proc/$PID/environ | cut -f2 -d ‘ ‘
echo
echo —– Resource usage —–
echo
echo CPU : `ps eho %cpu -p $PID`%
echo MEMORY: `ps eho %mem -p $PID`%
echo
echo —– TCP connections —–
echo
netstat -pan | grep ” $PID/” | grep tcp | awk ‘ { print “Status: “$6” local: “$4” remote: “$5; }’
echo
echo —– UDP connections —–
echo
netstat -pan | grep ” $PID/” | grep udp | awk ‘ { print “Local: “$4” remote: “$5; }’
echo
echo —– Open files —–
echo
lsof -p $PID
else
echo Process $PID does not exist
exit 2
fi

4 thoughts on “Linux: get info regarding a process ID

    1. Now the RSS feed icon is always in the upper right corner instead of being hidden in the footer. This can be used to get my new posts automatically.

Leave a Reply

Your email address will not be published.