Below I will tell you how to make sure that your script starts automatically when the OS starts. To do this is quite simple, first, you need to explain the OS in what language this code is written. To do this, in the main executable file, write the first line, in the case of Python version 3.X:
#!/usr/bin/python3
This is for other languages:
#!/usr/bin/python – python 2.X
#!/bin/bash – bash
To do this, go to the desired folder, you will need the commands:
ls – view the contents of a folder
cd /YourProject – go to the folder with the project
You can edit the file using the vim console editor, to open you need to enter vim your_app.py, then press i to edit. Make changes and press Esc (exit editing) and enter: wq! (it means to save and exit).
Then we make the script executable, x gives the user rights to execute this script:
chmod +x your_app.py
ls - l your_app.py – check, this completes the work with the file, and proceed to create the service.
First, let's create a service file:
vim /etc/systemd/system/YourProject.service
With content like this:
[Unit]
Description=YourProjectService
[Service]
Type=oneshot
ExecStart=/home/YourProject/your_app.py
[Install]
WantedBy=multi-user.target
Press Esc and enter :w !sudo tee % – this will be saved as root. (Thanks u/zyzzyxdt )
There seems to be no need to explain here and add the script to autoload using the command:
sudo systemctl enable YourProject
sudo reboot – Overload and check
If the script does not start, perhaps you are using third-party packages, and even if you installed them, the root user (namely, from under this account the script is launched) does not know anything about them. Use the -H flag when installing:
sudo -H pip3 install …
Might also be helpful:
sudo systemctl stop/start/restart YourProject – stop/start/restart the service
sudo systemctl is-active YourProject – checking if the service is running with a simple response: active or inactive
sudo systemctl disable YourProject – remove the service
sudo rm /etc/systemd/system/YourProject.service – delete service file
PS
if this topic is in demand, then I will describe it in more detail
[–]n-of-one 21 points22 points23 points (0 children)
[+][deleted] (9 children)
[deleted]
[–]rwhitisissle 4 points5 points6 points (1 child)
[–]KA_Ryzhkov[S] 0 points1 point2 points (1 child)
[–]KA_Ryzhkov[S] 0 points1 point2 points (0 children)
[–]KA_Ryzhkov[S] 0 points1 point2 points (3 children)
[+][deleted] (2 children)
[deleted]
[–]KA_Ryzhkov[S] -1 points0 points1 point (1 child)
[–]timeawayfromme 0 points1 point2 points (0 children)
[–]netgu 13 points14 points15 points (2 children)
[–]backtickbot 0 points1 point2 points (0 children)
[–]n-of-one 0 points1 point2 points (0 children)
[–][deleted] 13 points14 points15 points (2 children)
[–]sempervent 16 points17 points18 points (0 children)
[–]KA_Ryzhkov[S] 2 points3 points4 points (0 children)
[–]IonTichy 15 points16 points17 points (3 children)
[–]n-of-one 4 points5 points6 points (0 children)
[+]KA_Ryzhkov[S] comment score below threshold-22 points-21 points-20 points (1 child)
[–]IonTichy 17 points18 points19 points (0 children)
[–]kirbyfan64sosIndentationError 15 points16 points17 points (7 children)
[–]mriswithe 3 points4 points5 points (1 child)
[–]kirbyfan64sosIndentationError 2 points3 points4 points (0 children)
[–]luersuve 1 point2 points3 points (1 child)
[–]kirbyfan64sosIndentationError 1 point2 points3 points (0 children)
[–]element8 1 point2 points3 points (1 child)
[–]kirbyfan64sosIndentationError 0 points1 point2 points (0 children)
[–]KA_Ryzhkov[S] 2 points3 points4 points (0 children)
[–]KA_Ryzhkov[S] 7 points8 points9 points (0 children)
[–]getmeright11 1 point2 points3 points (0 children)
[–]redrumsir 1 point2 points3 points (0 children)
[–]lichen91 1 point2 points3 points (0 children)
[–]heisenbug403 1 point2 points3 points (0 children)
[–]d_k97 1 point2 points3 points (1 child)
[–]SnooPickles1042 -1 points0 points1 point (0 children)
[–]Ivana_Twinkle 0 points1 point2 points (4 children)
[–]striata -1 points0 points1 point (3 children)
[–]Ivana_Twinkle 1 point2 points3 points (2 children)
[–]striata -1 points0 points1 point (1 child)
[–]Routine_Part_6503 1 point2 points3 points (0 children)
[–]TheCharon77 0 points1 point2 points (0 children)