Creating A Linux Service
This tutorial is for creating a custom Linux service that will automatically run on system startup.
This service will automatically run python server on system boot. To be used in development services. Please take note that this method is not suitable for production as it uses Python’s simple http server.
STEP 1: In your computer, create a file with a .service extension. Please be sure to hide the .service file. In my case, I saved the .service extension at etc/systemd/system
ex. sample.service
The content of the sample.service file looks like this
[Unit]
Description=Sample Service description goes here
After=network.target
[Service]
Type=simple
User=root
ExecStart= -/usr/bin/bash -c ‘cd /home/Documents/pythonproject; source env/bin/activate; python manage.py runserver’
TimeoutStart=0
[Install]
WantedBy=default.target
NOTES:
-/usr/bin/bash
this access the terminal
-c
shortcut for command
‘cd /home/Documents/pythonproject; source env/bin/activate; python manage.py runserver’
The sample command in running python project in terminal
;
equivalent to ‘Enter’ key in terminal
STEP 2: Enable the systemctl service unit
In your terminal type the following
systemctl daemon-reload
systemctl enable sample.service
systemctl start sample.service
systemctl reboot -i
The last command will reboot your computer
NOTES:
sample.service
denotes the name of the service file that we created in step1
STEP 3: Check the status of the created service
To check if the created script was running as expected. type the following in your terminal
systemctl status sample.service
A message will show if the service was properly loaded and will show errors as well
NOTE: Check your firewall
Check if the port you are hosting is available on firewall
firewall-cmd --zone=public --add-port=8000/tcp --permanent
Then remember to reload the firewall for changes to take effect.
firewall-cmd --reload













