向 systemd 发送特定信号以关闭服务

pgc*_*ahy 7 systemd

我有一个远程服务器,并通过浏览器连接到那里托管的Jupyter笔记本。jupyter 服务通过 运行systemd。问题是 jupyter 期望两个 ctrl-c命令在 5 秒内完全关闭。systemd只发送一个信号来停止进程,然后等待超时,当它看到 jupyter 没有停止时,最后发送一个终止信号。当我想停止或重新启动服务时,这会导致长时间的延迟和不干净的退出。我知道 systemd 有一个ExecStop参数,但找不到任何有关如何实际使用它的示例,以及如何ctrl-c通过此机制发送相当于两次击键的示例。

我当前的服务文件是:

[Unit]
    Description=Jupyter notebook

[Service]
    Type=simple
    PIDFile=/var/run/jupyter-notebook.pid
    ExecStart=/home/linuxbrew/.linuxbrew/bin/jupyter notebook --no-browser
    User=pgcudahy
    Group=pgcudahy
    WorkingDirectory=/home/pgcudahy
    Environment=PATH=/home/linuxbrew/.linuxbrew/opt/python/libexec/bin:/home/linuxbrew/.linuxbrew/opt/cython/bin:/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/home/pgcudahy/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

[Install]
    WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)

pgc*_*ahy 6

因此,通过更多的研究,我想发送的ctrl-cSIGINT可以用/bin/kill -s SIGINT

将其添加到我的服务文件中可以彻底关闭 jupyter

ExecStop=/bin/kill -s SIGINT -$MAINPID & /bin/kill -s SIGINT -$MAINPID

整个文件是

[Unit]
    Description=Jupyter notebook

[Service]
    Type=simple
    PIDFile=/var/run/jupyter-notebook.pid
    ExecStart=/home/linuxbrew/.linuxbrew/bin/jupyter notebook --no-browser
    ExecStop=/bin/kill -s SIGINT -$MAINPID & /bin/kill -s SIGINT -$MAINPID
    User=pgcudahy
    Group=pgcudahy
    WorkingDirectory=/home/pgcudahy
    Environment=PATH=/home/linuxbrew/.linuxbrew/opt/python/libexec/bin:/home/linuxbrew/.linuxbrew/opt/cython/bin:/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/home/pgcudahy/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

[Install]
    WantedBy=multi-user.target
Run Code Online (Sandbox Code Playgroud)