触发断电时如何执行功能?
// need to make sure here that poweroff command is executed successfully before calling senEmail function
$output = exec('poweroff', $output, $return);
if (!$return) {
// command executed successfully
// now send email before poweroff completes its process
sendEmail();
}
sendEmail()
未触发,因为关闭已在处理。
答案 0 :(得分:1)
稍微取决于系统详细信息,poweroff
会调用shutdown
,这可能是您从程序调用的更好匹配。可以指示shutdown
在一段时间后关闭系统。
您可以发送邮件以响应SIGTERM信号,关机过程将发送该信号。有足够的资源可供使用。始终存在发送电子邮件所需的流程已经逐渐停止的风险。
最好还是可以改变依赖顺序:在合理的宽限期内调用shutdown
,尝试发送电子邮件,如果发送失败,则取消关闭。