5

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

1 Answer 1

2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.