用C#编写Linux守护进程

如果要在Red Hat Enterprise Linux上将.NET Core进程作为后台进程运行,则可以创建自定义systemd单元。今天我将为.NET Core编写两个自定义系统单元的例子。一个是运行.NET Core控制台应用程序的一种类型,另一个是运行ASP.NET Core Web应用程序的简单类型。
控制台应用程序
建立一个应用程序
您可以用dotnet run在systemd中使用指定项目目录作为工作目录。但是,我们来构建一个二进制文件并将其用于systemd。用dotnet new 命令创建您的项目后编辑Program.cs如下。
using System;
using System.IO; namespace ConsoleApplication
{
public class Program
{
public static void Main(string[] args)
{
var path = Path.GetTempFileName();
File.WriteAllText(path, "Hello Temp File!");
Console.WriteLine($"Wrote temp file: {path}");
}
}
}
然后用dotnet publish命令发布项目。你会看到bin/<Configuration>/<Framework>目录下的二进制文件。
|
1
2
3
4
5
|
$ dotnet publish -c ReleasePublishing ConsoleApp for .NETCoreApp,Version=v1.1Project ConsoleApp (.NETCoreApp,Version=v1.1) was previously compiled. Skipping compilation.publish: Published to /home/tatanaka/Documents/git/tanaka-takayoshi/SystemdExample/1.1/ConsoleApp/bin/Release/netcoreapp1.1/publishPublished 1/1 projects successfully |
创建一个自定义的systemd
首先,创建一个运行守护进程和工作目录的用户。
$ sudo useradd -s /sbin/nologin dotnetuser
$ sudo mkdir /var/SystemdExample
$ sudo cp /home/tatanaka/Documents/git/tanaka-takayoshi/SystemdExample/1.1/ConsoleApp/bin/Release/netcoreapp1.1/publish/* /var/SystemdExample
$ sudo chown -R dotnetuser:dotnetuser /var/SystemdExample
然后在/etc/systemd/system/目录下创建一个自定义的systemd单元文件。文件名应该是<unit-name>.<unit-type>。我创建的目录和文件名为:/etc/systemd/system/netcore-console-example.service。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
[Unit]Description=Example for .NET Core ConsoleApp with systemdDefaultDependencies=no[Service]Type=oneshotRemainAfterExit=noExecStart=/opt/rh/rh-dotnetcore11/root/usr/bin/dotnet ConsoleApp.dllWorkingDirectory=/var/SystemdExampleUser=dotnetuserGroup=dotnetuser[install] |
您应该在ExecStart中指定dotnet的完整路径。以上是红帽提供的.NET Core 1.1的情况。然后你可以用systemctl命令执行守护进程。您可以使用systemctl status命令或journalctl命令查看控制台输出。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
$ sudo systemctl start netcore-console-example.service $ sudo systemctl status netcore-console-example.service ● netcore-console-example.service - Example for .NET Core ConsoleApp with systemd Loaded: loaded (/etc/systemd/system/netcore-console-example.service; enabled; vendor preset: disabled) Active: inactive (dead) since Fri 2017-02-24 00:29:16 JST; 13s ago Process: 18075 ExecStart=/opt/rh/rh-dotnetcore11/root/usr/bin/dotnet ConsoleApp.dll (code=exited, status=0/SUCCESS) Main PID: 18075 (code=exited, status=0/SUCCESS)Feb 24 00:29:16 localhost.localdomain systemd[1]: Starting Example for .NET Core ConsoleApp with systemd...Feb 24 00:29:16 localhost.localdomain dotnet[18075]: Wrote temp file: /tmp/tmph1ok6H.tmpFeb 24 00:29:16 localhost.localdomain systemd[1]: Started Example for .NET Core ConsoleApp with systemd.$ journalctl -u netcore-console-example.service -eFeb 24 00:29:16 localhost.localdomain systemd[1]: Starting Example for .NET Core ConsoleApp with systemd...Feb 24 00:29:16 localhost.localdomain dotnet[18075]: Wrote temp file: /tmp/tmph1ok6H.tmpFeb 24 00:29:16 localhost.localdomain systemd[1]: Started Example for .NET Core ConsoleApp with systemd.$ sudo cat /tmp/tmph1ok6H.tmpHello Temp File! |
使用PrivateTemp
在上述系统单元中,程序在临时文件夹下写入一个文件。你有时想写一个来自其他用户的临时文件是安全的。您可以在[Service]section中的指定使用PrivateTemp。
|
1
2
3
4
5
6
7
8
|
[Service]Type=oneshotRemainAfterExit=noExecStart=/opt/rh/rh-dotnetcore11/root/usr/bin/dotnet ConsoleApp.dllWorkingDirectory=/var/SystemdExampleUser=dotnetuserGroup=dotnetuserPrivateTemp=true |
重新加载单元文件后,程序可以像前一样访问/tmp目录,但这不是实际的/tmp目录。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
$ sudo systemctl daemon-reload $ sudo systemctl start netcore-console-example.service$ sudo systemctl status netcore-console-example.service● netcore-console-example.service - Example for .NET Core ConsoleApp with systemd Loaded: loaded (/etc/systemd/system/netcore-console-example.service; enabled; vendor preset: disabled) Active: inactive (dead) since Fri 2017-02-24 00:35:46 JST; 12s ago Process: 18415 ExecStart=/opt/rh/rh-dotnetcore11/root/usr/bin/dotnet ConsoleApp.dll (code=exited, status=0/SUCCESS) Main PID: 18415 (code=exited, status=0/SUCCESS)Feb 24 00:35:46 localhost.localdomain systemd[1]: Starting Example for .NET Core ConsoleApp with systemd...Feb 24 00:35:46 localhost.localdomain dotnet[18415]: Wrote temp file: /tmp/tmpJLWAGC.tmpFeb 24 00:35:46 localhost.localdomain systemd[1]: Started Example for .NET Core ConsoleApp with systemd.$ ls /tmp/tmpJLWAGC.tmpls: cannot access /tmp/tmpJLWAGC.tmp: No such file or directory |
Web应用程序
建立一个应用程序
现在我们来构建一个ASP.NET Core Web应用程序。今天我使用默认的模板项目。
|
1
2
3
4
5
6
7
8
9
10
|
$ dotnet new -t webCreated new C# project in /home/tatanaka/Documents/git/tanaka-takayoshi/SystemdExample/1.1/WebApp.$ dotnet restore ** snipped**log : Restore completed in 9721ms.$ dotnet publish -c ReleasePublishing WebApp for .NETCoreApp,Version=v1.1** snipped **publish: Published to /home/tatanaka/Documents/git/tanaka-takayoshi/SystemdExample/1.1/WebApp/bin/Release/netcoreapp1.1/publishPublished 1/1 projects successfully |
现在可以用dotnet命令运行。
|
1
2
3
4
5
6
7
|
$ dotnet bin/Release/netcoreapp1.1/publish/WebApp.dll info: Microsoft.Extensions.DependencyInjection.DataProtectionServices[0] User profile is available. Using '/home/tatanaka/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.Hosting environment: ProductionContent root path: /home/tatanaka/Documents/git/tanaka-takayoshi/SystemdExample/1.1/WebAppNow listening on: http://localhost:5000Application started. Press Ctrl+C to shut down. |
创建一个自定义的systemd
为这个Web应用程序也指定dotnetuser名称。
|
1
2
3
|
$ sudo mkdir /var/SystemdExample$ sudo cp -R bin/Release/netcoreapp1.1/publish/* /var/SystemdWebExample$ sudo chown -R dotnetuser:dotnetuser /var/SystemdWebExample |
然后创建一个自定义的systemd单元文件/etc/systemd/system/netcore-web-example.service。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
[Unit]Description=Example for .NET Core WebApp with systemdDefaultDependencies=noWants=network.target # network is requiredAfter=network.target[Service]ExecStart=/opt/rh/rh-dotnetcore11/root/usr/bin/dotnet WebApp.dllWorkingDirectory=/var/SystemdWebExampleRestart=alwaysRestartSec=10 # Restart service after 10 seconds if dotnet service crashesSyslogIdentifier=dotnet-exampleUser=dotnetuserGroup=dotnetuserPrivateTmp=trueEnvironment=ASPNETCORE_ENVIRONMENT=Production # specify environment variable for environmentEnvironment=ASPNETCORE_URLS=http://*:8080 # specify environement variable for listening port[Install]WantedBy = multi-user.target |
最后,您可以将ASP.NET Core应用程序作为Linux守护程序运行。请注意,此应用程序侦听端口8080代替了ASP.NET Core 默认的 5000,因为我在ASPNETCORE_URLS单元文件中指定了环境变量 。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
$ systemctl start netcore-web-example.service[tatanaka@localhost WebApp]$ systemc^C[tatanaka@localhost WebApp]$ sudo systemctl status netcore-web-example.service[sudo] password for tatanaka: ● netcore-web-example.service - Example for .NET Core WebApp with systemd Loaded: loaded (/etc/systemd/system/netcore-web-example.service; disabled; vendor preset: disabled) Active: active (running) since Sat 2017-02-25 01:02:12 JST; 11s ago Main PID: 7041 (dotnet) CGroup: /system.slice/netcore-web-example.service └─7041 /opt/rh/rh-dotnetcore11/root/usr/bin/dotnet WebApp.dllFeb 25 01:02:12 localhost.localdomain systemd[1]: Started Example for .NET Core WebApp with systemd.Feb 25 01:02:12 localhost.localdomain systemd[1]: Starting Example for .NET Core WebApp with systemd...Feb 25 01:02:12 localhost.localdomain dotnet-example[7041]: info: Microsoft.Extensions.DependencyInjection.DataProtectionServices[0]Feb 25 01:02:12 localhost.localdomain dotnet-example[7041]: User profile is available. Using '/home/dotnetuser/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.Feb 25 01:02:13 localhost.localdomain dotnet-example[7041]: Hosting environment: ProductionFeb 25 01:02:13 localhost.localdomain dotnet-example[7041]: Content root path: /var/SystemdWebExampleFeb 25 01:02:13 localhost.localdomain dotnet-example[7041]: Now listening on: http://*:8080Feb 25 01:02:13 localhost.localdomain dotnet-example[7041]: Application started. Press Ctrl+C to shut down.$ journalctl -u netcore-web-example -xf-- Logs begin at Mon 2017-02-20 11:58:31 JST. --Feb 25 01:02:36 localhost.localdomain dotnet-example[7041]: info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]Feb 25 01:02:36 localhost.localdomain dotnet-example[7041]: Sending file. Request path: '/images/banner4.svg'. Physical path: '/var/SystemdWebExample/wwwroot/images/banner4.svg'Feb 25 01:02:36 localhost.localdomain dotnet-example[7041]: info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]Feb 25 01:02:36 localhost.localdomain dotnet-example[7041]: Request finished in 0.1973ms 200 image/svg+xmlFeb 25 01:02:36 localhost.localdomain dotnet-example[7041]: info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]Feb 25 01:02:36 localhost.localdomain dotnet-example[7041]: Request starting HTTP/1.1 GET http://localhost:8080/favicon.icoFeb 25 01:02:36 localhost.localdomain dotnet-example[7041]: info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]Feb 25 01:02:36 localhost.localdomain dotnet-example[7041]: Sending file. Request path: '/favicon.ico'. Physical path: '/var/SystemdWebExample/wwwroot/favicon.ico'Feb 25 01:02:36 localhost.localdomain dotnet-example[7041]: info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]Feb 25 01:02:36 localhost.localdomain dotnet-example[7041]: Request finished in 0.5824ms 200 image/x-icon |
然而这对于ASP.NET Core的生产使用来说是不够的。你可能需要设置一个反向代理服务器,比如Jexus,nginx,防火墙等等。
用C#编写Linux守护进程的更多相关文章
- C#与Linux守护进程
用C#编写Linux守护进程 如果要在Red Hat Enterprise Linux上将.NET Core进程作为后台进程运行,则可以创建自定义systemd单元.今天我将为.NET Core编 ...
- Linux守护进程编写指南
Linux守护进程编写指南 守护进程(Daemon)是运行在后台的一种特殊进程.它独立于控制终端并且周期性地执行某种任务或等待处理某些发生的事件.守护进程是一种很有用的进 程.Linux的大多数服务器 ...
- Linux守护进程的编程实现
Linux 守护进程的编程方法 守护进程(Daemon)是执行在后台的一种特殊进程.它独立于控制终端而且周期性地执行某种任务或等待处理某些发生的事件.守护进程是一种非常实用的进程.Linux的大多数s ...
- 笔记整理--Linux守护进程
Linux多进程开发(三)进程创建之守护进程的学习 - _Liang_Happy_Life__Dream - 51CTO技术博客 - Google Chrome (2013/10/11 16:48:2 ...
- CentOS 7.4 初次手记:第一章 Linux守护进程(daemon)
第一节 init & sysvinit 6 I sysvinit 运行顺序... 6 II Sysvinit和系统关闭... 7 III Sysvinit 的小结... 7 IV 运行级别.. ...
- 【转】学习Linux守护进程详细笔记
[原文]https://www.toutiao.com/i6566814959966093837/ Linux守护进程 一. 守护进程概述 守护进程,也就是通常所说的Daemon进程,是Linux中的 ...
- [Linux] 守护进程和守护线程
对于JAVA而言,一般一个应用程序只有一个进程——JVM.除非在代码里面另外派生或者开启了新进程. 而线程,当然是由进程开启的.当开启该线程的进程离开时,线程也就不复存在了. 所以,对于JAVA而言, ...
- .NET跨平台实践:用C#开发Linux守护进程
Linux守护进程(Daemon)是Linux的后台服务进程,它脱离了与控制终端的关联,直接由Linux init进程管理其生命周期,即使你关闭了控制台,daemon也能在后台正常工作. 一句话,为L ...
- .NET跨平台实践:用C#开发Linux守护进程(转)
Linux守护进程(Daemon)是Linux的后台服务进程,它脱离了与控制终端的关联,直接由Linux init进程管理其生命周期,即使你关闭了控制台,daemon也能在后台正常工作. 一句话,为L ...
随机推荐
- zabbix web监控模板
问题关键:宏变量 {HOST.NAME} 配置 我直接在 Linux OS这个模板中添加的,都随意: 效果 加个报警 以上.
- Linux系统编程:简单文件IO操作
使用Linux的文件API,经常看见一个东西,叫做文件描述符. 什么是文件描述符? (1)文件描述符其实实质是一个数字,这个数字在一个进程中表示一个特定的含义,当我们open打开一个文件时,操作系统在 ...
- AutoMapper 使用总结
初识AutoMapper 在开始本篇文章之前,先来思考一个问题:一个项目分多层架构,如显示层.业务逻辑层.服务层.数据访问层.层与层访问需要数据载体,也就是类.如果多层通用一个类,一则会暴露出每层的字 ...
- 抽象方法为什么不能被private与static修饰
private private访问修饰符修饰的方法只能在本类当中使用.所以,必然不能用private去修饰抽象方法.抽象方法一定是要被子类去重写的. static Java中用static修饰符修饰的 ...
- 20 Zabbix系统性能优化建议
点击返回:自学Zabbix之路 20 Zabbix系统性能优化建议 1. Zabbix性能变慢的可能表现: zabbix队列有太多被延迟的item,可以通过administration-queue查看 ...
- PC端截取GIF图片的软件
PC端截取GIF图片的软件分享:下载>>
- AssertionError while merging cells with xlwt (Python)
产生这一错误的原因是,行列数字的赋值有问题,三行数字大于下行数字,左列数字大于右列数字. sheet.write_merge(top_row, bottom_row, left_column, rig ...
- spring的父子容器
在创建ssm项目工程时,经常需要读取properties资源配置文件,传统的方法当然可以. 但是spring提供了更简便的方法,@value注解. 在page.properties文件中,配置分页信息 ...
- MIME---multipart类型
1.3 multipart类型 MIME邮件中各种不同类型的内容是分段存储的,各个段的排列方式.位置信息都通过Content-Type域的multipart类型来定义.multipart类型主要有三 ...
- 深入一致性哈希(Consistent Hashing)算法原理,并附100行代码实现
转自:https://my.oschina.net/yaohonv/blog/1610096 本文为实现分布式任务调度系统中用到的一些关键技术点分享——Consistent Hashing算法原理和J ...