Skip to content

Creating a systemd startup script

Nicholas Saraniti edited this page Mar 10, 2024 · 10 revisions

If you're running the MLB LED Scoreboard on a Raspberry Pi, Raspian is probably your OS of choice. If this is the case, you can create a systemd service script to automatically start the scoreboard when you boot the pi and automatically restart it if it happens to crash.

To get started, you'll need to create a new file. Let's call it mlb-led-scoreboard.service for consistency.

Depending on your software version, its contents should be:

v7.x.x and lower (Expand)
[Unit]
Description=MLB LED Scoreboard
Wants=network-online.target time-sync.target
After=network.target network-online.target time-sync.target
ExecStartPre=/bin/sleep/15

[Service]
Environment="SCOREBOARD_ARGS=--led-brightness=40 --led-slowdown-gpio=2 --led-gpio-mapping=adafruit-hat"
WorkingDirectory=/home/pi/mlb-led-scoreboard
ExecStart=/usr/bin/python3 main.py $SCOREBOARD_ARGS
Restart=always
RestartSec=2

[Install]
WantedBy=multi-user.target
v8.0.0 and higher (Expand)
[Unit]
Description=MLB LED Scoreboard
Wants=network-online.target time-sync.target
After=network.target network-online.target time-sync.target
ExecStartPre=/bin/sleep/15

[Service]
Environment="SCOREBOARD_ARGS=--led-brightness=40 --led-slowdown-gpio=2 --led-gpio-mapping=adafruit-hat"
WorkingDirectory=/home/pi/mlb-led-scoreboard
ExecStart=/home/pi/mlb-led-scoreboard/main.py $SCOREBOARD_ARGS
Restart=always
RestartSec=2

[Install]
WantedBy=multi-user.target

It should be obvious what you can edit to make sure this works with your setup. SCOREBOARD_ARGS= should be set to the command line arguments you would normally pass to configure your LED matrix and WorkingDirectory needs to be set to the directory where you checked out the mlb-led-scoreboard repo.

Time to move this file to the right place!

$  sudo mv mlb-led-scoreboard.service /lib/systemd/system/

We're almost done! We can now enable the service so it starts up every time we boot up our Raspberry Pi.

$  sudo systemctl enable mlb-led-scoreboard.service

Your Raspberry Pi is now set up to load the MLB LED Scoreboard script every time it boots. But we should make sure the script works first.

$  sudo systemctl start mlb-led-scoreboard

You should now see your scoreboard working! You can pretty easily start, stop and restart the scoreboard any time now.

$  sudo systemctl restart mlb-led-scoreboard
$  sudo systemctl stop mlb-led-scoreboard

It's beyond the scope of this wiki page to troubleshoot things if things don't work for you. If the service start command doesn't start your scoreboard. First, check all of the paths and arguments in the service script to make sure they are the ones you would normally use while starting your script from the command line. Next, use the following command to get a glimpse of the last outputted message from the service script. It should give you a pretty good idea of what's going wrong. Any time you make modifications to the service file, you'll need to run sudo systemctl enable mlb-led-scoreboard.service again to update the changes.

$  sudo systemctl status mlb-led-scoreboard

One last command you can use if you're having issues with the scoreboard to view a full log of what the scoreboard has been printing in the background is as follows. You can change the --since attribute to whatever time you wish to look at.

$ sudo journalctl -u mlb-led-scoreboard --since "5 minutes ago"

Now, just reboot your Raspberry Pi to make sure everything starts up on boot for you!

$  sudo reboot