0

I wonder if there's some way to have both the functionality of a Path unit and a Timer. The idea is to start a specific service if a path on the system is found but only between 9AM and 6PM.

Currently I've the following path / service that will start ffmpeg every time a USB camera is available at /dev/video4.

camera.path:

[Unit]
Description=USB Camera Stream Service Path

[Path]
PathExists=/dev/video4
Unit=camera.service

[Install]
WantedBy=multi-user.target

camera.service:

[Unit]
Description=USB Camera Stream Service

[Service]
Type=simple
User=root
Group=root

ExecStart=/usr/bin/ffmpeg -f v4l2 -input_format h264 -video_size 1920x1080 -i /dev/video4 -copyinkf -codec copy -f flv rtmp://127.0.0.1/live/stream

Note: I also want to be able to run systemctl start camera.service to start the service manually anytime I would like to.

Thank you.

1 Answer 1

2

The easiest way would probably be to have systemd execute a script that tests the current time and executes your command if it is in the range specified.

Create the script as, for example, /usr/local/bin/myscript:

#!/bin/sh
if [ $(date +%H) -ge 9 -a $(date +%H) -le 18 ]; then
    ffmpeg -f v4l2 -input_format h264 -video_size 1920x1080 -i /dev/video4 -copyinkf -codec copy -f flv rtmp://127.0.0.1/live/stream
fi

Be sure to give the script execute permissions:

sudo chmod +x /usr/local/bin/myscript

Then change this line in camera.service:

ExecStart=/usr/bin/ffmpeg -f v4l2 -input_format h264 -video_size 1920x1080 -i /dev/video4 -copyinkf -codec copy -f flv rtmp://127.0.0.1/live/stream

To this:

ExecStart=/usr/local/bin/myscript
1
  • Well, that's an option. Thank you. :)
    – TCB13
    Mar 3, 2022 at 1:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .