Run jupyterhub as a system service

Gregory Machon edited this page Aug 30, 2017 · 13 revisions

Ubuntu/Debian

Save https://gist.github.com/lambdalisue/f01c5a65e81100356379 as /etc/init.d/jupyterhub and follow

$ sudo chmod +x /etc/init.d/jupyterhub
# Create a default config to /etc/jupyterhub/jupyterhub_config.py
$ sudo jupyterhub --generate-config -f /etc/jupyterhub/jupyterhub_config.py
# Start jupyterhub
$ sudo service jupyterhub start
# Stop jupyterhub
$ sudo service jupyterhub stop
# Start jupyterhub on boot
$ sudo update-rc.d jupyterhub defaults
# Or use rcconf to manage services http://manpages.ubuntu.com/manpages/natty/man8/rcconf.8.html
$ sudo rcconf

CentOS/Fedora/Generic systemd

Assuming you're using /etc/jupyterhub for your configs, save this as /lib/systemd/system/jupyterhub.service:

[Unit]
Description=Jupyterhub

[Service]
User=jupyterhub
ExecStart=/usr/bin/jupyterhub --JupyterHub.spawner_class=sudospawner.SudoSpawner
WorkingDirectory=/etc/jupyterhub

[Install]
WantedBy=multi-user.target

NOTE: You may need to add After=network-online.target in the [Unit] section, otherwise enabling this as a service on boot will fail if networking hasn't loaded yet. Set the correct User with correct permissions to run /usr/bin/jupyterhub.

Now run: sudo systemctl daemon-reload. That's it! Now you can use sudo systemctl <start|stop|status> jupyterhub.

NOTE: Make sure to install sudospawner.SudoSpawner by running: pip3 install git+https://github.com/jupyter/sudospawner

Ubuntu/Debian Anaconda3 with systemd

Similar to the above but with a couple changes. Assuming you're using /opt/anaconda3/jupyterhub for your configs, save this as /etc/systemd/system/jupyterhub.service

[Unit]
Description=Jupyterhub
After=syslog.target network.target

[Service]
User=root
Environment="PATH=/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/anaconda3/bin"
ExecStart=/opt/anaconda3/bin/jupyterhub -f /etc/jupyterhub/jupyterhub_config.py

[Install]
WantedBy=multi-user.target

If you want c.JupyterHub.cleanup_servers = False in the JupyterHub config to be respected by systemd, you need to add KillMode=process in the [Service] section. By default systemd kills all child processes on exit.

As above use sudo systemctl daemon-reload and sudo systemctl <start|stop|status> jupyterhub.

NOTE: This was for an anaconda3 install and so we added the anaconda3 to the path.

Other

You may want to check https://github.com/jupyter/jupyterhub/issues/317 or share your tips ;)