3

linux Ubuntu通过systemd 添加开机自动启动程序方法

 2 years ago
source link: https://blog.p2hp.com/archives/8690
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

本文介绍如何在Linux 系统中自行建立一个web服务器,并设定让Systemd 自动启动与管理web服务器的运作。

建立Echo 服务器

首先以Python 写一个简单的echo 服务器,将其储存在/opt/echo_server.py

#!/usr/bin/env python3
import socket
# 建立socket
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# 绑定本机9000 连接端口
serv.bind(( ' 0.0.0.0 ' , 9000 ))
# 开始接受client 连接
serv.listen()
while True :
# 接受client 连接
conn, addr = serv.accept()
print ( ' Client from ' , addr)
while True :
# 接收信息
data = conn.recv( 1024 )
# 若无信息则离开
if not data: break
# 传送信息
conn.send(data)
conn.close()
print ( ' Client disconnected ' )
#!/usr/bin/env python3 
import socket

# 建立socket
serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# 绑定本机9000 连接端口
serv.bind(( ' 0.0.0.0 ' , 9000 ))

# 开始接受client 连接
serv.listen()

while  True :

    # 接受client 连接
    conn, addr = serv.accept()
    print ( ' Client from ' , addr)

    while  True :

        # 接收信息
        data = conn.recv( 1024 )

        # 若无信息则离开
        if  not data: break

        # 传送信息
        conn.send(data)

    conn.close()
    print ( ' Client disconnected ' )

这段Python 程序在执行之后,会监听本机的 9000 连接端口,建立连接之后将所有收到的信息返回client 端。

建立好 /opt/echo_server.py 之后,顺便开启执行权限:

# 开启执行权限
chmod +x /opt/echo_server.py
# 开启执行权限
chmod +x /opt/echo_server.py

我们可以先手动测试一下,启动echo 服务器:

# 启动echo 服务器(server 端)
/opt/echo_server.py
# 启动echo 服务器(server 端) 
/opt/echo_server.py

接着打开另外一个终端,使用 nc 命令连接 9000 端口:

# 连接localhost 的9000 端口(client 端)
nc localhost 9000
# 连接localhost 的9000 端口(client 端) 
nc localhost 9000

连上之后,随意输入一些文字,发出之后应该就会看到服务器回应相同的文字,而在服务器端应该也会显示一些关于client 的信息,这样就代表echo 服务器可以正常运行了。

建立Systemd 服务单元配置文件

建立一个新的Systemd 服务单元配置文件,储存于/etc/systemd/system/echo_server.service

[Unit]
Description= Echo Server
[Service]
Type= simple
ExecStart= /opt/echo_server.py
Restart= always
[Install]
WantedBy= multi-user.target
[Unit] 
Description= Echo Server

[Service] 
Type= simple
 ExecStart= /opt/echo_server.py
 Restart= always

[Install] 
WantedBy= multi-user.target

权限要设定为644

sudo chmod 644 /etc/systemd/system/echo_server.service

如果在开发过程中,有修改过Systemd 的服务单元配置文件,记得重新载入daemon 让新设置生效:

# 重新载入Systemd 配置文件
sudo systemctl daemon-reload
# 重新载入Systemd 配置文件 
sudo systemctl daemon-reload

接着就可以使用 systemctl 命令启动自定义的echo 服务器:

# 启动自定义的echo 服务器
sudo systemctl start echo_server
# 启动自定义的echo 服务器
sudo systemctl start echo_server

查看echo 服务器的状态:

