Saturday, July 26, 2014

Unix Shell: Process Control(2)

1. Terminate the process
When terminating a process, we should firstly try HUP, then try TERM, lastly KILL. "HUP" and "TERM" signal gives the process a chance to clean up the temporary files.

script_1:
 #! /bin/bash  
   
 echo "" >temp  
 a=5  
 while [ $a -ne 0 ]  
 do  
   ((a++))  
   echo $a >>temp  
   sleep 5  
 done  

terminal:
1 - 3) Start the signal_1 3 times
4) Run the ps command, there are 3 processes about script_1
5) Open the temp file, now 3 script_1 processes are trying to add text into temp
6) send HUP signal to 3285, which kills the process
7) send TERM signal to 3287, which also kills the process
8) send KILL signal to 3289, which also kills the process
9) Run ps command, it indicates that all 3 processes about script_1 are killed.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 &  
 [2] 3285  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 &  
 [3] 3287  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 &  
 [4] 3289  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ps  
  PID TTY     TIME CMD  
  2954 pts/0  00:00:00 bash  
  3162 pts/0  00:00:04 emacs  
  3285 pts/0  00:00:00 script_1  
  3287 pts/0  00:00:00 script_1  
  3288 pts/0  00:00:00 sleep  
  3289 pts/0  00:00:00 script_1  
  3290 pts/0  00:00:00 sleep  
  3291 pts/0  00:00:00 sleep  
  3292 pts/0  00:00:00 ps  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ cat temp  
   
 6  
 7  
 7  
 7  
 8  
 8  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ kill -HUP 3285  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ kill -TERM 3287  
 [2]  Hangup         ./script_1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ kill -KILL 3289  
 [3]- Terminated       ./script_1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ps  
  PID TTY     TIME CMD  
  2954 pts/0  00:00:00 bash  
  3162 pts/0  00:00:04 emacs  
  3312 pts/0  00:00:00 ps  
 [4]+ Killed         ./script_1  

We should be cautious about the KILL signal, since it will leave the temporary files (like lock) in file system, which may be a problem for this process running next time.

Kill the process by name:
terminal:
1 - 3) start the process script_1 for 3 times, now we have 3 script_1 processes running. Process IDs are: 3333, 3335, 3338
4) Run ps command to list all running processes. There are 3 script_1 processes running now.
5) Run "pgrep" command to list all process IDs whose name is script_1, we get 3 results: 3333, 3335, 3338
6) Run "pkill" command send HUP signal to all processes with name "script_1", be cautious it may kill more process than we expect.
7) Run "ps" command, all script_1 processes are killed now.
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 &  
 [2] 3333  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 &  
 [3] 3335  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script_1 &  
 [4] 3338  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ps  
  PID TTY     TIME CMD  
  2954 pts/0  00:00:00 bash  
  3162 pts/0  00:00:05 emacs  
  3333 pts/0  00:00:00 script_1  
  3335 pts/0  00:00:00 script_1  
  3337 pts/0  00:00:00 sleep  
  3338 pts/0  00:00:00 script_1  
  3339 pts/0  00:00:00 sleep  
  3340 pts/0  00:00:00 sleep  
  3341 pts/0  00:00:00 ps  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ pgrep script_1  
 3333  
 3335  
 3338  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ pkill -HUP script_1  
 [2]  Hangup         ./script_1  
 [3]- Hangup         ./script_1  
 [4]+ Hangup         ./script_1  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ps  
  PID TTY     TIME CMD  
  2954 pts/0  00:00:00 bash  
  3162 pts/0  00:00:05 emacs  
  3357 pts/0  00:00:00 ps  

No comments:

Post a Comment