Skip to content

Service Configuration

Up

NOTE: This installation step is included in the automatic installation

When the Flask server is started in a SSH session as described in Installation Step 11, it will terminate with the SSH session.

Instead, you may want the server to start up independently from any user sessions, restart after a failure and automatically start up when the device is powered up.

In order to achieve this, the Flask server start can be configured as service under control of systemd.

The following procedure is for the case where audio recording with video is not required. Otherwise, see next section.

  1. Open a SSH session on the Raspberry Pi
  2. Copy the service template raspiCamSrv.service which is provided with raspiCamSrv to your home directory
    cp ~/prg/raspi-cam-srv/config/raspiCamSrv.service ~
  3. Adjust the service configuration:
    nano ~/raspiCamSrv.service
    Replace all (4) occurrences of '<user>' with the user ID, specified during System Setup
    If you need a port different from 5000 (see RaspiCamSrv Installation, step 11), replace also port 5000 by your port.
  4. Stage the service configuration file to systemd:
    sudo cp ~/raspiCamSrv.service /etc/systemd/system
  5. Start the service:
    sudo systemctl start raspiCamSrv.service
  6. Check that the Flask server has started as service:
    sudo journalctl -ef
  7. Enable the service so that it automatically starts with system boot:
    sudo systemctl enable raspiCamSrv.service
  8. Reboot the system to test automatic server start:
    sudo reboot

Service Configuration for Audio Support

If it is intended to record audio along with videos, a slightly different setup is required (see Settings):
Instead of installing the service as a system unit, it needs to be installed as user unit (see systemd/User) in order to get access to PulseAudio.

Trixie and Bookworm Systems

If your system is a trixie or a bookworm system (lsb_release -a) follow these steps:

  1. Open a SSH session on the Raspberry Pi
  2. Copy the service template raspiCamSrv.service which is provided with raspiCamSrv to your home directory
    cp ~/prg/raspi-cam-srv/config/raspiCamSrv.service ~
  3. Adjust the service configuration:
    nano ~/raspiCamSrv.service
    Replace all (4) occurrences of '<user>' with the user ID, specified during System Setup
    If necessary, raplace also the standard port 5000 with your port.
    Remove the entry User=<user> from the [System] section
    In section [Install], change WantedBy=multi-user.target to WantedBy=default.target
  4. Create the directory for systemd user units
    mkdir -p ~/.config/systemd/user
  5. Stage the service configuration file to systemd for user units:
    cp ~/raspiCamSrv.service ~/.config/systemd/user
  6. Start the service:
    systemctl --user start raspiCamSrv.service
  7. Check that the Flask server has started as service:
    journalctl --user -ef
    If you get No journal files were found., try
    sudo journalctl -ef
  8. Enable the service so that it automatically starts with a session for the active user:
    systemctl --user enable raspiCamSrv.service
  9. Enable lingering in order to start the unit right after boot and keep it running independently from a user session
    loginctl enable-linger
  10. Reboot the system to test automatic server start:
    sudo reboot

Bullseye Systems

If your system is a bullseye system (lsb_release -a), which is currently still the case for Pi Zero, follow these steps:

  1. Open a SSH session on the Raspberry Pi
  2. Clone branch 0_3_12_next of Picamera2 repository
    cd ~/prg
    git clone -b 0_3_12_next https://github.com/raspberrypi/picamera2
  3. Copy the service template raspiCamSrv.service which is provided with raspiCamSrv to your home directory
    cp ~/prg/raspi-cam-srv/config/raspiCamSrv.service ~
  4. Adjust the service configuration:
    nano ~/raspiCamSrv.service
    - Replace '<user>' with the user ID, specified during System Setup
    - If necessary, raplace also the standard port 5000 with your port.
    - Add another Environment entry: Environment="PYTHONPATH=/home/<user>/prg/picamera2"
    - Remove the entry User=<user> from the [System] section
    - In section [Install], change WantedBy=multi-user.target to WantedBy=default.target
    For an example of the final .service file, see below
  5. Create the directory for systemd user units
    mkdir -p ~/.config/systemd/user
  6. Stage the service configuration file to systemd for user units:
    cp ~/raspiCamSrv.service ~/.config/systemd/user
  7. Start the service:
    systemctl --user start raspiCamSrv.service
  8. Check that the Flask server has started as service:
    journalctl --user -e
  9. Enable the service so that it automatically starts with a session for the active user:
    systemctl --user enable raspiCamSrv.service
  10. Enable lingering in order to start the unit right after boot and keep it running independently from a user session
    loginctl enable-linger
  11. Reboot the system to test automatic server start:
    sudo reboot

Example Service Configuration

Below is an example .service specification for user "sn":

[Unit]
Description=raspiCamSrv
After=network.target

[Service]
ExecStart=/home/sn/prg/raspi-cam-srv/.venv/bin/flask --app raspiCamSrv run --port 5000 --host=0.0.0.0
Environment="PATH=/home/sn/prg/raspi-cam-srv/.venv/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
Environment="PYTHONPATH=/home/sn/prg/picamera2"
WorkingDirectory=/home/sn/prg/raspi-cam-srv
StandardOutput=inherit
StandardError=inherit
Restart=always

[Install]
WantedBy=default.target