Sunday, September 18, 2011

Process Management in Unix Machine

Process Management

The ps command enumerates the processes running on your system. The ps command is similar to the Task Manager in Windows OS, in that it lists the running processes (programs).

[root@server ~]# ps
  PID TTY          TIME CMD
 2980 pts/1     bash
 2997 pts/1     ps
[root@server ~]# which ps
/bin/ps

As per existing standards required binaries are to exist in /bin directory and ps is one such binary.

[root@server ~]# rpm -ql procps-3.2.3-7EL
/bin/ps
/lib/libproc-3.2.3.so
/sbin/sysctl
/usr/bin/free
/usr/bin/pgrep
/usr/bin/pkill
/usr/bin/pmap
/usr/bin/skill
/usr/bin/slabtop
/usr/bin/snice
/usr/bin/tload
/usr/bin/top
/usr/bin/uptime
/usr/bin/vmstat
/usr/bin/w
/usr/bin/watch
/usr/share/doc/procps-3.2.3
(Output filtered)

The ps with the –e option will list all the processing running on the system

[root@server ~]# ps -e
  PID TTY          TIME CMD
    1 ?         init
    2 ?         ksoftirqd/0
    3 ?         events/0
    4 ?         khelper
    5 ?         kacpid
   18 ?         kblockd/0
   28 ?         pdflush
   29 ?         pdflush
   31 ?         aio/0
   19 ?         khubd
   30 ?         kswapd0
  105 ?         kseriod
  176 ?         scsi_eh_0
  188 ?         kmirrord/0
  197 ?         kjournald
 1042 ?         udevd
The command ps –aux or ps aux (BSD Style) will list all process and their ids and will provide detailed information about the processes running on the system.

[root@server ~]# ps -aux
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.3/FAQ
USER       PID %CPU %MEM   VSZ  RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.2  1664  556 ?        S       0:00 init [5]
root         2  0.0  0.0     0    0 ?        SN      0:00 [ksoftirqd/0]
root         3  0.0  0.0     0    0 ?        S<   07:36   0:00 [events/0]
root         4  0.0  0.0     0    0 ?        S<   07:36   0:00 [khelper]
root         5  0.0  0.0     0    0 ?        S<   07:36   0:00 [kacpid]
root        18  0.0  0.0     0    0 ?        S<   07:36   0:00 [kblockd/0]
root        28  0.0  0.0     0    0 ?        S       0:00 [pdflush]
root        29  0.0  0.0     0    0 ?        S       0:00 [pdflush]
root        31  0.0  0.0     0    0 ?        S<   07:36   0:00 [aio/0]
root        19  0.0  0.0     0    0 ?        S       0:00 [khubd]
root        30  0.0  0.0     0    0 ?        S       0:00 [kswapd0]
root       105  0.0  0.0     0    0 ?        S       0:00 [kseriod]
root       176  0.0  0.0     0    0 ?        S       0:00 [scsi_eh_0]
root       188  0.0  0.0     0    0 ?        S<   07:36   0:00 [kmirrord/0]
root       197  0.0  0.0     0    0 ?        S       0:00 [kjournald]
root      1042  0.0  0.1  2452  448 ?        S<s     0:00 udevd
root      3022  0.8  1.1  5484 3040 pts/2    S       0:07 /usr/lib/vmware-tools/bin32/vmware
root      3087  0.0  0.3  2808  772 pts/1    R+      0:00 ps -aux

-a  all w/ tty, including other users  -t by tty
-u  by effective user ID (supports names)
-x  processes w/o controlling ttys

PROCESS STATE CODES

Here are the different values that the s, stat and state output specifiers (header "STAT" or "S") will display to describe the state of a process.
D    Uninterruptible sleep (usually IO)
R    Running or runnable (on run queue)
S    Interruptible sleep (waiting for an event to complete)
T    Stopped, either by a job control signal or because it is being traced.
W    paging (not valid since the 2.6.xx kernel)
X    dead (should never be seen)
Z    Defunct ("zombie") process, terminated but not reaped by its parent.

