我有一个远程服务器并通过浏览器连接到朱皮特在那里托管的笔记本。 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
答案1
因此,通过更多的研究,我想发送的ctrl-c
是SIGINT
可以用/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
答案2
对我来说,这可以将以下代码添加到“some_systemd.service”文件中:
[Service]
ExecStart=/usr/local/sbin/DVR_Living_Room.sh
KillSignal=SIGINT
“SIGINT”是终止信号,解释为:“Ctrl + C”,它会干净退出。
我需要 FFMPEG 这个信号,因为在 FFMPEG 现场录制期间任何其他类型的退出都会损坏视频。 FFMPEG 仅接受“CTRL+C”作为有效退出,以干净地停止录制视频而不损坏视频。
此外,您还可以在这里找到一系列不同的终止信号: https://man7.org/linux/man-pages/man7/signal.7.html