-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsystem_daemon_setup
More file actions
61 lines (42 loc) · 1.83 KB
/
system_daemon_setup
File metadata and controls
61 lines (42 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# Setup a Script to Autorun using Linux Systemd
# (Step 1): Application - Write a sample application or service which is desired to run as a system daemon.
[For Example, A sample Python Flask application as below]
#!/usr/bin/python3
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, world!'
if __name__ == '__main__':
app.run(host='localhost', port=5000, debug=True)
Save the application/service as:
sudo nano /usr/bin/flask_app.py
# (Step 2): Service File - Create a service for system daemon.
The service file must have .service extension under /lib/systemd/system directory
sudo nano /lib/systemd/system/flaskapp.service
Add the below content to '/lib/systemd/system/flaskapp.service'.
[Unit]
Description=Flask Application
After=multi-user.target
Conflicts=getty@tty1.service
[Service]
Type=simple
ExecStart=/usr/bin/python3 /usr/bin/flask_app.py
StandardInput=tty-force
[Install]
WantedBy=multi-user.target
In [Service]: Change Application filename, path of the application file accordingly.
In [Unit]: Describe about the Application/Service
# (Step 3): Enable the Service - Reload the systemctl daemon to read newly added service
The system service has been added to the service. Reload the systemctl daemon to read newly added service.
This daemon need to be reloaded each time after making any changes in the .service file.
sudo systemctl daemon-reload
Enable & Start the service to start on system boot.
sudo systemctl enable flaskapp.service
sudo systemctl start flaskapp.service
# (Step 4): Check the Status of the system daemon
sudo systemctl status flaskapp.service
To Start/Stop/Restart the service:
sudo systemctl start flaskapp.service
sudo systemctl stop flaskapp.service
sudo systemctl restart flaskapp.service