Making the server autostart and autonomous from your terminal

Making a Raspi server autostart

This is a tutorial for adding autostart functionality to a Raspi server. We assume that the server was installed following these instructions.

When you use your surveillance camera and the CAM-AI server for real-life applications, it is important to make sure that the system restarts completely after a reboot or a power outage. This can be achieved by making a Linux system service that starts the CAM-AI server. Here is how this works. Following this procedure, your CAM-AI server will run independently from your PC’s terminal.

Log into your Raspi as user cam_ai. If the name of your machine is different from cam-ai-raspi, replace this term with the real name or the IP address.

ssh cam_ai@cam-ai-raspi

First, you create a service definition file:

sudo nano /etc/systemd/system/c_server.service

Then you add the following content to that file:

[Unit]
Description=CAM-AI-Server
After=network-online.target mariadb.service
[Service]
User=cam_ai
KillMode=process
KillSignal=SIGTERM
WorkingDirectory=/home/cam_ai/bashes
ExecStart=/home/cam_ai/bashes/start-c_server-service.sh
[Install]
WantedBy=multi-user.target

You save the file and close Nano. After that, you create the directory and bash file:

mkdir ~/bashes

If the directory already exists, you will get an error message, never mind…

nano ~/bashes/start-c_server-service.sh

Then you add the following content to that file:

#!/bin/bash
mkdir -p /home/cam_ai/cam-ai/data/logs
sleep 30s
exec >> /home/cam_ai/cam-ai/data/logs/c_server.log 2>> /home/cam_ai/cam-ai/data/logs/c_server.err
cd /home/cam_ai/cam-ai
source env/bin/activate
python cam-ai-server.py manage.py runserver 0.0.0.0:8000 --noreload

Save the file and leave Nano. Then you make the new file executable and enable the newly created service:

sudo chmod +x ~/bashes/start-c_server-service.sh
sudo systemctl enable c_server

sudo reboot now

It’s done. Your Raspi will now autostart your server automatically.

Discuss this topic in our Forum

1 thought on “Making the server autostart and autonomous from your terminal”

  1. Pingback: Enabling HTTPS on your Raspi server - CAM-AI: Articial intelligence for cameras

Comments are closed.

Scroll to Top