tyrel.cloud


All posts tagged:


Systemd Service Creation

Updated by Tyrel on 2020-05-28

Summary

Need to know for any modern linux system administrator

Creating a Systemd Service for Jupyter Notebook and Jupyter Lab

Why systemd? Well systemd is pretty cool in the fact that we can create either system or user services that will start at bootup or login respectively and restart themselves if they fail. Before systemd services I was using a lot more cron jobs which can be cool, but can also be a pain to manage! Below I will show you how to create two simple services that will handle one of my juypyter notebook and lab environments.

1 - Create Service Files

pi@pi4b:~ $ sudo nano ~/jupyter-notebook.service

# Jupyter-Notebook Service File

[Unit]
Description=Jupyter Notebook
After=network.target

[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi
ExecStart=/home/pi/.local/bin/jupyter-notebook --port=8888
Restart=on-failure

[Install]
WantedBy=multi-user.target

pi@pi4b:~ $ sudo nano ~/jupyter-lab.service

# Jupyter-Lab Service File

[Unit]
Description=Jupyter Lab
After=network.target

[Service]
Type=simple
User=pi
WorkingDirectory=/home/pi
ExecStart=/home/pi/.local/bin/jupyter-lab --port=8889
Restart=on-failure

[Install]
WantedBy=multi-user.target

2 - Enable Service Files by Creating Symlinks

pi@pi4b:~ $ sudo systemctl enable ~/jupyter-notebook.service

Created symlink /etc/systemd/system/multi-user.target.wants/jupyter-notebook.service → /home/pi/jupyter-notebook.service.
Created symlink /etc/systemd/system/jupyter-notebook.service → /home/pi/jupyter-notebook.service.

pi@pi4b:~ $ sudo systemctl enable ~/jupyter-lab.service

Created symlink /etc/systemd/system/multi-user.target.wants/jupyter-lab.service → /home/pi/jupyter-lab.service.
Created symlink /etc/systemd/system/jupyter-lab.service → /home/pi/jupyter-lab.service.

3 - Tell Systemd to re-read service files

pi@pi4b:~ $ sudo systemctl daemon-reload


4 - Start the services

pi@pi4b:~ $ sudo systemctl start juptyer-notebook.service

pi@pi4b:~ $ sudo systemctl start juptyer-lab.service


5 - Verify status

pi@pi4b:~ $ sudo systemctl status jupyter-notebook.service

● jupyter-notebook.service - Jupyter Notebook
   Loaded: loaded (/home/pi/jupyter-notebook.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2020-05-28 02:24:24 EDT; 1h 12min ago
 Main PID: 498 (jupyter-noteboo)
    Tasks: 12 (limit: 4915)
   CGroup: /system.slice/jupyter-notebook.service
           ├─ 498 /usr/bin/python3 /home/pi/.local/bin/jupyter-notebook --port=8888
           └─1612 /usr/bin/python3 -m ipykernel_launcher -f /home/pi/.local/share/jupyter/runtime/kernel-392b8244-7f07-47e0-a0bc-2c34c999

pi@pi4b:~ $ sudo systemctl status jupyter-lab.service

● jupyter-lab.service - Jupyter Lab
   Loaded: loaded (/home/pi/jupyter-lab.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2020-05-28 02:24:24 EDT; 1h 4min ago
 Main PID: 500 (jupyter-lab)
    Tasks: 14 (limit: 4915)
   CGroup: /system.slice/jupyter-lab.service
           ├─ 500 /usr/bin/python3 /home/pi/.local/bin/jupyter-lab --port=8889
           └─1368 /usr/bin/python3 -m ipykernel_launcher -f /home/pi/.local/share/jupyter/runtime/kernel-e469053f-cc99-4426-ae79-3a772ae1

Service Set Up is now Complete!

Feel like I missed something? Let me know in the comments!