惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Attack and Defense Labs
Attack and Defense Labs
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Recent Announcements
Recent Announcements
博客园 - 【当耐特】
博客园 - 三生石上(FineUI控件)
量子位
aimingoo的专栏
aimingoo的专栏
V
V2EX
Vercel News
Vercel News
B
Blog
M
MIT News - Artificial intelligence
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hacker News: Ask HN
Hacker News: Ask HN
TaoSecurity Blog
TaoSecurity Blog
N
News and Events Feed by Topic
D
DataBreaches.Net
Blog — PlanetScale
Blog — PlanetScale
S
Secure Thoughts
U
Unit 42
博客园 - 叶小钗
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
N
News | PayPal Newsroom
Help Net Security
Help Net Security
S
Security Affairs
Microsoft Security Blog
Microsoft Security Blog
W
WeLiveSecurity
博客园 - Franky
Forbes - Security
Forbes - Security
Microsoft Azure Blog
Microsoft Azure Blog
博客园_首页
Schneier on Security
Schneier on Security
I
InfoQ
B
Blog RSS Feed
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Webroot Blog
Webroot Blog
AWS News Blog
AWS News Blog
Last Week in AI
Last Week in AI
Security Archives - TechRepublic
Security Archives - TechRepublic
C
CERT Recently Published Vulnerability Notes
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志
L
Lohrmann on Cybersecurity
SecWiki News
SecWiki News
Recent Commits to openclaw:main
Recent Commits to openclaw:main
J
Java Code Geeks

博客园 - 五蕴非空

AI工具实践日记(二):在 OpenClaw 中调用 OpenCode 进行开发任务 AI工具实践日记(一):在树莓派上搭建OpenClaw,一个后端开发者的真实踩坑记录 .net core XML 解析帮助类 常用工具类 .net Core 同一接口不同实现的依赖注入 - 五蕴非空 大批量数据操作的性能优化方案 .net core 3.1 配置文件立即更新 asp.net core 3.0 JObject The collection type 'Newtonsoft.Json.Linq.JObject' is not supported .net Core2.2 WebApi通过OAuth2.0实现微信登录 Asp.net导出Excel文件 Ext.Net 使用总结之GridPanel中的选中行 Ext.Net 使用总结之查询条件中的起始日期 Ext.Net 使用总结之GridPanel的删除事件 使用 NuGet 管理项目库 JavaScript 获取客户端计算机硬件及系统信息 程序员技术练级攻略 ThoughtWorks(中国)程序员读书雷达 Sql 分割 键值对字符串 得到某值对应的名称 Ext.net 中日期格式的计算
为.net Core 3.0 WebApi 创建Linux守护进程
五蕴非空 · 2020-03-15 · via 博客园 - 五蕴非空

前言

我们一般可以在Linux服务器上执行 dotnet <app_assembly.dll> 命令来运行我们的.net Core WebApi应用。但是这样运行起来的应用很不稳定,关闭终端窗口之后,应用也会停止运行。为了让其可以稳定运行,我们需要让它变成系统的守护进程,成为一种服务一直在系统中运行,出现异常时也能重新启动。
Linux系统有自己的守护进程管理工具 Systemd 。systemd 是内核启动后的第一个用户进程,PID 为1,是所有其它用户进程的父进程。它直接与内核交互,性能出色,可以提供用于启动、停止和管理进程的许多强大的功能。我们完全可以将程序交给 Systemd ,让系统统一管理,成为真正意义上的系统服务。
systemctl 用于管理 systemd 的行为,替换之前的 sysvinit 和 upstart。

创建服务文件

创建服务定义文件:

vim /etc/systemd/system/qf-intecabinet.service

以下是应用的一个示例服务文件:

[Unit]
Description=运行Qf.InteCabinet服务

[Service]
WorkingDirectory=/opt/InteCabinet  # 依赖环境,可以指定多个
ExecStart=/usr/bin/dotnet /opt/InteCabinet/Qf.InteCabinet.WebApi.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=qf-intecabinet
User=root  # 管理服务的用户,用户必须存在并且拥有正确应用文件的所有权
Environment=ASPNETCORE_ENVIRONMENT=Production  # 环境变量
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false

[Install]
WantedBy=multi-user.target  # 该服务所在的Target

管理服务

保存该文件并启用该服务

systemctl enable qf-intecabinet.service

启动该服务

systemctl start qf-intecabinet.service

查看服务状态

systemctl status qf-intecabinet.service

查看日志

journalctl -fu qf-intecabinet.service

使用时间选项(如 --since today、--until 1 hour ago)或这些选项的组合可以减少返回的条目数

journalctl -fu qf-intecabinet.service --since "2020-01-01" --until "2020-03-01 12:00"

关闭服务

systemctl stop qf-intecabinet.service

参考资料

微软官方文档