For BSD formats and when the stat keyword is used, additional characters may be displayed:
<    high-priority (not nice to other users)
N    low-priority (nice to other users)
L    has pages locked into memory (for real-time and custom IO)
s    is a session leader
l    is multi-threaded (using CLONE_THREAD, like NPTL pthreads do)
+    is in the foreground process group

Note: Use ps –aux | less command to display output one page at a time.

[root@server ~]# ps -aux | grep sshd
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.3/FAQ
root      2037  0.0  0.6  5892 1652 ?        Ss      0:00 /usr/sbin/sshd
root      3152  0.0  0.1  2444  352 pts/1    R+      0:00 grep sshd

The above command filters the ps –aux and lists only the sshd process.

[root@server ~]# ps -aux | grep tty
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.3/FAQ
root      2199  0.0  0.1  3112  408 tty1     Ss+     0:00 /sbin/mingetty tty1
root      2201  0.0  0.1  2972  404 tty2     Ss+     0:00 /sbin/mingetty tty2
root      2202  0.0  0.1  2192  408 tty3     Ss+     0:00 /sbin/mingetty tty3
root      2203  0.0  0.1  2476  408 tty4     Ss+     0:00 /sbin/mingetty tty4
root      2452  0.0  0.1  1476  408 tty5     Ss+     0:00 /sbin/mingetty tty5
root      2525  0.0  0.1  2464  408 tty6     Ss+    0:00 /sbin/mingetty tty6
root      3154  0.0  0.2  5028  672 pts/1    S+      0:00 grep tty

The above command filters all processes and lists only tty (Tele Type Terminals) processes.

[root@server ~]# ps -t tty1
  PID TTY          TIME CMD
 2199 tty1     mingetty
[root@server ~]# ps -t tty2
  PID TTY          TIME CMD
 2201 tty2     mingetty

The above command lists only -tty processes, -t option.

The who command also displays information about tty/pts terminals.

[root@server ~]# who
root     :0           Dec 20
root     pts/1        Dec 20 (:0.0)
root     pts/2        Dec 20 (:0.0)
[root@server ~]# who -a
                        Dec 20                216 id=si    term=0 exit=0
           system boot  Dec 20
           run-level 5  Dec 20                    last=S
                        Dec 20               1497 id=l5    term=0 exit=0
                        Dec 20               2199 id=1
                        Dec 20               2201 id=2
LOGIN      tty3         Dec 20               2202 id=3
                        Dec 20               2203 id=4
                        Dec 20               2452 id=5
                        Dec 20               2525 id=6
                        Dec 20               2571 id=x
root     ? :0           Dec 20    ?          2829
root     + pts/1        Dec 20    .          2978 (:0.0)
root     + pts/2        Dec 20         2978 (:0.0)


The pstree utility

[root@server ~]# which pstree
/usr/bin/pstree
[root@server ~]# rpm -qf /usr/bin/pstree
psmisc-21.4-4
[root@server ~]# rpm -ql psmisc-21.4-4
/sbin/fuser
/usr/bin/killall
/usr/bin/pstree
/usr/bin/pstree.x11
/usr/share/locale/de/LC_MESSAGES/psmisc.mo
/usr/share/locale/en/LC_MESSAGES/psmisc.mo
/usr/share/locale/fr/LC_MESSAGES/psmisc.mo
/usr/share/locale/it/LC_MESSAGES/psmisc.mo
/usr/share/locale/pt/LC_MESSAGES/psmisc.mo
/usr/share/locale/sv/LC_MESSAGES/psmisc.mo
/usr/share/man/man1/fuser.1.gz
/usr/share/man/man1/killall.1.gz
/usr/share/man/man1/pstree.1.gz

The pstree command displays the processes in a tree like view.



Process Selections

The free command lists the amount of total memory, used, free, shared, buffers, cached and the swap usage.

