1

I have been playing around with it for a little while but am pretty stuck with creating a service to run a script. The script I am trying to run is as follows.

#!/bin/sh
while true

do

sudo bbb-record --rebuildall

sleep 300
done

I tried creating a daemon for this to run like:

[Unit]
Description=bbb-rebuild
[Service]
ExecStart=/home/sysadmin/rebuild
Restart=always

[Install]
WantedBy=multi-user.target 

exit=0 

I get the error message

==== AUTHENTICATION COMPLETE ===
Failed to start rebuild.service: Unit rebuild.service is not loaded properly: Invalid argument.
See system logs and 'systemctl status rebuild.service' for details.

after checking status
Apr 22 16:46:55 bbb2 systemd[1]: rebuild.service: Unit entered failed state.
Apr 22 16:46:55 bbb2 systemd[1]: rebuild.service: Failed with result 'exit-code'.
Apr 22 16:46:55 bbb2 systemd[1]: rebuild.service: Service hold-off time over, scheduling restart.
Apr 22 16:46:55 bbb2 systemd[1]: Stopped bbb-rebuild.
Apr 22 16:46:55 bbb2 systemd[1]: rebuild.service: Start request repeated too quickly.
Apr 22 16:46:55 bbb2 systemd[1]: Failed to start bbb-rebuild.
Apr 22 16:46:55 bbb2 systemd[1]: rebuild.service: Unit entered failed state.
Apr 22 16:46:55 bbb2 systemd[1]: rebuild.service: Failed with result 'start-limit-hit'.
Apr 22 16:47:22 bbb2 systemd[1]: [/etc/systemd/system/rebuild.service:12] Missing '='.
Kulfy
  • 17,696
  • Since the service as written will run this script as root, why sudo in the script? – user535733 Apr 22 '20 at 18:47
  • [/etc/systemd/system/rebuild.service:12] Missing '='. suggests a hidden characters or wrong encoding present in the file. I would suggest running dos2unix on the file. See this answer for information. – Raffa Apr 22 '20 at 19:39

1 Answers1

0

You should better use systemd-timers to achieve this.

Create a timer named like your systemd service rebuild.timer

# nano /etc/systemd/system/rebuild.timer

[Unit]
Description=Start script every hour

[Timer]
OnCalendar=hourly

[Install]
WantedBy=timers.target

Edit your script to the following

#!/bin/sh
bbb-record --rebuildall

Ensure your script is runable chmod +x /home/sysadmin/rebuild

Edit your systemd service to remove restart=always and exit 0

[Unit]
Description=bbb-rebuild

[Service]
ExecStart=/home/sysadmin/rebuild

[Install]
WantedBy=multi-user.target 

Then simply enable your systemd-timer with systemctl enable rebuild.timer.
Then check the timer is enabled with systemctl status rebuild.timer.

  • Why sudo instead of simply running the service as the correct user? – user535733 Apr 22 '20 at 18:34
  • You're right, it is better to specify a user to run the service with, if nothing is specified the script is run as root, so sudo is this case useless – Fractalyse Apr 22 '20 at 18:45