I'm trying to run a bash script on startup as a systemd service, I'm doing this on a Raspberry Pi 4 with Raspbian Buster Lite.
I'm able to execute the bash script if I run it manually ./hls.sh
and I am able to also run the service if I do sudo service tv start
but the tv.service does not appear to be able to execute the bash script hls.sh
on start. I did give permissions chmod 777
for both the service and the bash file as well.
Any help here would be appreciated, I've been trying to figure this out on and off for a month now.
Edit:
Using the suggestions by Carl, I modified the files. However, it still doesn't work. I noticed also that when Type=oneshot you can't do a Restart=always, and if I do a restorecon -r
there's an error that says the command isn't found. Per Carl's suggestion I put everything in /opt I decided not to use the temp file suggestion because I need to have the ffmpeg output go to the same place every time (my understanding is that the other way would randomly generate a folder?).
Edit2: Issue was related to this systemd issue: https://unix.stackexchange.com/questions/209832/debian-systemd-network-online-target-not-working the workaround was simply to do RestartSec=5s under [Service]
Bash Script [Before]
#!/bin/bash
/usr/bin/ffmpeg -reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 2 -y -nostdin \
-hide_banner -loglevel fatal \
-i http://10.0.0.11:9981/stream/channelnumber/2 \
-vcodec copy -acodec copy -scodec copy -g 60 \
-fflags +genpts -user_agent HLS_delayer \
-metadata service_provider="TimeShift" \
-metadata service_name="TV 1" \
-f hls -hls_flags delete_segments \
-hls_time 60 \
-hls_list_size 480 \
-hls_wrap 481 \
-hls_segment_filename /home/pi/hls/1_%03d.ts /home/pi/hls/1_hls.m3u8
Bash Script [After]
#!/usr/bin/env bash
set -e -o pipefail # return exit code of command with non-zero exit code and stop executing
echo "The solution works!"
/usr/bin/ffmpeg -reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 2 -y -nostdin \
-hide_banner -loglevel fatal \
-i http://10.0.0.11:9981/stream/channelnumber/2 \
-vcodec copy -acodec copy -scodec copy -g 60 \
-fflags +genpts -user_agent HLS_delayer \
-metadata service_provider="TimeShift" \
-metadata service_name="JSTV 1" \
-f hls -hls_flags delete_segments \
-hls_time 60 \
-hls_list_size 480 \
-hls_wrap 481 \
-hls_segment_filename /opt/hls/tmp/1_%03d.ts /opt/hls/tmp/1_hls.m3u8
Service [Before]
[Unit]
Description=Timeshift TV
After=tvheadend.service
PartOf=tvheadend.service
Restart=always
[Service]
ExecStartPre=/bin/mkdir -p /home/pi/hls/
ExecStart=/home/pi/hls.sh 103 &
ExecStop=/bin/rm -rf /home/pi/hls
[Install]
WantedBy=default.target
Service [After]
[Unit]
Description=Timeshift TV
After=tvheadend.service
PartOf=tvheadend.service
[Service]
WorkingDirectory=/opt/hls
User=nobody
Type=oneshot
ExecStartPre=+/bin/mkdir -p -m777 /opt/hls/tmp
ExecStart=/opt/hls/hls.sh
ExecStopPost=+/bin/rm -rf /opt/hls/tmp
#Restart=always
[Install]
WantedBy=multi-user.target
After a reboot, checking the status via systemctl status [Before]
● tv.service - Timeshift TV
Loaded: loaded (/etc/systemd/system/tv.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2020-05-18 05:36:40 BST; 45s ago
Process: 465 ExecStartPre=/bin/mkdir -p -m777 /home/pi/hls/ (code=exited, status=0/SUCCESS)
Process: 472 ExecStart=/home/pi/hls.sh 103 & (code=exited, status=1/FAILURE)
Main PID: 472 (code=exited, status=1/FAILURE)
May 18 05:36:39 tv3 systemd[1]: Starting Timeshift TV...
May 18 05:36:39 tv3 systemd[1]: Started Timeshift TV.
May 18 05:36:40 tv3 systemd[1]: tv.service: Main process exited, code=exited, status=1/FAILURE
May 18 05:36:40 tv3 systemd[1]: tv.service: Failed with result 'exit-code'.
After a reboot, checking the status via systemctl status [After]
● tv.service - Timeshift tv
Loaded: loaded (/etc/systemd/system/tv.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Mon 2020-05-18 20:13:39 BST; 15min ago
Process: 464 ExecStartPre=/bin/mkdir -p -m777 /opt/hls/tmp (code=exited, status=0/SUCCESS)
Process: 471 ExecStart=/opt/hls/hls.sh (code=exited, status=1/FAILURE)
Process: 484 ExecStopPost=/bin/rm -rf /opt/hls/tmp (code=exited, status=0/SUCCESS)
Main PID: 471 (code=exited, status=1/FAILURE)
May 18 20:13:38 tv3 systemd[1]: Starting Timeshift tv...
May 18 20:13:38 tv3 hls.sh[471]: The solution works!
May 18 20:13:39 tv3 systemd[1]: tv.service: Main process exited, code=exited, status=1/FAILURE
May 18 20:13:39 tv3 systemd[1]: tv.service: Failed with result 'exit-code'.
May 18 20:13:39 tv3 systemd[1]: Failed to start Timeshift tv.
[After] starting the process manually still works
● tv2.service - Timeshift tv
Loaded: loaded (/etc/systemd/system/tv2.service; enabled; vendor preset: enabled)
Active: activating (start) since Mon 2020-05-18 20:53:13 BST; 17s ago
Process: 865 ExecStartPre=/bin/mkdir -p -m777 /opt/hls/tmp (code=exited, status=0/SUCCESS)
Main PID: 866 (bash)
Tasks: 2 (limit: 4915)
Memory: 15.3M
CGroup: /system.slice/tv2.service
├─866 bash /opt/hls/hls.sh
└─867 /usr/bin/ffmpeg -reconnect 1 -reconnect_at_eof 1 -reconnect_streamed 1 -reconnect_delay_max 2 -y -nostdin -hide_banner -logle
May 18 20:53:13 tv3 systemd[1]: Starting Timeshift tv...
May 18 20:53:13 tv3 hls.sh[866]: The solution works!
pi@tv3:/etc/systemd/system $ cd /opt/hls
pi@tv3:/opt/hls $ ls
hls.sh tmp
pi@tv3:/opt/hls $ cd tmp
pi@tv3:/opt/hls/tmp $ ls
1_000.ts