简体   繁体   English

为什么这个程序在init / reboot / shutdown时没有收到SIGTERM?

[英]Why didn't this program receive SIGTERM on init/reboot/shutdown?

I need to intercept reboot or shutdown. 我需要拦截重启或关机。 The prgram is like this: prgram是这样的:

void sig_handler(int sig) {
    if (sig == SIGTERM) {
        /* do something */
    }
}

int main() {
    ....
    signal(SIGTERM, sig_handler);

    /* daemon */
    pid = fork();
    if (pid > 0) exit(EXIT_SUCCESS);
    // I didn't do setsid() to retain process group id.
    ....
}

This works when I tested by 'kill -15 '. 当我用'kill -15'测试时,这个工作正常。 However, when I tried 'reboot' or 'shutdown' command, it never received the signal. 但是,当我尝试'reboot'或'shutdown'命令时,它从未收到信号。 The init man page says: init手册页说:

"When init is requested to change the runlevel, it sends the warning signal SIGTERM to all processes that are undefined in the new runlevel. It then waits 5 seconds before forcibly terminating these processes via the SIGKILL signal. Note that init assumes that all these processes (and their descendants) remain in the same process group which init originally created for them. If any process changes its process group affiliation it will not receive these signals. Such processes need to be terminated separately." “当请求init更改运行级别时,它会将警告信号SIGTERM发送到新运行级别中未定义的所有进程。然后等待5秒,然后通过SIGKILL信号强制终止这些进程。请注意,init假定所有这些进程(及其后代)保留在init最初为它们创建的进程组中。如果任何进程更改其进程组从属关系,它将不会收到这些信号。这些进程需要单独终止。

How to tell init daemon to send SIGTERM to this program? 如何告诉init守护进程将SIGTERM发送到该程序? My guess is I should set process group id to something init knows, but how can I do that? 我的猜测是我应该将进程组ID设置为init知道的东西,但是我该怎么做呢?

If your program is not started by the init system, it won't be managed by the init system. 如果您的程序未由init系统启动,则它不会由init系统管理。 Launch it from an init.d script to get the benefits described. 从init.d脚本启动它以获得所描述的好处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么程序没有崩溃? - Why didn't the program crash? 收到SIGTERM - receive SIGTERM 为什么我的程序fork函数没有按预期运行 - why my program fork function didn't run as expected C 程序 - 进程在发送 SIGTERM 信号后不调用 - C program - Process don't invoke after sending SIGTERM signal 为什么不打印? - Why it didn't print? 为什么客户端调用shutdown(sockfd,SHUT_RD)后,客户端程序中的recv()会收到发送给客户端的消息? - Why can recv() in the client program receive messages sent to the client after the client has invoked shutdown(sockfd, SHUT_RD)? 从C程序关闭Linux意味着作为init进程运行 - Shutdown Linux from C program meant to be ran as init process 为什么程序在此C编程或unix编程(execvp()系统调用)中不执行某些语句? - Why the program didn't execute some sentences in this C programming or unix programming(execvp() System calls)? 为什么程序在此C编程或unix编程(execvp()系统调用)中不执行某些语句? - Why the program didn't execute some sentences in this C programming or unix programming(execvp() System calls)? 谁能弄清楚为什么我的程序(用C语言)将句子拆分为单词不起作用? - Can anyone figure out why my program (in C) for splitting sentence to word didn't work?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM