DEV Community

medmor
medmor

Posted on • Updated on

Control rc car using raspberry pi (Part 3 : Run the server at startup)

Configure the server to run as a service at startup

To set up the server to run at startup using systemd, we need to create a unit file that we will name it carServer.service under /etc/systemd/system.

This file will contain the needed configuration informations to start our server at startup. We can configure our service like this:

[Unit]
Description=Rc Car Server Controller
Wants=network-online.target
After=network-online.target

[Service]
ExecStart=/usr/bin/python3 /usr/local/bin/carProjectServer/server.py
User=enakr
Restart=always
WorkingDirectory=/usr/local/bin/carProjectServer

[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Here you can see that the file contains multiple sections, and the content is self-explanatory. For example:

  • The ExecStart points to our python program that will be run when the service is started. You can change the path to match your workspace folder.
  • The After=network-online.target and Wants=network-online.target ensure that the service runs after the network is up.

Finally, we need to enable our service by running the command :
sudo systemctl enable rcCarServer.service.
Now, when you reboot the raspberry, the server should start.
You can check if the server is running with the command:
sudo systemctl status rcCarServer.service.

Top comments (0)