Transferred from: http://www.jb51.net/article/74844.htm
In the LIUNX system in order to execute a command every minute, the most common way is crontab, if you do not want to use Crontab, with colleagues in the program can use the timer to achieve this function, so began to grope, found that need some signal knowledge ...
See which signals your Linux supports: kill-l
1) SIGHUP 2) SIGINT 3) Sigquit 4) Sigill 5) SIGTRAP
6) SIGABRT 7) Sigbus 8) SIGFPE 9) SIGKILL) SIGUSR1
One) (SIGSEGV) (SIGUSR2) sigpipe) sigalrm) SIGTERM
Sigstkflt) (SIGCHLD) Sigcont SIGSTOP) SIGTSTP
(Sigttin) Sigttou () Sigurg) sigxcpu) Sigxfsz
(SIGVTALRM) sigprof) sigwinch SIGIO) SIGPWR
Sigsys) (sigrtmin) sigrtmin+1) sigrtmin+2 Notoginseng) sigrtmin+3
sigrtmin+4) sigrtmin+5 (sigrtmin+6) sigrtmin+7) sigrtmin+8
sigrtmin+9) (sigrtmin+10) sigrtmin+11 () sigrtmin+12) sigrtmin+13
(sigrtmin+14) sigrtmin+15 () SIGRTMAX-14) SIGRTMAX-13) SIGRTMAX-12
SIGRTMAX-11) SIGRTMAX-10 SIGRTMAX-9) SIGRTMAX-8 () SIGRTMAX-7
(SIGRTMAX-6) (SIGRTMAX-5) SIGRTMAX-4) SIGRTMAX-3) SIGRTMAX-2
SIGRTMAX-1) Sigrtmax
[Email protected]:~#
Signal: The way communication between processes is a software interrupt. Once a process receives a signal, it interrupts the original program execution process to process the signal. The operating system specifies the default behavior after a process receives a signal, but we can modify the behavior of the process after it receives the signal by binding the signal handler function, and there are two signals that are immutable sigstop and Sigkill.
There are two general reasons for sending a signal:
1 (passive) The kernel detects a system event. For example, a child process exit sends a SIGCHLD signal like a parent process. Keyboard presses CONTROL+C sends SIGINT signal
2 (Active) sends a signal to the specified process via system call Kill
In C language There is a Setitimer function, function Setitimer can provide three kinds of timers, they are independent of each other, any one timing completion will send a timing signal to the process, and automatically re-timer. The parameter which determines the type of timer:
The
itimer_real timed real time, the same as the alarm type. SIGALRM
itimer_virt the actual execution time of the timer process in the user state. SIGVTALRM
itimer_prof The actual execution time of the timing process in the user state and kernel mentality. Sigprof
These three kinds of timers when the timing is complete, the signal sent to the process is different, wherein the Itimer_real class timer sends SIGALRM signal, Itimer_ The Virt class timer sends the SIGVTALRM signal, and the Itimer_real class timer sends the SIGPROF signal. The
function alarm essentially sets a low-precision, non-overloaded Itimer_real class timer, which can only be accurate to seconds, and can only be timed once per set. The timers set by the function Setitimer are different, they can not only be timed to subtle (theoretically), but can also be automatically cycled and timed. In a UNIX process, you cannot use both the alarm and the Itimer_real class timers.
SIGINT terminating process interrupt process (CONTROL+C)
SIGTERM terminating process software termination signal
SIGKILL Terminate process Kill process
SIGALRM Alarm Clock signal
The pre-knowledge is almost ready, and it's time to march into Python's signal.
Define the signal name
The signal package defines the individual signal names and their corresponding integers, such as
Import signal print signal. SIGALRM print signal. Sigcont
Python uses the same signal name as Linux. You can pass
$man 7 Signal
Inquire
Preset Signal processing functions
The core of the signal package is to use the signal.signal () function to preset the (register) signal processing function as follows:
Signal.signal (Signalnum, Hanlder)
Signalnum is a signal, handler is the processing function of the signal. We mentioned in the signal base that the process can ignore the signal, can take the default action, and can customize the operation. When handler is signal. Sig_ign, the signal is ignored (ignore). When handler is Singal. SIG_DFL, the process takes the default action. When handler is a function name, the process takes the action defined in the function.
Import Signal
# Define Signal handler function
def Myhanlder (Signum, frame):
Print ('I Received: ', Signum)
# register signal. SIGTSTP ' s handler
< Span style= "color: #008000;" > signal.signal (signal. SIGTSTP, MyHandler)
< Span style= "color: #008000;" > Signal.pause ()
print ('End of Signal Demo')
In the main program, we first use the signal.signal () function to preset the signal processing function. We then execute Signal.pause () to let the process pause to wait for the signal. When the signal SIGUSR1 is passed to the process, the process resumes from the pause and, according to the preset, executes the SIGTSTP signal handler function MyHandler (). MyHandler two parameters one is used to identify the signal (Signum), and the other is used to obtain the status of the process stack when the signal occurs (stack frame). Both of these parameters are passed by the signal.singnal () function.
The above program can be saved in a file (such as test.py). We use the following method to run:
Python test.py
For the process to run. When the program runs to Signal.pause (), the process pauses and waits for a signal. At this point, a SIGTSTP signal is sent to the process by pressing CTRL + Z. As we can see, the process executes the myhandle () function, which is then returned to the main program to continue execution. (Of course, you can also query the process ID with $ps, and then use $kill to signal.) )
(The process does not have to use Signal.pause () to pause to wait for a signal, it can also accept signals in the work, such as changing the above signal.pause () to a cycle that takes a long time to work. )
We can change the operations in MyHandler () to suit our needs to personalize the processing for different signals.
This part of the basic knowledge has deepened the understanding of the basic knowledge of signal, it is worth a turn and convenient to consult
Turn: Python signal signal