如何在触发关机时执行功能?
// 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()
没有被触发,因为关闭已经在处理中。
最佳答案
稍微取决于系统细节,poweroff
将调用 shutdown
,这可能更适合从您的程序调用。 shutdown
可以指示在一段时间后关闭系统。
您可以发送邮件以响应 SIGTERM 信号,关闭进程将发送该信号。有很多可用的资源。始终存在发送电子邮件所需的流程已经停止运行的风险。
更好的是,您可以颠倒依赖顺序:在合理的宽限期内调用 shutdown
,尝试发送电子邮件,如果发送失败,则取消关闭。
关于php - 关机前执行一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47108147/