Skip to content

Making the server a systemd service

Alexander Borzunov edited this page Sep 19, 2023 · 8 revisions

Systemd is a service management system available in most modern Linux distributions. If you create a Petals service inside systemd, it will allow you to:

  • Start, stop, and restart the server using a single command
  • Make the server start automatically after reboot
  • Save all logs from the server

Here are the instructions for making the Petals server a systemd service:

  1. Configure systemd to keep shared memory files when a user logs out.

    Open /etc/systemd/logind.conf, find the section [Login] and add the line RemoveIPC=no. Then, reboot your machine.

  2. Create the Petals service.

    Create the file /etc/systemd/system/petals.service with the following contents:

    [Unit]
    Description=Petals
    After=network.target
    StartLimitIntervalSec=0
    
    [Service]
    Type=simple
    Restart=always
    RestartSec=1
    User=user
    Environment=PYTORCH_CUDA_ALLOC_CONF=max_split_size_mb:128
    Environment=CUDA_VISIBLE_DEVICES=0
    ExecStart=python -m petals.cli.run_server petals-team/StableBeluga2
    
    [Install]
    WantedBy=multi-user.target

    Here, replace python with the path of the Python 3.7+ interpreter you'd like to use to run Petals. This should be the interpreter where you have previously installed Petals with the python -m pip install petals command. Next, replace user from the line User=user with the name of a Linux user you'd like to use for running Petals. You can use your own user or create a separate one specifically for Petals to isolate the server from your personal files.

  3. Run this to start the server and check its status:

    sudo systemctl start petals
    sudo systemctl status petals
  4. You can check the Petals logs at any time by running:

    sudo journalctl -u petals -f
  5. If everything goes fine, you can make Petals to be started after reboot:

    sudo systemctl enable petals
  6. If you'd ever need to pause Petals and use your GPU for something else, run this:

    sudo systemctl stop petals
    # The GPU is free, do whatever you want...
    
    # Once you're done
    sudo systemctl start petals