Skip to content

Commit ea41d32

Browse files
committed
Run actions non-blocking
Stops hanging service on shutdown. Also refactored for less code repetition.
1 parent fa6f9c6 commit ea41d32

1 file changed

Lines changed: 37 additions & 19 deletions

File tree

scripts/files/camera-streamer/camera-streamer-control

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,37 @@ fi
88
action=$1
99
single=$2
1010

11-
[ -n "$single" ] || systemctl $action "camera-streamer-libcamera.service"
11+
run-action() {
12+
unit=$1
13+
echo "Running $action for $unit..."
14+
systemctl $action --no-block "$unit"
15+
echo "... done."
16+
}
17+
18+
add-path-unit() {
19+
dev=$1
20+
name=$2
21+
22+
# Add runtime path unit file that starts the service when the device is plugged in
23+
path_dir="/run/systemd/system"
24+
if ! grep -s -q -F "PathExistsGlob=$dev" "$path_dir/camera-streamer-usb-$name.path"; then
25+
echo "Adding path unit for autolaunch of camera-streamer-usb@$name"
26+
cat > "$path_dir/camera-streamer-usb-$name.path" <<EOF
27+
[Unit]
28+
Description=camera-streamer $name autolaunch
29+
30+
[Path]
31+
PathExistsGlob=$dev
32+
Unit=camera-streamer-usb@$name.service
33+
EOF
34+
systemctl daemon-reload || true
35+
fi
36+
}
37+
38+
# start/stop/restart the libcamera service
39+
if [ -z "$single" ] || [[ "$single" == "libcamera" ]]; then
40+
run-action "camera-streamer-libcamera.service"
41+
fi
1242

1343
# iterate over all the camera confs in /boot/camera-streamer
1444
for conf in /boot/camera-streamer/usb-*.conf; do
@@ -25,24 +55,12 @@ for conf in /boot/camera-streamer/usb-*.conf; do
2555
[ -n "$DEVICE" ] || continue
2656

2757
if [[ "$action" == "start" ]] || [[ "$action" == "restart" ]]; then
28-
# Add runtime path unit file that starts the service when the device is plugged in
29-
path_dir="/run/systemd/system"
30-
if ! grep -q "PathExistsGlob=$DEVICE" "$path_dir/camera-streamer-usb-$name.path"; then
31-
echo "Adding path unit for autolaunch of camera-streamer-usb@$name"
32-
cat > "$path_dir/camera-streamer-usb-$name.path" <<EOF
33-
[Unit]
34-
Description=camera-streamer $name autolaunch
35-
36-
[Path]
37-
PathExistsGlob=$DEVICE
38-
Unit=camera-streamer-usb@$name.service
39-
EOF
40-
systemctl daemon-reload || true
41-
fi
58+
add-path-unit "$DEVICE" "$name"
4259
fi
4360

44-
# do the action
45-
echo "Running $action for camera-streamer-usb-$name.path..."
46-
systemctl $action "camera-streamer-usb-$name.path"
47-
echo "... done."
61+
# start/stop/restart the USB camera services
62+
run-action "camera-streamer-usb-$name.path"
63+
if systemctl is-active --quiet "camera-streamer-usb@$name.service" && ([[ "$action" == "stop" ]] || [[ "$action" == "restart" ]]); then
64+
run-action "camera-streamer-usb@$name.service"
65+
fi
4866
done

0 commit comments

Comments
 (0)