systemctl stop与信号

11 篇文章 0 订阅

systemctl stop与kill的关系

如果一个systemd的服务文件如下

/lib/systemd/system/sysdemo.service

[Unit]
Description=Demo Service
After=syslog.target network.target

[Service]
Restart=always
RestartSec=60
WorkingDirectory=/usr/bin
ExecStart=/usr/bin/sysdemo
ExecStop=/bin/kill $MAINPID

[Install]
WantedBy=default.target

那么使用下面这个命令就可以停止sysdemo.service

sudo systemctl stop sysdemo.service

其实就是依据sysdemo.service文件定义,向/usr/bin/sysdemo运行起来的进程发送了kill -s SIGTERM信号(默认信号)

如果要使用core dump得这样,

LimitCORE=infinity
ExecStop=/bin/kill -s QUIT $MAINPID

程序响应kill信号

一般响应SIGINT与SIGTERM。

  • SIGINT 控制台执行ctrl+c 退出
  • SIGTERM 正常不带参数的kill命令信号
  • 如果需要core dump, 可以使用SIGQUIT
void sig_handler(int sig) {
    // SIGQUIT Ctrl+\,不同于SIGINT,这个是会产生core dump文件的。
    // 进程终止并且产生core文件
    if (sig == SIGINT) {
        // ctrl+c 退出时执行的代码
        std::cout << "ctrl+c pressed!" << std::endl;
        if (s_engine)
            s_engine->Stop();
    } else if (sig == SIGTERM) {
        // kill 退出时执行的代码
        std::cout << "got SIGTERM!" << std::endl;
        if (s_engine)
            s_engine->Stop();
    }
}

int main(int argc, char **argv) {

    struct sigaction sga;
    sigemptyset(&sga.sa_mask);
    sga.sa_flags = 0;
    sga.sa_handler = sig_handler;
    sigaction(SIGINT, &sga, NULL);
    sigaction(SIGTERM, &sga, NULL);
    ...
}

常见信号

查看man信号

$man 7 signal

常见信号如下,未列举完全

       Signal     Value     Action   Comment
       ──────────────────────────────────────────────────────────────────────
       SIGHUP        1       Term    Hangup detected on controlling terminal
                                     or death of controlling process
       SIGINT        2       Term    Interrupt from keyboard
       SIGQUIT       3       Core    Quit from keyboard
       SIGILL        4       Core    Illegal Instruction
       SIGABRT       6       Core    Abort signal from abort(3)
       SIGFPE        8       Core    Floating-point exception
       SIGKILL       9       Term    Kill signal
       SIGSEGV      11       Core    Invalid memory reference
       SIGPIPE      13       Term    Broken pipe: write to pipe with no
                                     readers; see pipe(7)
       SIGALRM      14       Term    Timer signal from alarm(2)
       SIGTERM      15       Term    Termination signal
       SIGUSR1   30,10,16    Term    User-defined signal 1
       SIGUSR2   31,12,17    Term    User-defined signal 2
       SIGCHLD   20,17,18    Ign     Child stopped or terminated
       SIGCONT   19,18,25    Cont    Continue if stopped
       SIGSTOP   17,19,23    Stop    Stop process
       SIGTSTP   18,20,24    Stop    Stop typed at terminal
       SIGTTIN   21,21,26    Stop    Terminal input for background process
       SIGTTOU   22,22,27    Stop    Terminal output for background process


  The entries in the "Action" column of the tables below specify the default disposition for each signal, as follows:

       Term   Default action is to terminate the process.

       Ign    Default action is to ignore the signal.

       Core   Default action is to terminate the process and dump core (see core(5)).

       Stop   Default action is to stop the process.

       Cont   Default action is to continue the process if it is currently stopped.

关于action一列,指明了该信号的动作。Term是终止进程,Core终止进程并产生core dump。

常用几种信号区别

  • SIGKILL:
    The signals SIGKILL and SIGSTOP cannot be caught, blocked, or ignored.
    SIGKILL是发送到处理的信号以使其立即终止。当发送到程序,SIGKILL使其立即终止。在对比SIGTERM和SIGINT,这个信号不能被捕获或忽略,并且在接收过程中不能执行任何清理在接收到该信号。
    这也是kill -9 强制杀死进程的原因。

  • SIGQUIT:
    SIGQUIT是其控制终端发送到进程,当用户请求的过程中执行核心转储的信号。 SIGQUIT通常可以ctrl+ \。在Linux上,人们还可以使用Ctrl-4或虚拟控制台,SysRq yek。

  • SIGTERM:
    SIGTERM是kill或killall命令发送到进程默认的信号。它会导致进程的终止,但是SIGKILL信号不同,它可以被捕获和解释(或忽略)的过程。因此,SIGTERM类似于问一个进程终止可好,让清理文件和关闭。

  • SIGINT:
    当用户希望中断该进程发送处理的信号。通常ctrl-C,但在某些系统上,“删除”字符或“break”键

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值