[root@server ~]# free
             total       used       free     shared    buffers     cached
Mem:        256044     247204       8840          0      15440     140916
-/+ buffers/cache:      90848     165196
Swap:       524280          0     524280
[root@server ~]#
                              
The free command with the –m option returns the memory and swap in megabytes.

[root@server ~]# free -m
             total       used       free     shared    buffers     cached
Mem:           250        241          8          0         15        137
-/+ buffers/cache:         88        161
Swap:          511          0        511

Use the free command with the –m and –s (seconds) to update the memory usage every 3 seconds.

[root@server ~]# free -m -s 3
             total       used       free     shared    buffers     cached
Mem:           250        241          8          0         15        137
-/+ buffers/cache:         89        160
Swap:          511          0        511

             total       used       free     shared    buffers     cached
Mem:           250        241          8          0         15        137
-/+ buffers/cache:         89        161
Swap:          511          0        511

Use the top command to display information about the processes running on the system. The top command updates the information displayed every 3 seconds.

[root@server ~]# top

top - up  ,  3 users,  load average: 0.05, 0.11, 0.15
Tasks:  76 total,   1 running,  73 sleeping,   2 stopped,   0 zombie
Cpu(s):  6.0% us,  5.6% sy,  0.0% ni, 88.4% id,  0.0% wa,  0.0% hi,  0.0% si
Mem:    256044k total,   247976k used,     8068k free,    15540k buffers
Swap:   524280k total,        0k used,   524280k free,   140992k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 2675 root      15   0 38508  15m 5096 S  6.8  6.0   1:09.98 X
 2978 root      15   0 38348  14m 8680 S  2.9  5.6   0:17.09 gnome-terminal
24855 root      16   0  2612  940  756 R  0.7  0.4   0:00.12 top
 3022 root      15   0  5484 3056 2496 S  0.3  1.2   0:29.69 vmware-toolbox-
    1 root      16   0  1664  556  480 S  0.0  0.2   0:00.86 init
    2 root      34  19     0    0    0 S  0.0  0.0   0:00.12 ksoftirqd/0
    3 root       5 -10     0    0    0 S  0.0  0.0   0:00.22 events/0
    4 root       5 -10     0    0    0 S  0.0  0.0   0:00.02 khelper
    5 root      15 -10     0    0    0 S  0.0  0.0   0:00.00 kacpid
   18 root       5 -10     0    0    0 S  0.0  0.0   0:00.14 kblockd/0
   28 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pdflush
   29 root      15   0     0    0    0 S  0.0  0.0   0:00.44 pdflush
   31 root       8 -10     0    0    0 S  0.0  0.0   0:00.00 aio/0
   19 root      15   0     0    0    0 S  0.0  0.0   0:00.00 khubd
   30 root      15   0     0    0    0 S  0.0  0.0   0:00.12 kswapd0
  105 root      25   0     0    0    0 S  0.0  0.0   0:00.00 kseriod
  176 root      22   0     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_0
  188 root       6 -10     0    0    0 S  0.0  0.0   0:00.00 kmirrord/0
  197 root      16   0     0    0    0 S  0.0  0.0   0:00.87 kjournald

Pressing SHIFT+f will bring up a menu to select more options in the top command.

Note: zombie processes tend to be in deep sleep.

[root@server ~]# top --help
top: procps version 3.2.3
usage:  top -hv | -bcisS -d delay -n iterations [-u user | -U user] -p pid [,pid ...]

The top command with the –u username option will display usage of a particular user.

[root@server ~]# top -u root

top - up  ,  3 users,  load average: 0.07, 0.05, 0.06
Tasks:  77 total,   1 running,  73 sleeping,   3 stopped,   0 zombie
Cpu(s):  2.8% us,  3.8% sy,  0.0% ni, 93.4% id,  0.0% wa,  0.0% hi,  0.0% si
Mem:    256044k total,   248564k used,     7480k free,    15648k buffers
Swap:   524280k total,        0k used,   524280k free,   141020k cached

  PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND
 2675 root      15   0 38508  15m 5112 S  3.3  6.0   1:21.08 X
 2978 root      15   0 38216  14m 8680 S  1.6  5.6   0:21.18 gnome-terminal
 3022 root      15   0  5484 3056 2496 S  1.0  1.2   0:38.82 vmware-toolbox-
