2011-04-08

bash - working with ps

Platform :
FreeBSD

Objective :
To grab the process details of a pid, without resorting to parse the output of process status command , "ps" :
E.g.
ps aux | grep -i pid

Solution :
The command "ps" is capable of listing the detail of a process, given the pid. The output can be customized to different details.
Example :
  • Typically,
    $ ps -p 575
      PID  TT  STAT      TIME COMMAND
      575  ??  Ss     0:00.15 /usr/sbin/syslogd -s
  • To include more details :
    $ ps aux -p 575
    USER   PID %CPU %MEM   VSZ   RSS  TT  STAT STARTED      TIME COMMAND
    root   575  0.0  0.5  3344  1328  ??  Ss    2:36PM   0:00.16 /usr/sbin/syslogd -s
  • A more selective output :
    $ ps -p 575 -o pid,start,command
      PID STARTED COMMAND
      575  2:36PM /usr/sbin/syslogd -s
    The column information can be obtain from :
    ps -L
    %cpu %mem acflag acflg args blocked caught comm command cpu cputime emul etime f flags ignored inblk
    inblock jid jobc ktrace label lim lockname login logname lstart lwp majflt minflt msgrcv msgsnd
    mwchan ni nice nivcsw nlwp nsignals nsigs nswap nvcsw nwchan oublk oublock paddr pagein pcpu pending
    pgid pid pmem ppid pri re rgid rgroup rss rtprio ruid ruser sid sig sigcatch sigignore sigmask sl
    start stat state svgid svuid tdev tdnam time tpgid tsid tsiz tt tty ucomm uid upr uprocp user usrpri
    vsize vsz wchan xstat
  • To skip printing of header :
    $ ps -p 575 -o pid= -o start= -o command=
      575  2:36PM /usr/sbin/syslogd -s

Adieu !!!

No comments: