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:
Then execute it in the background:
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:
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:
Suppose we have below testing code:
- test.c
Then compile it and execute it:
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.
You can view the key mappings that sends specific signal to a process using the “stty -a” command as shown below.
Let's stop test.py now:
Supplement
* Linux and Unix fg command
* Linux and Unix bg command
* Linux Signals
* Python - signal — Set handlers for asynchronous events
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:
- #!/usr/bin/env python
- import signal, os
- def handler(signum, frame):
- print '\nSignal handler called with signal: {0}\n'.format(signum)
- signal.signal(signal.SIGUSR1, handler)
- while True:
- pass
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:
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:
Suppose we have below testing code:
- test.c
- #include
- #include
- #include
- void main()
- {
- int pid, sig, ret;
- printf("Please input PID: ");
- scanf("%d", &pid);
- printf("Please input Signal: ");
- scanf("%d", &sig);
- ret = kill(pid, sig);
- printf("Ret=%d\n", ret);
- }
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.
You can view the key mappings that sends specific signal to a process using the “stty -a” command as shown below.
Let's stop test.py now:
Supplement
* Linux and Unix fg command
* Linux and Unix bg command
* Linux Signals
* Python - signal — Set handlers for asynchronous events
沒有留言:
張貼留言