Skip to content

Creating a systemd startup script

Steve Ehrenberg edited this page Mar 6, 2018 · 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.

[Unit]
Description=MLB LED Scoreboard
Wants=network-online.target
After=network.target network-online.target

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

[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 service mlb-led-scoreboard start

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

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

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 service mlb-led-scoreboard status

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

$  sudo reboot