8

I have a remote server and connect via a browser to Jupyter notebooks hosted there. The jupyter service is run via systemd. The problem is that jupyter expects two ctrl-c commands within 5 seconds of each other to shut down cleanly. systemd sends only one signal to halt the process, then waits for a timeout, and when it sees that jupyter hasn't stopped, finally sends a kill signal. This leads to a long delay and an unclean exit when I want to stop or restart the service. I know that systemd has an ExecStop parameter but can't find any examples of how it is actually used, and how I can send the equivalent of two ctrl-c keystrokes via this mechanism.

My current service file is:

[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

2 Answers 2

7

So with some more research, what I want to send with ctrl-c is a SIGINT which can be done with /bin/kill -s SIGINT

Adding this to my service file shuts down jupyter cleanly

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

The whole file is

[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
4
5

For me this worked adding the following code to the "some_systemd.service" file:

[Service]
ExecStart=/usr/local/sbin/DVR_Living_Room.sh
KillSignal=SIGINT

"SIGINT" is the kill signal interpreted as: "Ctrl + C" which does a clean exit.

I needed this signal for FFMPEG because any other type of exit during a live recording from FFMPEG will corrupt the video. FFMPEG only accepts "CTRL+C" as a valid exit to cleanly stop recording the video and not corrupt it.

Also, you can find a complete slew of different kill signals here: https://man7.org/linux/man-pages/man7/signal.7.html

1
  • 1
    The question says ''how I can send the equivalent of two Ctrl-C keystrokes via this mechanism?''   I don't see an answer to that question. Aug 11, 2022 at 21:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .