6. Finding Out Process StatusOne of the most important Unix commands is ps. With this command you can find outvarious information about processes running on your machine. Without options, thiscommand displays the list of processes running under the current shell. Example:hong:~ $psPID TTYTIME CMD31587 pts/000:00:00 bash31601pts/000:00:00 pshong:~ $Here, PIDis the process ID, a unique integer for each process. TTYtells you the terminal in which the process runs. The command ttywill display the name of your terminal. TIMEtells you the CPU time used by the process. CMDshows the command that was used to invoke the process.ps has many options. One often used is-f. With this option more information will be displayed:
In the above example, UIDshows the user who created the process. PPIDis the parent PID. STIMEis the starting time of the process.Another useful option is -l:hong:~$ps -lF SUIDPIDPPIDC PRINI ADDRSZ WCHANTTYTIME CMD000S100231587315860720-502 wait4pts/000:00:00bash000R100231611315870760-795 -pts/000:00:00 pshong:~ $In this example, Stells you the process state: Sfor sleep, Rfor running, Tfor stopped, Zfor zombie/defunct processes. UIDshows you the User ID number of the user who created the process. In Unix, each user has a username (such as xiein above example) and an integer uid (such as 1002 for user xie, and 0 for root). PRItells you the scheduling priority of the process.It is between 0 (high) and 127 (low). NIis the nice value, which is used to change the priority indirectly. The nice value is between -20 (a greater chance of a higher priority) and 19 (a greater chance of lower priority). To display all processes, use option -e. You can combine different options together too:hong:~ $ps –el
You will see that some processes are run by the root and are hence not associated with any terminal (TTY=?). Some of these are daemons, or servers.Another option -Hdisplays the process hierarchy, i.e. parent-child relationship. For example:[email protected]:~$ ps -efH | grep S900432DS900432D 32628 326250 21:19 ?00:00:00sshd: [email protected]/0S900432D 32629 326280 21:19 pts/000:00:00-bashS900432D958 326290 22:20 pts/000:00:00ps -efHS900432D959 326290 22:20 pts/000:00:00grep S900432D[email protected]:~$The above command line consists of two commands ps -efHand grep S900432Dconnected by a pipe ("|"), which links the standard output of the first command with the standard input of the second command. The command grepdisplays those input lines that contain the string "S900432D". The output in the above command line shows all processes started by the user "S900432D". It also reveals the hierarchical structures of these processes.Try all commands used in this question and try to understand the output with the help of manual page of the pscommand.7. Killing ProcessesYou can kill any process you own by sending the signal SIGKILL (signal 9). Firstly you needto find out the PID of the process you want to kill, then use the command:kill -9 pidNote you cannot kill a process that is owned by other users. However, root can kill any process.