# 查看echo 服务器状态
systemctl status echo_server
# 查看echo 服务器状态
systemctl status echo_server
echo_server.service - Echo Server
Loaded: loaded (/etc/systemd/system/echo_server.service; disabled; vendor pre
Active: active (running) since Thu 2019-09-19 06:59:46 UTC; 1min 46s ago
Main PID: 20142 (python3)
Tasks: 1 (limit: 4915)
CGroup: /system.slice/echo_server.service
└─20142 python3 /opt/echo_server.py
Sep 19 06:59:46 test-vm systemd[1]: Started Echo Server.
echo_server.service - Echo Server
   Loaded: loaded (/etc/systemd/system/echo_server.service; disabled; vendor pre
   Active: active (running) since Thu 2019-09-19 06:59:46 UTC; 1min 46s ago
 Main PID: 20142 (python3)
    Tasks: 1 (limit: 4915)
   CGroup: /system.slice/echo_server.service
           └─20142 python3 /opt/echo_server.py

Sep 19 06:59:46 test-vm systemd[1]: Started Echo Server.

这样就成功启动了自己开发的echo 服务器了,此时我们可以直接使用上面的 nc 命令测试一下服务器是否正常运行。

如果要停止echo 服务器,可执行:

# 停止echo 服务器
sudo systemctl stop echo_server
# 停止echo 服务器
sudo systemctl stop echo_server

如果想要让echo 服务器可以在开机时自动启动,可执行:

# 设置开机自动启动echo 服务器
sudo systemctl enable echo_server
# 设置开机自动启动echo 服务器
sudo systemctl enable echo_server
Created symlink /etc/systemd/system/multi-user.target.wants/echo_server.service → /etc/systemd/system/echo_server.service.

若要取消开机自动启动echo 服务器,则可执行:

# 取消开机自动启动echo 服务器
sudo systemctl disable echo_server
# 取消开机自动启动echo 服务器
sudo systemctl disable echo_server
Removed /etc/systemd/system/multi-user.target.wants/echo_server.service.

关于 systemctl 指令的详细用法,请参考Linux systemd 系统服务管理基础教学文章

Systemd 服务单元配置文件的完整文件可以从手册查询:

# 查询Systemd 服务单元配置文件说明文件
man systemd.unit
man systemd.service
man systemd.exec
# 查询Systemd 服务单元配置文件说明文件
man systemd.unit
man systemd.service
man systemd.exec

另外也可从/lib/systemd/system/(Ubuntu)或/usr/lib/systemd/system/(CentOS)目录中找到大量的实际示例。

Systemd 服务单元配置文件说明:

以下是一个比较详细的Systemd 服务单元配置文件示例,在编写自定义服务的配置文件时,可以先复制这份配置再接着修改:

[Unit]
# 服务名称
Description= Your Server
# 服务相关文件
# Documentation=https://example.com
# Documentation=man:<a class="wpal-linked-keyword" href="https://nginx.p2hp.com/" target="_blank">nginx</a>(8)
# 设定服务启动的先后相关姓,例如在网络启动之后:
# After=network.target
[Service]
# 进程类型
Type= simple
# 启动服务命令
ExecStart= /opt/your_command
# 服务进程PID(通常配合forking 的服务使用)
# PIDFile=/run/your_server.pid
# 启动服务前,执行的命令
# ExecStartPre=/opt/your_command
# 启动服务后,执行的命令
# ExecStartPost=/opt/your_command
# 停止服务命令
# ExecStop=/opt/your_command
# 停止服务后,执行的命令
# ExecStopPost=/opt/your_command
# 重新载入服务命令
# ExecReload=/opt/your_command
# 服务终止时自动重新启动
Restart= always
# 重新启动时间格时间(预设为100ms)
# RestartSec=3s
# 启动服务超时秒数
# TimeoutStartSec=3s
# 停止服务超时秒数
# TimeoutStopSec=3s
# 执行时的工作目录
# WorkingDirectory=/opt/your_folder
# 执行服务的用户(名称或ID 皆可)
# User=myuser
# 执行服务的群组(名称或ID 皆可)
# User=mygroup
# 环境变数设置
# Environment="VAR1=word1 word2" VAR2=word3 "VAR3=$word 5 6"
# 服务输出信息指向设定
# StandardOutput=syslog
# 服务错误信息息指向设定
# StandardError=syslog
# 设定服务在Syslog 中的名称
# SyslogIdentifier=your-server
[Install]
WantedBy= multi-user.target
[Unit] 
# 服务名称
Description= Your Server

# 服务相关文件
# Documentation=https://example.com 
# Documentation=man:nginx(8)

# 设定服务启动的先后相关姓,例如在网络启动之后:
# After=network.target

[Service] 
# 进程类型
Type= simple

# 启动服务命令
ExecStart= /opt/your_command

# 服务进程PID(通常配合forking 的服务使用)
# PIDFile=/run/your_server.pid

# 启动服务前,执行的命令
# ExecStartPre=/opt/your_command

# 启动服务后,执行的命令
# ExecStartPost=/opt/your_command

# 停止服务命令
# ExecStop=/opt/your_command

# 停止服务后,执行的命令
# ExecStopPost=/opt/your_command

# 重新载入服务命令
# ExecReload=/opt/your_command

# 服务终止时自动重新启动
Restart= always

# 重新启动时间格时间(预设为100ms)
# RestartSec=3s

# 启动服务超时秒数
# TimeoutStartSec=3s

# 停止服务超时秒数
# TimeoutStopSec=3s

# 执行时的工作目录
# WorkingDirectory=/opt/your_folder

# 执行服务的用户(名称或ID 皆可)
# User=myuser

# 执行服务的群组(名称或ID 皆可)
# User=mygroup

# 环境变数设置
# Environment="VAR1=word1 word2" VAR2=word3 "VAR3=$word 5 6"

# 服务输出信息指向设定
# StandardOutput=syslog

# 服务错误信息息指向设定
# StandardError=syslog

# 设定服务在Syslog 中的名称
# SyslogIdentifier=your-server

[Install] 
WantedBy= multi-user.target

参考资料:PubNubBenjamin MorelLinodeLinuxConfig.orgTecAdmin.net   dititalocea


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK