Skip to content

Instantly share code, notes, and snippets.

@wjladams
Last active June 3, 2023 13:22
Show Gist options
  • Save wjladams/2ff01829df039fe03e271b061dac4166 to your computer and use it in GitHub Desktop.
Save wjladams/2ff01829df039fe03e271b061dac4166 to your computer and use it in GitHub Desktop.
Running Jupyter as service on ubuntu with systemd

How to use

  1. Copy the jupyter.service file to /etc/systemd/system/jupyter.service
  2. Edit that file to change the home directory
  3. Setup a password on jupyter by running jupyter notebook password
  4. Run systemctl daemon-reload to notify systemd about the new service
  5. Run systemctl enable jupyter.service to enable it, so it will start every time
  6. Run systemctl start jupyter.service to start it the first time. After that, because of the WantedBy=graphical.target the service will be run on login

On SELinux systems (including fedora) you need to allow the system to execute things in the anaconda directory. To do this you need to run (replacing /home/wjadams with your home directory)

sudo semanage fcontext -a -t bin_t '/home/wjadams/anaconda3/bin.*'
sudo chcon -Rv -u system_u -t bin_t '/home/wjadams/anaconda3/bin'
sudo restorecon -R -v '/home/wjadams/anaconda3/bin'

And then the system should allow you to run jupyter-notebook from that directory

[Unit]
Description=Jupyter Server Setup
After=syslog.target network.target
# Comment out the following on Fedora/RedHat/Centos, for some reason
# it doesn't have the syslog.socket, but removing the "Requires" doesn't effect anything
Requires=syslog.socket
[Service]
User=wjadams
Type=simple
WorkingDirectory=/home/wjadams
ExecStart=/home/wjadams/anaconda3/bin/jupyter-notebook --no-browser --NotebookApp.token=
[Install]
WantedBy=graphical.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment