Cracking IT Interview

​​


Managing Process Continues..





top command:

Display top running jobs


This is most frequently used command to know CPU and memory utilization. It provides you dynamic real time view of top running processes in the system.


Example:

$top

last pid:  7978;  load averages:  0.14,  0.10,  0.11                                                                                                                                      up 69+13:28:39  03:45:58
59 processes:  1 running, 58 sleeping
CPU:  0.0% user,  0.0% nice,  0.0% system,  0.0% interrupt,  100% idle
Mem: 155M Active, 2814M Inact, 252M Wired, 166M Cache, 105M Buf, 367M Free
Swap: 512M Total, 1480K Used, 510M Free

  PID USERNAME      THR PRI NICE   SIZE    RES STATE    TIME   WCPU COMMAND
 8682 root            1  52    0 49468K 39008K select  69.7H 21.00% python2.7
13699 bind            4  20    0 44220K 25984K kqread  30:53  0.00% named
 8656 mysql          10  20    0 91252K 26296K sigwai  21:46  0.00% mysqld
 8474 root            1  20    0 11292K  2040K select   3:43  0.00% ntpd
 8694 root            1  20    0 29408K 13604K select   2:18  0.00% httpd
 8717 root            1  20    0 13064K  2860K select   2:06  0.00% sshd
 8780 root            1  20    0 11324K  2648K select   1:20  0.00% sendmail
 3573 root            1  20    0  9612K  1152K select   1:02  0.00% syslogd
 8821 root            1  20    0  9668K  1256K select   0:59  0.00% inetd
 3379 root            1  20    0 12128K  2328K select   0:35  0.00% devd
 8790 root            1  20    0  9644K  1268K nanslp   0:28  0.00% cron

----------------------               --------                      ---------------------

-----------------               ----------------------         -----------

Press "Q" to quit from the display screen.


Single character commands in "top":


press "h" to open the users help manual and try for the given commands:


Top version 3.5beta12, Copyright (c) 1984 through 1996, William LeFebvre
A top users display for Unix
These single-character commands are available:

^L      - redraw screen
q       - quit
h or ?  - help; show this text
C       - toggle the displaying of weighted CPU percentage
d       - change number of displays to show
e       - list errors generated by last "kill" or "renice" command
H       - toggle the displaying of threads
i or I  - toggle the displaying of idle processes
j       - toggle the displaying of jail ID
k       - kill processes; send a signal to a list of processes
m       - toggle the display between 'cpu' and 'io' modes
n or #  - change number of processes to display
o       - specify sort order (pri, size, res, cpu, time, threads, jid)
P       - toggle the displaying of per-CPU statistics
r       - renice a process
s       - change number of seconds to delay between updates
S       - toggle the displaying of system processes
a       - toggle the displaying of process titles
t       - toggle the display of this process
u       - display processes for only one user (+ selects all users)
z       - toggle the displaying of the system idle process

Hit any key to continue:


To list out specific number of processes with top commands:

top -n number


$top -n 10
last pid:  8369;  load averages:  0.07,  0.08,  0.08  up 69+13:53:50    04:11:09
60 processes:  2 running, 58 sleeping

Mem: 155M Active, 2808M Inact, 258M Wired, 166M Cache, 111M Buf, 368M Free
Swap: 512M Total, 1480K Used, 510M Free

  PID USERNAME      THR PRI NICE   SIZE    RES STATE    TIME   WCPU COMMAND
 8682 root            1  78    0 49468K 39008K RUN     69.7H 21.00% python2.7
13699 bind            4  20    0 44220K 25984K kqread  30:53  0.00% named
 8656 mysql          10  20    0 91252K 26296K sigwai  21:46  0.00% mysqld
 8474 root            1  20    0 11292K  2040K select   3:43  0.00% ntpd
 8694 root            1  20    0 29408K 13604K select   2:18  0.00% httpd
 8717 root            1  21    0 13064K  2860K select   2:06  0.00% sshd
 8780 root            1  20    0 11324K  2648K select   1:20  0.00% sendmail
 3573 root            1  20    0  9612K  1152K select   1:03  0.00% syslogd
 8821 root            1  20    0  9668K  1256K select   0:59  0.00% inetd
 3379 root            1  20    0 12128K  2328K select   0:35  0.00% devd







nice and renice commands:

Set the job run priority


These commands are used for changing the priority of the running jobs.

"nice" command is used to set the priority while you are going to run any job or command. "renice" is used when the job is already in running phase.


Jobs are having some nice values along with it while running. This value varies between -20 to +19. Higher nice value implies lower priority. So,

-20 nice value -> Highest priority

+19 nice value -> Lowest priority


Give command "ps -al" to see the nice value of the processes:


$ps -al
  UID   PID  PPID CPU PRI
NI    VSZ    RSS MWCHAN STAT  TT     TIME COMMAND
    0  8858     1   0  20  0  10124   1592 wait   Is    v0  0:00.01 login [pam] (login)
131076 12529  8858   0  22 -5   3916   1916 wait   I<    v0  0:00.01 -bash (bash)
    0 12627 12529   0  20 -5  11372   1788 select I<    v0  0:00.01 sudo su -
    0 12673 12627   0  20 -5  10124   1388 wait   I<    v0  0:00.00 su -
    0 12674 12673   0  20  0   3900   1992 ttyin  I+    v0  0:00.05 bash
    0  8861     1   0  52  0   9612    928 ttyin  Is+   v1  0:00.00 /usr/libexec/getty Pc ttyv1
    0  8862     1   0  52  0   9612    928 ttyin  Is+   v2  0:00.00 /usr/libexec/getty Pc ttyv2
    0  8863     1   0  52  0   9612    928 ttyin  Is+   v3  0:00.00 /usr/libexec/getty Pc ttyv3



