关机时向正在运行的程序/脚本发送什么信号?

Lin*_*eak 9 linux shutdown signals shell-script

现在我有以下几点:

# this function is meant for future script expansions
# its purpose is clear, i.e. to clean up some temp files
# now, it is doing nothing, just a special null command
cleanup_on_signal() { :; }

# define functions to handle signals
# treat them as errors with appropriate messages
# example calls:
#    kill -15   this_script_name    # POSIX, all shells compatible
#    kill -TERM this_script_name    # Bash and alike - newer shells
signal_handler_HUP()   {  cleanup_on_signal; print_error_and_exit "\\ntrap()" "Caught SIGHUP (1).\\n\\tClean-up finished.\\n\\tTerminating. Bye!";    }
signal_handler_INT()   {  cleanup_on_signal; print_error_and_exit "\\ntrap()" "Caught SIGINT (2).\\n\\tClean-up finished.\\n\\tTerminating. Bye!";    }
signal_handler_QUIT()  {  cleanup_on_signal; print_error_and_exit "\\ntrap()" "Caught SIGQUIT (3).\\n\\tClean-up finished.\\n\\tTerminating. Bye!";   }
signal_handler_ABRT()  {  cleanup_on_signal; print_error_and_exit "\\ntrap()" "Caught SIGABRT (6).\\n\\tClean-up finished.\\n\\tTerminating. Bye!";   }
signal_handler_TERM()  {  cleanup_on_signal; print_error_and_exit "\\ntrap()" "Caught SIGTERM (15).\\n\\tClean-up finished.\\n\\tTerminating. Bye!";  }

# use the above functions as signal handlers;
# note that the SIG* constants are undefined in POSIX,
# and numbers are to be used for the signals instead
trap 'signal_handler_HUP' 1; trap 'signal_handler_INT' 2; trap 'signal_handler_QUIT' 3; trap 'signal_handler_ABRT' 6; trap 'signal_handler_TERM' 15
Run Code Online (Sandbox Code Playgroud)

我希望脚本在关机时整齐地终止,现在它确实如此。

但是我打开了一个同事的建议,在 CTRL+C 上提出一个问题,而不是退出 shell。

我不想关闭机器,我不经常这样做,反正:

关机时向正在运行的程序/脚本发送什么信号?

Rui*_*iro 10

在关闭时,正在运行的进程首先被 init(根据@JdeBP 来自旧实现的 sendigs)/systemd 告知停止。

其余进程(如果有)将发送一个 SIGTERM。忽略 SIGTERM 或没有按时完成的那些,很快就会被 init/systemd 发送一个 SIGKILL。

这些操作旨在保证稳定/干净的关闭(尽可能)。

出于好奇,请参阅相关(旧)systemd错误的报告:

错误 1352264 - systemd 在关机期间在 SIGTERM 后立即发送 SIGKILL

systemd 在关闭期间在 SIGTERM 之后立即发送 SIGKILL,进程没有机会终止

同样来自shutdown.c/main():

    disable_coredumps();

    log_info("Sending SIGTERM to remaining processes...");
    broadcast_signal(SIGTERM, true, true, arg_timeout);

    log_info("Sending SIGKILL to remaining processes...");
    broadcast_signal(SIGKILL, true, false, arg_timeout);
Run Code Online (Sandbox Code Playgroud)

同样来自 sysvinit 2.94 sources/init.c,这里是 SIGTERM 回合的代码。如果向任何进程发送了 SIGTERM,则在 5 秒内每秒测试一次以查看是否还有任何进程剩余。如果在这些测试之一中没有找到进程,请保持等待,或者在 5 秒后向剩余进程发送 SIGKILL。

        switch(round) { 
                case 0: /* Send TERM signal */
                        if (talk)
                                initlog(L_CO,
                                        "Sending processes configured via /etc/inittab the TERM signal");
                        kill(-(ch->pid), SIGTERM);
                        foundOne = 1;
                        break;
                case 1: /* Send KILL signal and collect status */
                        if (talk)
                                initlog(L_CO,
                                        "Sending processes configured via /etc/inittab the KILL signal");
                        kill(-(ch->pid), SIGKILL);
                        break;
        }
        talk = 0;

    }
    /*
     *  See if we have to wait 5 seconds
     */
    if (foundOne && round == 0) {
        /*
         *      Yup, but check every second if we still have children.
         */
        for(f = 0; f < sleep_time; f++) {
                for(ch = family; ch; ch = ch->next) {
                        if (!(ch->flags & KILLME)) continue;
                        if ((ch->flags & RUNNING) && !(ch->flags & ZOMBIE))
                                break;
                }
                if (ch == NULL) {
                        /*
                         *      No running children, skip SIGKILL
                         */
                        round = 1;
                        foundOne = 0; /* Skip the sleep below. */
                        break;
                }
                do_sleep(1);
        }
    }
  }

  /*
   *    Now give all processes the chance to die and collect exit statuses.
   */
  if (foundOne) do_sleep(1)
  for(ch = family; ch; ch = ch->next)
        if (ch->flags & KILLME) {
                if (!(ch->flags & ZOMBIE))
                    initlog(L_CO, "Pid %d [id %s] seems to hang", ch->pid,
                                ch->id);
                else {
                    INITDBG(L_VB, "Updating utmp for pid %d [id %s]",
                                ch->pid, ch->id);
                    ch->flags &= ~RUNNING;
                    if (ch->process[0] != '+')
                        write_utmp_wtmp("", ch->id, ch->pid, DEAD_PROCESS, NULL);
                }
        }

  /*
   *    Both rounds done; clean up the list.
   */
Run Code Online (Sandbox Code Playgroud)