【发布时间】:2011-12-13 10:36:28
【问题描述】:
我有一个在我的 linux 服务器后台运行的脚本,我想捕捉重启之类的信号或任何会杀死该脚本的信号,而是在实际退出之前保存任何重要信息。
我认为我需要捕获的大部分内容是 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);
}
?>
【问题讨论】: