20
loading...
This website collects cookies to deliver better user experience
systemd
is a software package included in linux operating systems, according to Lennart Poettering… He is the author of the package software.sytemctl
command, systemd services can be started (start) and stopped (stop), applications that have changed configuration files can be re-read and restarted (reload), the status of the process can be obtained (status), and its activation can be controlled after the system restarts ( enable/disable). For more information, see the manual page.$ man systemctl
Flask microframework
is a very easy-to-use framework software for creating simple APIs. To add it to your script file, it is sufficient to download the flask module with pip3
and include it on the page, and create an app object to create your routes. Then the script below can be used.import flask
app = flask.Flask(__name__)
app.config["DEBUG"] = True
@app.route('/', methods=['GET'])
def function_one():
return "<div style=\" display: flex;justify-content: center;align-items: center;height: 100%;border: 3px solid green; \" ><p>Hello sir, It seems to work properly.Here is<span style=\"font-size:50px; color:white; background-color:red\"> / </span></p> </div>"
@app.route('/users', methods=['GET'])
def function_two():
return "<div style=\" display: flex;justify-content: center;align-items: center;height: 100%;border: 3px solid green; \" ><p>Hello sir, It seems to work properly.Here is<span style=\"font-size:50px; color:white; background-color:green\"> /users</span> </p> </div>"
@app.route('/products', methods=['GET'])
def function_three():
return "<div style=\" display: flex;justify-content: center;align-items: center;height: 100%;border: 3px solid green; \" ><p>Hello sir, It seems to work properly.Here is<span style=\"font-size:50px; color:white; background-color:blue\"> /products </span></p> </div>"
app.run()
localhost:5000 port
. To change port or host, you can give new host and port with app.run(host='ip_addr',port='port' )
.sudo vi /lib/systemd/system/myFlaskApp.service
or Let's open it in nano
editor. This path may differ depending on the distro. To find out where other services are, e.g. for mariadb service,$ locate mariadb
updatedb
command and try again. services can also be found under /usr/lib/systemd/system/…
Or you can create such a directory under the /usr
directory./lib/systemd/system/myFlaskApp.service
=>[Unit]
Description=My flask API service
[Install]
WantedBy=multi-user.target
[Service]
Type=simple
User=your username
PermissionsStartOnly=true
ExecStart=/usr/bin/python3 /your/path/app.py
Restart=on-failure
TimeoutSec=600
Restart=on-failure
. This indicates that we only want it to restart when the exit code is not 0. We can also add Restart=always
and RestartSec=1
lines instead. It makes it reboot in any case.sudo vi /etc/init/myFlaskApp.config
.description "MyFlaskApp"
start on stopped rc RUNLEVEL=[2345]
respawn
exec python3 /your/path/app.py
$ sudo systemctl start myFlaskApp
active(running)
state with systemctl status
.deployment
options above in terms of both performance and manageability in the production environment.pid
as can be seen with sytemctl status
. If a modular API structure is created and the route operation and other operations are isolated from each other, this method may be useful in situations where no modifications to this file are required. In this way, we can call it “life hacking”. To use something as the meaning of the word to improve a lifestyle other than its purpose…systemd
service and control them with systemctl
.