Table of Content

Weight: 4

Description: Candidates should be able to perform basic process management.

Key Knowledge Areas:

  • Run jobs in the foreground and background
  • Signal a program to continue running after logout
  • Monitor active processes
  • Select and sort processes for display
  • Send signals to processes

Terms and Utilities:

&
bg
fg
jobs
kill
nohup
ps
top
free
uptime
pgrep
pkill
killall
screen

&

run in background

bg, fg, jobs

nohup

running process after logout

  • ignore hangup signal and append stdout and stderr to file
  • cannot run pipeline or command list, only for script
    echo "while sleep 30; do date; done" > test.sh
    nohup bash test.sh & 

nohup bash -c ‘killall wvdial ; sleep 60; wvdial’

ps

ps $(jobs -p)

-f full
-j jobs
-l long
--forest  process tree
-C command
-u user
--sort -sid,+comm

ps -j --forest
ps -ef
ps -fl 

ps -C getty -o user, pid, tty, time, comm

top

top -o %MEM

free

memory usage

free -mt # totally line in mb

uptime

load average for last 1,5,15 mins
oldhorse@dclab-u1504s:~$ free -mt
             total       used       free     shared    buffers     cached
Mem:           480        275        205          4         49        120
-/+ buffers/cache:        105        375
Swap:         2044          0       2044
Total:        2525        275       2250

kill

oldhorse@dclab-u1504s:~$ kill -l
 1) SIGHUP       2) SIGINT       3) SIGQUIT      4) SIGILL       5) SIGTRAP
 6) SIGABRT      7) SIGBUS       8) SIGFPE       9) SIGKILL     10) SIGUSR1
11) SIGSEGV     12) SIGUSR2     13) SIGPIPE     14) SIGALRM     15) SIGTERM
16) SIGSTKFLT   17) SIGCHLD     18) SIGCONT     19) SIGSTOP     20) SIGTSTP
21) SIGTTIN     22) SIGTTOU     23) SIGURG      24) SIGXCPU     25) SIGXFSZ
26) SIGVTALRM   27) SIGPROF     28) SIGWINCH    29) SIGIO       30) SIGPWR
31) SIGSYS      34) SIGRTMIN    35) SIGRTMIN+1  36) SIGRTMIN+2  37) SIGRTMIN+3
38) SIGRTMIN+4  39) SIGRTMIN+5  40) SIGRTMIN+6  41) SIGRTMIN+7  42) SIGRTMIN+8
43) SIGRTMIN+9  44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13
48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12
53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9  56) SIGRTMAX-8  57) SIGRTMAX-7
58) SIGRTMAX-6  59) SIGRTMAX-5  60) SIGRTMAX-4  61) SIGRTMAX-3  62) SIGRTMAX-2
63) SIGRTMAX-1  64) SIGRTMAX

Signal Meaning Usage

SIGTERM 
Terminate now (unless you can handle a SIGTERM) 
kill pid
kill -TERM pid
kill -15 pid

SIGHUP 
The modem hung up (or reload your configuration file
if you’re not a terminal application)
kill -HUP pid
kill 1 pid

SIGINT 
A user pressed Ctrl+C – interrupt and exit 
Ctrl+C

SIGSTOP 
A user pressed Ctrl+Z – suspend 
Ctrl+Z
kill -STOPpid

SIGCONT 
Continue 
fg or bg
kill -CONT pid

kill -s SIGTSTP %1 
jobs -l 
kill -s SIGCONT 

kill -s SIGTERM %2

killall -HUP inetd  # reload after inetd.conf change

pgrep , pkill, killall

pgrep -a process

pkill -e process  # echo which process killed

root@dclab-u1504s:~# pgrep -a sudo
1980 sudo nohup . test.sh

root@dclab-u1504s:~# killall -i sudo
Kill sudo(1980) ? (y/N) y

screen

1st term
screen -S sockname -L     # turn on logging, check screenlog.x under current folder 

2nd term 
screen -ls
screen -r screenname      # attach to detached screen 
screen -x screenname      # attach to not detached screen 

Quiz questions

1. What is a background process?
2. Name 4 ways you can kill a process which is in your jobs list.
3. How can you use ps to show a tree of processes similar to pstree?
4. What is the default signal sent by kill and killall?
5. If you have a process's name but not it's PID, what can you do to send a signal to it?
6. What will each of the following commands do, and who may run them?
killall -HUP init
kill -1 1
7. What happens if you kill 9
a process which is doing interactive console handling like man or vi?

Answers to quiz questions

1. A process which is not receiving terminal keyboard input.
2. kill %1; fg and Ctrl+C; killall processname;
kill 9112 (giving PID of process)
3. ps -axf
4. SIGTERM (terminate, signal 15)
5. Use killall rather than kill
6. Both send a hangup to process 1, init. This causes init to reload its configuration file /etc/inittab.
7. The console may be in an undefined state, and may have to be reset with the command reset or stty sane.