我有我的Linux服務器的後臺運行的腳本,我想趕上像重啓或任何會殺死這個腳本,而是真正退出之前任何重要提示信息保存信號。如何捕獲KILL或HUP或用戶中止信號?
我想大多數的什麼,我需要捕捉的,SIGINT,SIGTERM,SIGHUP,SIGKILL。
如何捕捉任何這些信號,並已就執行退出功能,否則繼續執行不管它在做什麼?
僞Perl代碼:
#!/usr/bin/perl
use stricts;
use warnings;
while (true)
{
#my happy code is running
#my happy code will sleep for a few until its breath is back to keep running.
}
#ops I have detected an evil force trying to kill me
#let's call the safe exit.
sub safe_exit()
{
# save stuff
exit(1);
}
僞PHP代碼:
<?php
while (1)
{
#my happy code is running
#my happy code will sleep for a few until its breath is back to keep running.
}
#ops I have detected an evil force trying to kill me
#let's call the safe exit.
function safe_exit()
{
# save stuff
exit(1);
}
?>
http://stackoverflow.com/questions/5180539/quick-perl-sigint-handler – daxim