24886 root      17   0  3612  944  760 R  0.7  0.4   0:00.04 top
 2189 root      16   0  6524 4004 1612 S  0.3  1.6   0:10.54 hald
 2956 root      25  10 32156  18m  10m S  0.3  7.5   0:06.61 rhn-applet-gui
 2972 root      15   0 21324 9224 6936 S  0.3  3.6   0:03.83 mixer_applet2
    1 root      16   0  1664  556  480 S  0.0  0.2   0:00.93 init
    2 root      34  19     0    0    0 S  0.0  0.0   0:00.12 ksoftirqd/0
    3 root       5 -10     0    0    0 S  0.0  0.0   0:00.25 events/0
    4 root       5 -10     0    0    0 S  0.0  0.0   0:00.02 khelper
    5 root      15 -10     0    0    0 S  0.0  0.0   0:00.00 kacpid
   18 root       5 -10     0    0    0 S  0.0  0.0   0:00.15 kblockd/0
   28 root      20   0     0    0    0 S  0.0  0.0   0:00.00 pdflush
   29 root      15   0     0    0    0 S  0.0  0.0   0:00.47 pdflush
   31 root       8 -10     0    0    0 S  0.0  0.0   0:00.00 aio/0
   19 root      15   0     0    0    0 S  0.0  0.0   0:00.00 khubd
   30 root      15   0     0    0    0 S  0.0  0.0   0:00.12 kswapd0
  105 root      25   0     0    0    0 S  0.0  0.0   0:00.00 kseriod
  176 root      22   0     0    0    0 S  0.0  0.0   0:00.00 scsi_eh_0
  188 root       6 -10     0    0    0 S  0.0  0.0   0:00.00 kmirrord/0
  197 root      15   0     0    0    0 S  0.0  0.0   0:00.92 kjournald

Changing Priority (renice)

  1. Open a new terminal and run the top command.
  2. Open a new terminal and run the sequence command seq 1000000 > seqmilrecord.txt
  3. Go back to terminal one where you ran top and sort the process in order of high cpu usage.
  4. Press n to change priority of a process.
  5. Enter the PID of the process in our case it was seq.
  6. Enter new renice value to increase or decrease priority of the seq command. Where -19 is the highest value where process gets a highest priority and 20 is the lowest value, meaning the lowest priority.

Killing a Process

Press k in the top menu to kill a process and type in the PID of the process you want to kill (shut).

The kill command.

The kill command is used to kill a process by sending signal to the process using the Process ID(PID).

kill –l

Will list all signals supported by the kill command.

kill processid

Will kill the process whose id was given to kill.

Killall command

The killall command kills all the processes of the given process name.

#killall seq

Will kill all seq processes running in other shells (in background &).

Note: The information listed by ps, top etc is pulled from a filesystem /proc which is mounted in memory. Below is a example of cpuinfo kept in the /proc filesystem.

[root@server ~]# cat /proc/cpuinfo
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 15
model           : 3
model name      : Intel(R) Pentium(R) 4 CPU 2.40GHz
stepping        : 8
cpu MHz         : 2389.200
cache size      : 1024 KB
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 5
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss pni ds_cpl
bogomips        : 4702.20

The uptime command displays information about the time since the system has been up and running.

[root@server ~]# uptime
  up  ,  3 users,  load average: 0.02, 0.08, 0.14


The jobs command is used to display the jobs running in the background. The fg (Foreground) commands is used to display foreground processes running in the shell. Both are inbuilt inside the shell and are not separate binary files.

[root@server ~]#pstree

No comments:

Post a Comment