2015年6月4日 星期四

[Linux 文章收集] UNIX / Linux: 3 Ways to Send Signal to Processes

Source From Here
Question
How do I send signal to another process? Can you explain me all available options to send signals to a process in UNIX / Linux environment?

Answer
You can send various signals to processes using one of the methods explains in this article. First of all, we write a python test.py to demonstrate the process:
- test.py:
  1. #!/usr/bin/env python  
  2. import signal, os  
  3.   
  4. def handler(signum, frame):  
  5.      print '\nSignal handler called with signal: {0}\n'.format(signum)  
  6.   
  7.   
  8. signal.signal(signal.SIGUSR1, handler)  
  9.   
  10. while True:  
  11.     pass  
Then execute it in the background:
$ ./test.py &
[1] 20993

1. Send Signal to a Process Using Kill
Use kill command to send a signal to a process. For example, if you want to send USR1 signal to the process “test.py”, do the following:
$ ps aux | grep test.py | grep -v grep
root 20993 97.5 0.3 115104 3480 pts/1 R 09:11 0:09 python ./test.py
$ kill -s USR1 20993
Signal handler called with signal: 10 # SIGUSR1 10 User defined signal 1 (POSIX)

Note: Refer to 4 Ways to Kill a Process – kill, killall, pkill, xkill.

2. Send Signal to a Process from Another Process
You can use the UNIX system call kill (from a C program) to send signal from one process to another. The following C code snippet shows how to use the killcommand. Kill system call takes two arguments:
1) the PID (process id) of the process that needs to be signalled
2) the signal that needs to be send to the process. Kill command returns 0 when it is successful.

Suppose we have below testing code:
- test.c
  1. #include   
  2. #include   
  3. #include   
  4.   
  5.   
  6. void main()  
  7. {  
  8.     int pid, sig, ret;  
  9.     printf("Please input PID: ");  
  10.     scanf("%d", &pid);  
  11.     printf("Please input Signal: ");  
  12.     scanf("%d", &sig);  
  13.     ret = kill(pid, sig);  
  14.     printf("Ret=%d\n", ret);  
  15. }  
Then compile it and execute it:
# gcc test.c // Compile will output a.out
# ./a.out
Please input PID: 20993
Please input Signal: 10
Ret=0

Signal handler called with signal: 10 # Sent out by test.py


3. Send Signal to a Process from Keyboard
When a process is running on the terminal, you can send signal to that process from the keyboard by using some specific combination of keys. The following are couple of examples.
* SIGINT (Ctrl + C) – You know this already. Pressing Ctrl + C kills the running foreground process. This sends the SIGINT to the process to kill it.
* You can send SIGQUIT signal to a process by pressing Ctrl + \ or Ctrl + Y

You can view the key mappings that sends specific signal to a process using the “stty -a” command as shown below.
# stty -a | grep intr
intr = ^C; quit = ^\; erase = ^?; kill = ^U; eof = ^D; eol = ;

Let's stop test.py now:
# jobs
[1]+ Running ./test.py & # test.py is running in background
# fg 1 # Put test.py to foreground
./test.py # Enter Ctrl+C to send signal SIGINT to test.py
^CTraceback (most recent call last):
File "./test.py", line 10, in
while True:
KeyboardInterrupt


Supplement
Linux and Unix fg command
Linux and Unix bg command
Linux Signals 
Python - signal — Set handlers for asynchronous events


沒有留言:

張貼留言

[Git 常見問題] error: The following untracked working tree files would be overwritten by merge

  Source From  Here 方案1: // x -----删除忽略文件已经对 git 来说不识别的文件 // d -----删除未被添加到 git 的路径中的文件 // f -----强制运行 #   git clean -d -fx 方案2: 今天在服务器上  gi...