Service Configuration
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.
- Open a SSH session on the Raspberry Pi
- Copy the service template raspiCamSrv.service which is provided with raspiCamSrv to your home directory
cp ~/prg/raspi-cam-srv/config/raspiCamSrv.service ~ - 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 alsoport 5000by your port. - Stage the service configuration file to systemd:
sudo cp ~/raspiCamSrv.service /etc/systemd/system - Start the service:
sudo systemctl start raspiCamSrv.service - Check that the Flask server has started as service:
sudo journalctl -ef - Enable the service so that it automatically starts with system boot:
sudo systemctl enable raspiCamSrv.service - 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:
- Open a SSH session on the Raspberry Pi
- Copy the service template raspiCamSrv.service which is provided with raspiCamSrv to your home directory
cp ~/prg/raspi-cam-srv/config/raspiCamSrv.service ~ - 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], changeWantedBy=multi-user.targettoWantedBy=default.target - Create the directory for systemd user units
mkdir -p ~/.config/systemd/user - Stage the service configuration file to systemd for user units:
cp ~/raspiCamSrv.service ~/.config/systemd/user - Start the service:
systemctl --user start raspiCamSrv.service - Check that the Flask server has started as service:
journalctl --user -ef
If you getNo journal files were found., trysudo journalctl -ef - Enable the service so that it automatically starts with a session for the active user:
systemctl --user enable raspiCamSrv.service - Enable lingering in order to start the unit right after boot and keep it running independently from a user session
loginctl enable-linger - 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:
- Open a SSH session on the Raspberry Pi
- Clone branch 0_3_12_next of Picamera2 repository
cd ~/prggit clone -b 0_3_12_next https://github.com/raspberrypi/picamera2 - Copy the service template raspiCamSrv.service which is provided with raspiCamSrv to your home directory
cp ~/prg/raspi-cam-srv/config/raspiCamSrv.service ~ - 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], changeWantedBy=multi-user.targettoWantedBy=default.target
For an example of the final .service file, see below - Create the directory for systemd user units
mkdir -p ~/.config/systemd/user - Stage the service configuration file to systemd for user units:
cp ~/raspiCamSrv.service ~/.config/systemd/user - Start the service:
systemctl --user start raspiCamSrv.service - Check that the Flask server has started as service:
journalctl --user -e - Enable the service so that it automatically starts with a session for the active user:
systemctl --user enable raspiCamSrv.service - Enable lingering in order to start the unit right after boot and keep it running independently from a user session
loginctl enable-linger - 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
