自学内容网 自学内容网

systemd systemctl命令

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

由于docker学习需要一些成本,我们这里直接使用Linux自带的service命令进行服务的部署。


编写service文件

为每个微服务创建一个 Systemd 服务文件,并位于 /etc/systemd/system/ 目录下。
Description: 对该服务的简短描述。
After: 指定该服务在 network.target 之后启动,表示网络服务已经可用。
ExecStart: 指定启动服务时执行的命令及其路径。
WorkingDirectory: 指定服务运行时的工作目录。
Restart: 设定重启策略,on-failure 表示当服务因错误退出时自动重启。
WantedBy=multi-user.target 意味着当系统进入多用户模式(即一般用户可以登录的状态)时,这个服务将被启动

[Unit]
Description=Speech Service
After=network.target

[Service]
ExecStart=/home/lkm/project/chatsystem/code/server/build/speech/speech_server
WorkingDirectory=/home/lkm/project/chatsystem/code/server/build/speech/
Restart=on-failure

[Install]
WantedBy=multi-user.target


[Unit]
Description=File Service
After=network.target

[Service]
ExecStart=/home/lkm/project/chatsystem/code/server/build/file/file_server
WorkingDirectory=/home/lkm/project/chatsystem/code/server/build/file/
Restart=on-failure

[Install]
WantedBy=multi-user.target
[Unit]
Description=User Service
After=network.target

[Service]
ExecStart=/home/lkm/project/chatsystem/code/server/build/user/user_server
WorkingDirectory=/home/lkm/project/chatsystem/code/server/build/user/
Restart=on-failure

[Install]
WantedBy=multi-user.target


[Unit]
Description=Transmite Service
After=network.target

[Service]
ExecStart=/home/lkm/project/chatsystem/code/server/build/transmite/transmite_server
WorkingDirectory=/home/lkm/project/chatsystem/code/server/build/transmite/
Restart=on-failure

[Install]
WantedBy=multi-user.target


[Unit]
Description=Message Service
After=network.target

[Service]
ExecStart=/home/lkm/project/chatsystem/code/server/build/message/message_server
WorkingDirectory=/home/lkm/project/chatsystem/code/server/build/message/
Restart=on-failure

[Install]
WantedBy=multi-user.target


[Unit]
Description=Friend Service
After=network.target

[Service]
ExecStart=/home/lkm/project/chatsystem/code/server/build/friend/friend_server
WorkingDirectory=/home/lkm/project/chatsystem/code/server/build/friend/
Restart=on-failure

[Install]
WantedBy=multi-user.target
[Unit]
Description=Gateway Service
After=network.target

[Service]
ExecStart=/home/lkm/project/chatsystem/code/server/build/gateway/gateway_server
WorkingDirectory=/home/lkm/project/chatsystem/code/server/build/gateway/
Restart=on-failure

[Install]
WantedBy=multi-user.target


启动服务

sudo systemctl start speech.service
sudo systemctl start file.service
sudo systemctl start user.service
sudo systemctl start transmite.service
sudo systemctl start message.service
sudo systemctl start friend.service
sudo systemctl start gateway.service

原文地址:https://blog.csdn.net/2301_77412625/article/details/142613460

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!