Creating a service on Raspberry Pi to run on boot

Miller Sartin
2 min readNov 24, 2022

--

Photo by Jainath Ponnala on Unsplash

I am using my Minecraft server for the service I am creating. My goal is to make my Minecraft service run on boot, and for me to be able to start and stop it with commands

Their are multiple ways to make something run on boot with your Raspberry pi, you can use crontab to run things on boot or every x minutes. You can also use systemctl. In this tutorial will be showing you how to use systemctl.

First thing you are going to do is create a bash file that contains the commands you want to run. The command I use is below. This command runs the Minecraft server.

java -Xmx1024M -Xms1024M -jar server.jar nogui

First I will create the bash script to run my service. I am running a minecraft server so I must make sure I am in my /minecraft/ folder. Below are the commands I used to create my bash script. Your script can be named anything but must have a .sh extension, this indicates its a bash file.

touch start.sh
sudo nano start.sh
My bash script “start.sh”

On the first line, put “#!/bin/bash” you MUST do this for it to work. After that you are free to paste your commands in.

Next you will create your .service file, this must be in the same directory as your bash script. This file can have any name but must have a .service file extension.

touch minecraft.service
sudo nano minecraft.service

This is my .service file

vi /home/owner/minecraft/minecraft.service

[Unit]
Description=My lit bangin minecraft server
After=network-online.target

[Service]
ExecStart=/bin/bash /home/owner/minecraft/start.sh
WorkingDirectory=/home/owner/minecraft/
StandardOutput=inherit
StandardError=inherit
Restart=always
User=owner

[Install]
WantedBy=multi-user.target

You can edit this and adapt the code to the paths and file names your system uses.

Next copy it to /lib/systemd/system/

sudo cp /home/owner/minecraft/minecraft.service /lib/systemd/system/

Now you are all done, you can test your service by using this command:

sudo systemctl start minecraft.service

Make sure it is running correctly using the status command:

sudo systemctl status minecraft.service

Stop the service using the stop command:

sudo systemctl stop minecraft.service

Now that you have tested your service you can make it start on every reboot. Do this by using the enable command:

sudo systemctl enable minecaft.service

Hopefully this helped you. If you are stuck comment your issues below and I will do my best to help you.

--

--

Miller Sartin

Student focused on cyber security, Linux and programming. Passionate about open source things and privacy.