春江暮客

春江暮客的个人学习分享网站

Manually Create Custom System Service on CentOS7

2019-11-17 Technology
Manually Create Custom System Service on CentOS7

During Linux system development, sometimes your program might crash or be stopped. To keep it running continuously, adding the program to the service list is a very good practice. Here is an explanation of how to add your own program to system services in Linux, taking CentOS7 as an example.

Steps:

  1. Build a simple Flask web program with Python
  2. Add it to system services
  3. Start the service and detailed explanation of parameters

Build a simple Flask web program with Python

This step can be any program you want to add to system services. The example here is a simple Flask web app that only outputs “Hello, World!”

yum install python-pip -y  
pip install --upgrade pip  
pip install flask  

Do not keep a long-running service under /root and then try to run it as a normal service user. A safer pattern is to put the app in a directory the service user can access, for example /opt/flask_app.

mkdir -p /opt/flask_app
cd /opt/flask_app

Then create a simple Flask program:

## Location /opt/flask_app/hello.py
from flask import Flask  
app = Flask(__name__)  

@app.route('/')  
def hello_world():  
    return 'Hello, World!'  

Test run:

export FLASK_APP=hello.py  
flask run  

You should see it running successfully:
flask_app

Test with wget:
wget_test

Add to system services

Create a systemd service file, for example named flask_app:

touch /etc/systemd/system/flask_app.service  

Write the following content into it:

[Unit]  
Description=flask_app service  
After=network.target  
StartLimitIntervalSec=0  

[Service]  
Type=simple  
Restart=always  
RestartSec=1  
User=www  
WorkingDirectory=/opt/flask_app
Environment=FLASK_APP=hello.py
ExecStart=/usr/local/bin/flask run --host=0.0.0.0 --port=5000

[Install]  
WantedBy=multi-user.target  

Using WorkingDirectory and Environment directly in the unit file is usually simpler than wrapping everything in a shell script.

Start service and explanation of parameters

Start the service:

systemctl daemon-reload
systemctl start flask_app  
systemctl enable flask_app
systemctl status flask_app
  • Description: system service description
  • After: runs after specified target, here after network is up
  • StartLimitIntervalSec: the time window systemd uses when applying start-rate limits
  • Restart: whether to restart after stopping
  • RestartSec: wait time before restarting after stopping
  • User: specify which user runs the program
  • WorkingDirectory: the directory the program runs from
  • Environment: environment variables passed to the process
  • ExecStart: the command to run the service program, full path required

Just copy the install section as is…

Summary:

This article covers a common task: turning your own program into a systemd service and restarting it automatically if it stops. In real deployments, avoid storing the app under /root if the service runs as a non-root user.

友情链接

其它