{"id":7216,"date":"2018-02-12T07:51:12","date_gmt":"2018-02-11T23:51:12","guid":{"rendered":"http:\/\/rmohan.com\/?p=7216"},"modified":"2018-02-12T07:58:41","modified_gmt":"2018-02-11T23:58:41","slug":"ps-command-1-basic","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7216","title":{"rendered":"ps command #1 &#8211; Basic"},"content":{"rendered":"<p>A linux command to monitor the system process consuming resources on the server. If you are working on a linux system it is good to have at least basic understanding of this command.<\/p>\n<p>ps program or command when run take a snapshot of running processes at that time and display on the terminal which can be used to analyse the system performance or identify any problematic process which can be a risk for system.<\/p>\n<p><b>ps Command:<\/b><br \/>\n<b><br \/>\n<\/b><br \/>\nWhen we run simple ps command, it will display very basic information &#8211;<\/p>\n<pre class=\"pre codeblock\">$ <b>ps<\/b>\r\n  PID TTY          TIME CMD\r\n22396 pts\/0    00:00:00 su\r\n22402 pts\/0    00:00:00 bash\r\n22417 pts\/0    00:00:00 su\r\n22420 pts\/0    00:00:00 bash\r\n23332 pts\/0    00:00:00 ps\r\n<\/pre>\n<div>PID &#8211; process id<br \/>\nTTY &#8211; terminal in which process is running<br \/>\nTIME &#8211; total cpu time taken till now<br \/>\nCMD &#8211; command<\/div>\n<div><\/div>\n<div>\n<p>Let&#8217;s try with one argument -f (full)<\/p>\n<p>$ ps -f<br \/>\nUID PID PPID C STIME TTY TIME CMD<br \/>\nroot 22396 22377 0 09:49 pts\/0 00:00:00 su<br \/>\nroot 22402 22396 0 09:49 pts\/0 00:00:00 bash<br \/>\nroot 22417 22402 0 09:50 pts\/0 00:00:00 su<br \/>\nroot 22420 22417 0 09:50 pts\/0 00:00:00 bash<br \/>\nroot 23337 22420 0 11:02 pts\/0 00:00:00 ps -f<\/p>\n<p>this output is display with some more information &#8211;<br \/>\nUID &#8211; process owner user id<br \/>\nPPID &#8211; parent process id<br \/>\nSTIME &#8211; process start time<\/p>\n<p>Let&#8217;s play with some argument and see what will be the output look like &#8211;<\/p>\n<p>$ ps -ef<\/p>\n<p>mohan 7585 1 0 18:29 ? 00:00:00 \/usr\/libexec\/gvfsd-http &#8211;spawner :1.7 \/org\/gtk\/gvfs\/exec_spaw\/2<br \/>\nroot 16991 1 0 Dec04 ? 00:00:00 \/usr\/sbin\/bluetoothd &#8211;udev<br \/>\nmohan 17099 1 0 Dec04 ? 00:09:26 \/usr\/lib64\/firefox\/firefox<br \/>\nmohan 22246 1 0 19:28 ? 00:00:05 gnome-terminal<br \/>\nmohan 22248 22246 0 19:28 ? 00:00:00 gnome-pty-helper<br \/>\nmohan 22377 22246 0 19:35 pts\/0 00:00:00 bash<br \/>\nroot 22396 22377 0 19:35 pts\/0 00:00:00 su<br \/>\nroot 22402 22396 0 19:35 pts\/0 00:00:00 bash<br \/>\nroot 22417 22402 0 19:35 pts\/0 00:00:00 su<br \/>\nroot 22420 22417 0 19:35 pts\/0 00:00:00 bash<br \/>\nmohan 22937 1 0 20:06 ? 00:00:00 gedit<br \/>\nroot 23282 1899 0 20:47 ? 00:00:00 \/usr\/libexec\/hald-addon-rfkill-killswitch<br \/>\nroot 24348 1810 0 22:07 ? 00:00:00 \/sbin\/dhclient -d -4 -sf \/usr\/libexec\/nm-dhcp-client.action -pf \/var\/run\/dhclient-eth1.pid -lf \/var\/lib\/dhclient\/dhclient-e738be73-e337-4f64-865e-aa936ac77c14-eth1.lease -cf \/var\/run\/nm-dhclient-eth1.conf eth1<br \/>\nmohan 27098 1236 2 22:23 ? 00:00:00 \/usr\/lib\/rstudio-server\/bin\/rsession -u mohan<br \/>\nroot 27112 1 0 22:23 ? 00:00:00 \/usr\/libexec\/fprintd<\/p>\n<p>All process<br \/>\nTo see all processes on the system (along with the command line arguments used to start each process) you could use:<\/p>\n<p>$ ps aux<\/p>\n<p>Processes for User<br \/>\nTo see all processes for a particular user (along with the command line arguments for each process) you could use:<\/p>\n<p>$ ps U &lt;username&gt; u<\/p>\n<p>$ ps U mohan u<\/p>\n<p>USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND<br \/>\nmohan 2695 0.0 0.0 229128 672 ? Sl Dec03 0:00 \/usr\/bin\/gnome-keyring-daemon &#8211;daemonize &#8211;login<br \/>\nmohan 2705 0.0 0.1 253264 1888 ? Ssl Dec03 0:01 gnome-session<br \/>\nmohan 2713 0.0 0.0 20040 128 ? S Dec03 0:00 dbus-launch &#8211;sh-syntax &#8211;exit-with-session<br \/>\nmohan 2714 0.0 0.1 32476 1356 ? Ssl Dec03 0:01 \/bin\/dbus-daemon &#8211;fork &#8211;print-pid 5 &#8211;print-address 7 &#8211;session<br \/>\nmohan 2732 0.0 0.3 133360 3636 ? S Dec03 0:06 \/usr\/libexec\/gconfd-2<br \/>\nmohan 2740 0.0 0.3 507280 3408 ? Ssl Dec03 0:26 \/usr\/libexec\/gnome-settings-daemon<br \/>\nmohan 2741 0.0 0.1 286220 1624 ? Ss Dec03 0:00 seahorse-daemon<br \/>\nmohan 2746 0.0 0.0 137388 844 ? S Dec03 0:00 \/usr\/libexec\/gvfsd<br \/>\nmohan 2760 0.0 0.5 447048 5116 ? Sl Dec03 0:25 metacity<br \/>\nmohan 2767 0.0 0.7 502416 7600 ? Sl Dec03 0:34 gnome-panel<br \/>\nmohan 2769 0.0 0.3 450232 3156 ? S&lt;sl Dec03 0:35 \/usr\/bin\/pulseaudio &#8211;start &#8211;log-target=syslog<br \/>\nmohan 2772 0.0 0.0 94828 252 ? S Dec03 0:00 \/usr\/libexec\/pulse\/gconf-helper<br \/>\nmohan 2773 0.0 5.6 1199004 57544 ? Sl Dec03 1:18 nautilus<br \/>\nmohan 2775 0.0 0.0 696412 256 ? Ssl Dec03 0:00 \/usr\/libexec\/bonobo-activation-server &#8211;ac-activate &#8211;ior-output-fd=18<br \/>\nmohan 2778 0.0 0.2 30400 2212 ? S Dec03 0:00 \/usr\/sbin\/restorecond -u<br \/>\nmohan 2783 0.0 0.4 469076 4244 ? Sl Dec03 0:02 gpk-update-icon<br \/>\nmohan 2786 0.0 0.0 146404 900 ? S Dec03 0:00 \/usr\/libexec\/gvfs-gdu-volume-monitor<br \/>\nmohan 2787 0.0 0.2 375072 2924 ? S Dec03 0:00 gnome-volume-control-applet<br \/>\nmohan 2788 0.0 0.5 331480 5988 ? S Dec03 0:48 \/usr\/libexec\/wnck-applet &#8211;oaf-activate-iid=OAFIID:GNOME_Wncklet_Factory &#8211;oaf-ior-fd=18<br \/>\nmohan 2789 0.0 0.2 476996 2900 ? Sl Dec03 0:00 \/usr\/libexec\/trashapplet &#8211;oaf-activate-iid=OAFIID:GNOME_Panel_TrashApplet_Factory &#8211;oaf-ior-fd=24<\/p>\n<p>Process tree<br \/>\nA process tree shows the child\/parent relationships between processes. (When a process spawns another process, the spawned is called a child process while the other is the parent)<\/p>\n<p>$ ps afjx<\/p>\n<\/div>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>Usually, when we are monitoring process, we are targeting something which can impact our server performance or some specific process. For doing so we grep the ps output &#8211;<\/p>\n<p>This is how we call list all <b>http<\/b> processes &#8211;<\/p>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps aux | grep http<\/b><\/pre>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\">atul      7585  0.0  0.0 177676   592 ?        S    Dec06   0:00 \/usr\/libexec\/gvfsd-http --spawner :1.7 \/org\/gtk\/gvfs\/exec_spaw\/2\r\nroot     28848  0.0  0.0   2700   168 pts\/0    D+   02:49   0:00 grep http<\/pre>\n<p>you can filter ps command output by any keyword as above.<\/p>\n<p>There are some ps options which can give you a customized output &#8211;<\/p>\n<p><b>To see every process on the system using standard syntax:<\/b><\/p>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps -e<\/b><\/pre>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps -ef<\/b><\/pre>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps -eF<\/b><\/pre>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps -ely<\/b><\/pre>\n<div>\u00a0<b>To see every process on the system using BSD syntax:<\/b><\/p>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps ax<\/b><\/pre>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps axu<\/b><\/pre>\n<p><b>To print a process tree:<\/b><\/p>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps -ejH<\/b><\/pre>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps axjf<\/b><\/pre>\n<p><b>To get info about threads:<\/b><\/p>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps -eLf<\/b><\/pre>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps axms<\/b><\/pre>\n<p><b>To get security\u00a0info:<\/b><\/p>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps -eo euser,ruser,suser,fuser,f,comm,lable<\/b><\/pre>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps axZ<\/b><\/pre>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps -eM<\/b><\/pre>\n<p><b>To see every process running as\u00a0root (real &amp; effective ID) in user format:<\/b><\/p>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps -U root -u root u<\/b><\/pre>\n<p><b>To see every process with a user-defined format:<\/b><\/p>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps -eo pid,tid,class,rtprio,ni,pri,psr,pcpu,stat,wchan:14,comm\r\n$ ps axo stat,euid,ruid,tty,tpgid,sess,pgrp,ppid,pid,pcpu,comm\r\n$ ps -eopid,tt,user,fname,tmout,f,wchan<\/b><\/pre>\n<p><b>Print only the process IDs of process syslogd:<\/b><\/p>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps -C syslogd -o pid=\r\n #ps -C &lt;process_name&gt; -o pid=<\/b><\/pre>\n<p><b>Print only the name of PID 42:<\/b><\/p>\n<pre class=\"pre codeblock\" data-gclp-id=\"8\" data-initialized=\"true\"><b>$ ps -p 42 -o comm=\r\n  #ps -p &lt;process_id&gt; -o comm=<\/b><\/pre>\n<\/div>\n<div><\/div>\n<div>\n<p>We can sort the ps command output by unix sort also which is easy to use. Need to pass ps command output to sort command with proper argument and Volla !! You will get the output as you want.<\/p>\n<p>Let&#8217;s see how this is work [ sort command arguement can differ per your linux flavour and version ]<br \/>\nI am using &#8211; CentOS 6.3<\/p>\n<p>1. Display the top CPU consuming process (Column 3 &#8211; %CPU)<\/p>\n<p>$ ps aux | head -1; ps aux | sort -k3 -nr |grep -v &#8216;USER&#8217;| head<\/p>\n<p>USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND<\/p>\n<p>mohan 21210 2.0 0.1 110232 1140 pts\/3 R+ 00:13 0:00 ps aux<br \/>\nhduser 2671 0.8 4.1 960428 42436 pts\/1 Sl+ Aug22 5:29 mongod<br \/>\nroot 1447 0.2 0.3 185112 3384 ? Sl Aug22 1:36 \/usr\/sbin\/vmtoolsd<br \/>\nmohan 2478 0.2 2.1 448120 21876 ? Sl Aug22 1:51 \/usr\/lib\/vmware-tools\/sbin64\/vmtoolsd -n vmusr &#8211;blockFd 3<br \/>\nrtkit 2359 0.1 0.1 168448 1204 ? SNl Aug22 0:44 \/usr\/libexec\/rtkit-daemon<br \/>\nroot 7 0.1 0.0 0 0 ? S Aug22 0:53 [events\/0]<br \/>\nroot 2204 0.1 4.3 147500 43872 tty1 Ss+ Aug22 0:45 \/usr\/bin\/Xorg :0 -nr -verbose -audit 4 -auth \/var\/run\/gdm\/auth-for-gdm-wEmBs1\/database -nolisten tcp vt1<br \/>\nroot 920 0.0 0.0 0 0 ? S Aug22 0:00 [bluetooth]<br \/>\nroot 9 0.0 0.0 0 0 ? S Aug22 0:00 [khelper]<br \/>\nroot 8 0.0 0.0 0 0 ? S Aug22 0:00 [cgroup]<\/p>\n<p>For my linux sort command arguements are &#8212;<br \/>\n-kn ==&gt; This use to select the column n, such as for column 4, -k4<br \/>\n-n ==&gt; column is numeric<br \/>\n-r ==&gt; reverse order<\/p>\n<p>sort -k3 -nr ==&gt; sort the third column of output in numeric reverse sort (largest to smallest)<\/p>\n<p>2. Display the top 10 memory consuming process (Column 4 &#8211; %MEM)<\/p>\n<p>$ ps aux | head -1; ps aux | sort -k4 -nr |grep -v &#8216;USER&#8217;| head<\/p>\n<p>USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND<\/p>\n<p>root 2204 0.1 4.3 147500 43872 tty1 Ss+ Aug22 0:46 \/usr\/bin\/Xorg :0 -nr -verbose -audit 4 -auth \/var\/run\/gdm\/auth-for-gdm-wEmBs1\/database -nolisten tcp vt1<br \/>\nhduser 2671 0.8 4.1 960428 42436 pts\/1 Sl+ Aug22 5:32 mongod<br \/>\nmohan 2458 0.0 2.3 943204 23624 ? S Aug22 0:16 nautilus<br \/>\nmohan 2516 0.0 2.2 275280 22316 ? Ss Aug22 0:06 gnome-screensaver<br \/>\nmohan 2478 0.2 2.1 448120 21876 ? Sl Aug22 1:52 \/usr\/lib\/vmware-tools\/sbin64\/vmtoolsd -n vmusr &#8211;blockFd 3<br \/>\nmohan 2507 0.0 1.6 321388 16680 ? S Aug22 0:01 python \/usr\/share\/system-config-printer\/applet.py<br \/>\nmohan 2589 0.0 1.4 292556 14600 ? Sl Aug22 0:14 gnome-terminal<br \/>\nmohan 2536 0.0 1.3 395832 13372 ? S Aug22 0:00 \/usr\/bin\/gnote &#8211;panel-applet &#8211;oaf-activate-iid=OAFIID:GnoteApplet_Factory &#8211;oaf-ior-fd=22<br \/>\nmohan 2502 0.0 1.3 474620 13952 ? Sl Aug22 0:01 gpk-update-icon<br \/>\nmohan 2537 0.0 1.2 459964 12736 ? S Aug22 0:10 \/usr\/libexec\/clock-applet &#8211;oaf-activate-iid=OAFIID:GNOME_ClockApplet_Factory &#8211;oaf-ior-fd=34<\/p>\n<p>3. Display the process by time (Column 4 &#8211; TIME)<\/p>\n<p>$ ps vx | head -1; ps vx | sort -k4 -r| grep -v &#8216;PID&#8217; | head<\/p>\n<p>PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND 2478 ? Sl 1:52 351 593 447526 21876 2.1 \/usr\/lib\/vmware-tools\/sbin64\/vmtoolsd -n vmusr &#8211;blockFd 3<br \/>\n2458 ? S 0:16 228 1763 941440 23624 2.3 nautilus<br \/>\n2589 ? Sl 0:15 28 296 292259 14600 1.4 gnome-terminal<br \/>\n2421 ? Ssl 0:14 22 34 500541 9676 0.9 \/usr\/libexec\/gnome-settings-daemon<br \/>\n2479 ? S 0:13 23 403 310472 11996 1.1 nm-applet &#8211;sm-disable<br \/>\n2537 ? S 0:10 37 168 459795 12736 1.2 \/usr\/libexec\/clock-applet &#8211;oaf-activate-iid=OAFIID:GNOME_ClockApplet_Factory &#8211;oaf-ior-fd=34<br \/>\n2444 ? Ssl 0:10 25 64 445791 4872 0.4 \/usr\/bin\/pulseaudio &#8211;start &#8211;log-target=syslog<br \/>\n2445 ? S 0:07 51 593 322206 12684 1.2 gnome-panel<br \/>\n2516 ? Ss 0:06 4 151 275128 22316 2.2 gnome-screensaver<br \/>\n2522 ? Sl 0:05 5 41 231870 1960 0.1 \/usr\/libexec\/gvfs-afc-volume-monitor<\/p>\n<p>4. Display the top 10 real memory usage process (Column 8 &#8211; RSS)<\/p>\n<p>$ ps vx | head -1; ps vx | sort -k8 -nr| grep -v &#8216;PID&#8217; | head<\/p>\n<p>PID TTY STAT TIME MAJFL TRS DRS RSS %MEM COMMAND<br \/>\n2458 ? S 0:16 228 1763 941440 23624 2.3 nautilus<br \/>\n2516 ? Ss 0:06 4 151 275128 22316 2.2 gnome-screensaver<br \/>\n2478 ? Sl 1:52 351 593 447526 21876 2.1 \/usr\/lib\/vmware-tools\/sbin64\/vmtoolsd -n vmusr &#8211;blockFd 3<br \/>\n2507 ? S 0:01 73 2 321385 16680 1.6 python \/usr\/share\/system-config-printer\/applet.py<br \/>\n2589 ? Sl 0:15 28 296 292259 14600 1.4 gnome-terminal<br \/>\n2502 ? Sl 0:01 29 257 474362 13952 1.3 gpk-update-icon<br \/>\n2536 ? S 0:00 92 1607 394224 13372 1.3 \/usr\/bin\/gnote &#8211;panel-applet &#8211;oaf-activate-iid=OAFIID:GnoteApplet_Factory &#8211;oaf-ior-fd=22<br \/>\n2537 ? S 0:10 37 168 459795 12744 1.2 \/usr\/libexec\/clock-applet &#8211;oaf-activate-iid=OAFIID:GNOME_ClockApplet_Factory &#8211;oaf-ior-fd=34<br \/>\n2445 ? S 0:07 51 593 322206 12684 1.2 gnome-panel<br \/>\n2438 ? Sl 0:03 30 542 433105 12512 1.2 metacity<\/p>\n<p>Like above examples you can create so many one liners for you. But before using anyone of above one command, check your ps and sort command behavior then use them.<br \/>\nMostly, every other shell has its own argument for ps and sort but basics are same. For sorting any command output by particular column first understand that output\/column and then use sort commnd.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>A linux command to monitor the system process consuming resources on the server. If you are working on a linux system it is good to have at least basic understanding of this command.<\/p>\n<p>ps program or command when run take a snapshot of running processes at that time and display on the terminal which can [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,73],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7216"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7216"}],"version-history":[{"count":3,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7216\/revisions"}],"predecessor-version":[{"id":7219,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7216\/revisions\/7219"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7216"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7216"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7216"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}