debian让dotnet 程序以守护进程方式运行,如果意外退出主动开启

  1. 创建服务文件: 打开一个新的.service文件在/etc/systemd/system/目录下,例如myapp.service
bash 复制代码
sudo nano /etc/systemd/system/myapp.service
  1. 编辑服务文件: 添加以下内容到myapp.service文件,确保修改ExecStart以指向你的.NET Core应用程序的可执行文件,以及其他的路径和配置根据你的需求:
bash 复制代码
[Unit]
Description=My .NET Core Application

[Service]
WorkingDirectory=/path/to/your/app
ExecStart=/usr/bin/dotnet /path/to/your/app/YourApp.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-app
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target
  • WorkingDirectory: 应用程序的工作目录。
  • ExecStart: 启动应用程序的命令。
  • Restart: 服务失败时的重启策略,always表示总是重启。
  • RestartSec: 重启前等待的时间。
  • KillSignal: 系统用于停止服务的信号。
  • User: 运行此服务的用户。
  • Environment: 设置环境变量,如ASP.NET Core环境。
    注:如何知道ExecStart中命令的路径呢?
    答:which命令会在您的系统的PATH环境变量指定的目录中搜索可执行文件。
bash 复制代码
which dotnet

我的配置如下:

bash 复制代码
[Unit]
Description=自动运行程序

[Service]
WorkingDirectory=/root/workerservice
ExecStart=/root/dotnet/dotnet  /root/workerservice/WorkerServiceForLinux.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-app
User=root
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target
  1. 启用和启动服务: 重新加载systemd守护进程以读取新的服务文件,然后启用并启动你的服务
bash 复制代码
sudo systemctl daemon-reload
sudo systemctl enable myapp.service
sudo systemctl start myapp.service
  1. 检查服务状态: 使用以下命令查看服务状态
bash 复制代码
sudo systemctl status myapp.service
  1. 停止与disable 服务
bash 复制代码
sudo systemctl disable myapp.service
sudo systemctl stop myapp.service
  1. 通过这些步骤,您的.NET Core应用程序将作为守护进程运行在Debian系统上。如果应用程序崩溃,systemd将按照您在myapp.service文件中定义的Restart和RestartSec选项自动重新启动它。
相关推荐
木子欢儿26 分钟前
Debian 13 + vsftpd 搭建FTP服务
linux·服务器·前端·网络·debian
讯捷蓝达38 分钟前
IBM V3500存储 双控故障、数据怎么恢复?
运维
qq_谁赞成_谁反对1 小时前
甲方IT的成长之路--SG TCP网络优化工具--20261026
运维·服务器·udp·tcp
龙亘川2 小时前
适应人工智能发展、构建就业友好型发展方式
大数据·运维·人工智能·信息可视化·智慧城市
数据库小学妹2 小时前
OLAP数据库怎么选、怎么优化?路线、提速与边界
运维·数据库·后端
说实话起个名字真难啊2 小时前
实战docker部署dify及注意点
运维·docker·容器
福建佰胜张工2 小时前
费希尔 FIELDVUE™ DVC6200-AC 智能阀门定位器深度解析:双作用配置、核心参数与典型应用
运维·安全·自动化
小羊没烦恼!2 小时前
分布式事务 | 使用DTM 的Saga 模式
java·linux·运维·数据库·wpf
上海运维Q先生3 小时前
PHP8.3.36升级修复手册----CVE-2026-109618 CVE-2026-109619
运维·php
NailiConveyor3 小时前
输送机机架怎么选材?碳钢、不锈钢、铝型材的成本与适用场景
大数据·运维·自动化·制造·业界资讯