When process starts, "0" is the default nice value. we can also see the nice value of the processes using "top" command and change value as well for running jobs:


$top -n 5
last pid: 12287;  load averages:  0.19,  0.16,  0.10  up 69+16:02:48    06:20:07
72 processes:  1 running, 68 sleeping, 3 stopped

Mem: 161M Active, 2828M Inact, 260M Wired, 166M Cache, 112M Buf, 340M Free
Swap: 512M Total, 1480K Used, 510M Free

  PID USERNAME      THR PRI
NICE   SIZE    RES STATE    TIME   WCPU COMMAND
 8682 root            1  52    0 49468K 39008K select  70.0H 16.99% python2.7
13699 bind            4  20    0 44220K 25984K kqread  30:56  0.00% named
 8656 mysql          10  20    0 91252K 26300K sigwai  21:48  0.00% mysqld
 8474 root            1  20    0 11292K  2040K select   3:43  0.00% ntpd
 8694 root            1  20    0 29408K 13604K select   2:18  0.00% httpd


Another, only the super-user is having this permission for changing the priority with nice value. 


Command to change the priority of the job before running it:


Syntax : nice -n nice_value your_command

Example : $nice -n 5 sh print1to10.sh


Command to change the priority of the already running job:


Syntax: renice nice_value -p process_PID

Example: $renice 5 -p 12627







Foreground and Background Jobs:


When we run any command in Unix, by default its process run at the foreground. No other jobs can be executed during the foreground job execution as it gives the output on the standard output screen after job execution. These jobs are foreground jobs.


There are several occasion when a job need a long time to execute specially when you are running some script with huge data. If you have to work for the other jobs or running script at the same time, it is not possible to depend on foreground processes in such circumstances. Solution is, running jobs in background. 


While running jobs in background, even if you logout from the system it keeps running all the time till completion of job.


How to run a job in background? Use "&" at the end of the given command preceded by a space.


Example:


$ls -m *.sh &
[1] 20514
$cron.sh, first.sh, my_smple.sh, rev.sh, test1.sh, test2.sh, v2.sh, v4.sh, v8.sh

[1]+  Done                    ls -m *.sh
$


nohup command & : 
Running Jobs in Background


"nohup" stands for "no hangups". The hangup (HUP) singal, which is sent to process to inform that user has logging off from the system. This signal is just avoided by nohup command.


With this command when we run the job in background, it creates "nohup.out" file for storing the process output. This file stores both standard output and error messages if any error occurs.


$nohup ls -m *.sh &
[1] 20701
$appending output to nohup.out

[1]+  Done                    nohup ls -m *.sh
$


to check the output:


$cat nohup.out
cron.sh, first.sh, my_smple.sh, rev.sh, test1.sh, test2.sh, v2.sh, v4.sh, v8.sh
$


to check the run time log:


$tail -f nohup.out
cron.sh, first.sh, my_smple.sh, rev.sh, test1.sh, test2.sh, v2.sh, v4.sh, v8.sh

^Z
[1]+  Stopped                 tail -f nohup.out
$







jobs command:

Display background Processes

List out all the running background jobs and the jobs just completed till the session logout time.

Example:

$jobs
[1]-  Stopped                 ping unixinterview.com
[2]+  Stopped                 ping unixinterview.com
$

Options to use with command:


  • jobs -l : List out process ID with the jobs command output


  • jobs -p : List out only process IDs for all the jobs.


  • jobs -r : List out only jobs which are running currently in system.


  • jobs -s : List out only jobs which are stopped currently. 


Examples:

$jobs -l
[1]  10790 Suspended: 18           ping unixinterview.com
[2]- 10807 Suspended: 18           ping unixinterview.com
[3]+ 10827 Stopped (tty output): 22top
$
$jobs -p
10790
10807
10827
$
$jobs -r
$
$jobs -s
[1]   Stopped                 ping unixinterview.com
[2]-  Stopped                 ping unixinterview.com
[3]+  Stopped                 top
$


fg, bg commands :

Switching Foreground and Background jobs


"fg" command is used to take the background running jobs to foreground.

"bg" command is used to send the foreground job to run in background.


Example:

$jobs
[1]+  Stopped                 tail -f nohup.out
$
$fg
tail -f nohup.out

^Z
[1]+  Stopped                 tail -f nohup.out
$


Use jobs command to list out the running jobs with these commands. When there are multiple jobs, give job ID with preceding "%" sign:


$jobs
[1]   Stopped                 ping unixinterview.com
[2]-  Stopped                 ping unixinterview.com
[3]+  Stopped                 tail -f nohup.out

​$
$fg %1
ping unixinterview.com
64 bytes from 198.71.232.3: icmp_seq=2 ttl=45 time=41.121 ms
64 bytes from 198.71.232.3: icmp_seq=3 ttl=45 time=42.835 ms
64 bytes from 198.71.232.3: icmp_seq=4 ttl=45 time=40.718 ms
64 bytes from 198.71.232.3: icmp_seq=5 ttl=45 time=41.325 ms
^Z
[1]+  Stopped                 ping unixinterview.com
$

$fg %3
tail -f nohup.out
^Z
[3]+  Stopped                 tail -f nohup.out
$



NEXT ->