Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
(연관된 글이 1개 있습니다.)

C# - Generic Host를 이용해 .NET 5로 리눅스 daemon 프로그램 만드는 방법

지난번에는 별다른 의존성 없이 단순하게 만드는 방법을 소개했는데요,

C# - .NET Core Console로 리눅스 daemon 프로그램 만드는 방법
; https://www.sysnet.pe.kr/2/0/11958

이번에는 ASP.NET Core의 Generic host를 이용한 방법으로 작성해 보겠습니다. 이에 대해서는 마이크로소프트의 문서에 친절하게 설명하고 있으니,

.NET Generic Host in ASP.NET Core
; https://learn.microsoft.com/en-us/aspnet/core/fundamentals/host/generic-host

위의 내용을 보셔도 충분합니다. ^^




시작은 .NET 5 Console 프로그램으로 만듭니다. 그다음 Generic Host를 위한 관련 패키지를 추가하면 되는데요, 혹은 그냥 간단하게 ".NET Core 콘솔 프로젝트에서 Kestrel 호스팅 방법" 글에서 설명한 것처럼 Console csproj 파일의 Sdk 항목을 "Microsoft.NET.Sdk.Web"으로 변경해도 됩니다.

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>
  </PropertyGroup>

</Project>

이제 Program.cs의 Main 메서드를 다음과 같이 작성하고,

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace testd
{
    class Program
    {
        static async Task Main(string[] args)
        {
            var host = new HostBuilder()
                .ConfigureServices((hostContext, services) => services.AddHostedService<ShutdownService>())
                .UseConsoleLifetime()
                .Build();

            await host.RunAsync();
        }
    }
}

실질적인 서비스 코드는 별도의 ShutdownService.cs 파일을 만들어 다음과 같이 만들어 두면 됩니다.

using Microsoft.Extensions.Hosting;
using System.Threading;
using System.Threading.Tasks;

namespace testd
{
    class ShutdownService : IHostedService
    {
        private bool pleaseStop;
        private Task BackgroundTask;
        private readonly IHostApplicationLifetime applicationLifetime;

        public ShutdownService(IHostApplicationLifetime applicationLifetime)
        {
            this.applicationLifetime = applicationLifetime;
        }

        public Task StartAsync(CancellationToken _)
        {
            Console.WriteLine("[testd] Starting service");

            BackgroundTask = Task.Run(async () =>
            {
                while (!pleaseStop)
                {
                    await Task.Delay(50);
                }

                Console.WriteLine("[testd] Background task gracefully stopped");
            });

            return Task.CompletedTask;
        }

        public async Task StopAsync(CancellationToken cancellationToken)
        {
            Console.WriteLine("[testd] Stopping service");

            pleaseStop = true;
            await BackgroundTask;

            Console.WriteLine("[testd] Service stopped");
        }
    }
}

당연히, BackgroundTask 속성에 할당한 Task.Run 코드에는 여러분이 원하는 코드를 넣어야 합니다. 사실상 위의 코드가 전부이고 나머지 팁/트릭은 ".NET Generic Host in ASP.NET Core" 글의 내용을 참고해 멋을 좀 더 부리시면 됩니다.




편의상, 서비스 등록/해제를 위한 코드를 "C# - .NET Core Console로 리눅스 daemon 프로그램 만드는 방법" 글에서 소개한 방법처럼 사용해도 됩니다.

using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

namespace testd
{
    class Program
    {
        static async Task Main(string[] args)
        {
            if (args.Length >= 1)
            {
                string netDllPath = typeof(Program).Assembly.Location;

                if (args[0] == "--install" || args[0] == "-i")
                {
                    InstallService(netDllPath, true);
                }

                else if (args[0] == "--uninstall" || args[0] == "-u")
                {
                    InstallService(netDllPath, false);
                }

                return;
            }

            var host = new HostBuilder()
                .ConfigureServices((hostContext, services) => services.AddHostedService<ShutdownService>())
                .UseConsoleLifetime()
                .Build();

            await host.RunAsync();
        }


        static int InstallService(string netDllPath, bool doInstall)
        {
            // 2021-04-22 업데이트
            // KillSignal=SIGINT 제거
            // KillMode=mixed 추가
            string serviceFile = @"
[Unit]
Description={0} running on {1}

[Service]
WorkingDirectory={2}
ExecStart={3} {4}
SyslogIdentifier={5}
KillMode=mixed

[Install]
WantedBy=multi-user.target
";
            // ...[생략]...
        }

        static int ControlService(string serviceName, string mode)
        {
            // ...[생략]...
        }
    }
}

이후 서비스 등록/해제 및 systemctl 관련 명령어를 다음과 같이 수행할 수 있습니다.

[서비스 등록]
sudo dotnet ./testd.dll --install

[서비스 해제]
sudo dotnet ./testd.dll --uninstall

[서비스 시작]
sudo systemctl start dotnet-testd

[서비스 중지-1 SIGINT]
sudo systemctl stop dotnet-testd

[서비스 중지-2 SIGTERM]
sudo systemctl kill dotnet-testd

// 기타 Ctrl + '\'키로 발생하는 SIGQUIT

확인을 위해 서비스 등록을 하고 "tail -F /var/log/syslog"로 보면 다음과 같은 로그가 찍혀 있습니다.

Apr 21 22:27:48 testnix systemd[1]: Started testd.dll running on Unix 5.8.0.48.
Apr 21 22:27:48 testnix dotnet-testd[758407]: [testd] Starting service

그리고 서비스 중지(systemctl kill)를 하면 이렇게 로그가 남고!

Apr 21 22:28:30 testnix dotnet-testd[758407]: [testd] Stopping service
Apr 21 22:28:30 testnix dotnet-testd[758407]: [testd] Background task gracefully stopped
Apr 21 22:28:30 testnix dotnet-testd[758407]: [testd] Service stopped
Apr 21 22:28:30 testnix systemd[1]: dotnet-testd.service: Succeeded.

(첨부 파일은 이 글의 예제 코드를 포함합니다.)




그나저나, 제가 이 글을 쓴 이유는 ".NET Core 콘솔 프로젝트에서 Kestrel 호스팅 방법" 글에 달린 덧글 때문입니다. ^^;

현재 .NET 5로 해당 프로그램을 만들면 Ctrl + C에 대해 잘 반응을 하지만 daemon으로 등록해 두면 systemctl stop(SIGINT)에 대해 반응하지 않고 곧바로 프로세스가 종료됩니다. 종료가 된다는 것은 다행이지만 Console.CancelKeyPress 이벤트 핸들러가 실행되지 않으므로 아쉽게도 프로세스 종료 시 수행해야 할 특정 작업이 있다면 더 이상 실행이 되지 않습니다.

그래서 혹시나 마이크로소프트 측에서 만든 서비스 관련 코드라면 이에 대한 반응을 준비하지 않았나 싶어 Generic Host를 이용해 다시 한번 동일한 daemon 예제 코드를 작성해 본 것인데요, 마찬가지로 "systemctl stop"에는 반응하지 않았습니다. (즉,"Background task gracefully stopped" 등의 로그가 남지 않습니다.)

혹시 이 원인에 대해 리눅스 환경 및 닷넷 코어를 잘 아시는 분은 덧글 부탁드립니다. ^^




2021-04-22 업데이트: 덧글에 주신 의견에 따라, 서비스 등록 시 기존의 KillSignal=SIGINT를 제외하고 KillMode=mixed를 추가하면 systemctl stop/kill에 대해 SIGTERM 신호를 받을 수 있어 ProcessExit 이벤트가 실행됩니다. (당분간이라고 해야 할지 모르겠지만) 일단은 이런 방법으로 stop/kill에 대해 수행할 코드가 있다면 대처하시면 되겠습니다.




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 3/26/2023]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 



2021-04-21 11시12분
아래의 글은 Generic Host와 docker 환경에서의 종료 관계를 설명하고 있습니다.

Graceful Shutdown C# Apps
; https://medium.com/@rainer_8955/gracefully-shutdown-c-apps-2e9711215f6d
정성태
2021-04-22 11시12분
[----] 글의 아래 부분이 잘못된 내용이라 댓글 남깁니다.

> [서비스 중지-1 SIGINT]
> sudo systemctl stop dotnet-testd

> [서비스 중지-2 SIGTERM]
> sudo systemctl kill dotnet-testd

https://www.freedesktop.org/software/systemd/man/systemd.kill.html#Options 를 참고했을때,

systemctl 의 기본 KillMode는 control-group입니다. 제가 이해한 바, 이 경우 `systemctl stop`이 실행되었을때 다음이 실행됩니다.

1. ExecStop 에 명시된 명령을 실행
2. 이후 종료되지 않은 프로세스가 있다면 프로세스를 강제 종료

아래 인용에 의해, systemctl 의 KillMode를 mixed로 변경하면 이 글의 문제가 해결될것으로 추측합니다.

> If set to mixed, the SIGTERM signal (see below) is sent to the main process while the subsequent SIGKILL signal (see below) is sent to all remaining processes of the unit's control group

또한 systemctl stop 과 systemctl kill 의 차이는 http://0pointer.de/blog/projects/systemd-for-admins-4.html 의 아래 인용이 잘 설명해주고 있으니 참고하시면 될 것 같습니다.

> How does this relate to systemctl stop? kill goes directly and sends a signal to every process in the group, however stop goes through the official configured way to shut down a service, i.e. invokes the stop command configured with ExecStop= in the service file. Usually stop should be sufficient.
[guest]
2021-04-22 11시22분
[----] 제 댓글 중 오해를 부를 수 있는 문장이 있어 다시 댓글 남깁니다.
> systemctl 의 KillMode를 mixed로 변경하면 이 글의 문제가 해결될것으로 추측합니다.

위는

"dotnet-testd 서비스의 KillMode를 mixed로 변경하면 이 글의 문제가 해결될것으로 추측합니다."

로 수정되야 합니다.
[guest]
2021-04-22 05시21분
의견 정말 감사합니다. 테스트를 해보니, 기존의 dotnet-testd.service 파일에 있던 KillSignal=SIGINT를 지우고 KillMode=mixed를 추가해 ProcessExit 이벤트가 모두 실행이 되는 것을 확인했습니다.

한 가지 의문이 있는데요, 아래와 같이 제가 주석을 달았던 것은,

> [서비스 중지-1 SIGINT]
> sudo systemctl stop dotnet-testd

stop 명령어의 경우 다음의 문서 내용과 함께,

systemd.service — Service unit configuration
; https://www.freedesktop.org/software/systemd/man/systemd.service.html

"
ExecStop =

If this option is not specified, the process is terminated by sending the signal specified in KillSignal= or RestartKillSignal= when service stop is requested.
"

이 글의 원래 시작이었던 daemon 소스 코드를 보면,

C# - .NET Core Console로 리눅스 daemon 프로그램 만드는 방법
; https://www.sysnet.pe.kr/2/0/11958

ExecStop은 비어 있고, KillSignal=SIGINT로 설정했기 때문에 "systemctl stop" 명령은 SIGINT를 보내는 것이 맞습니다. 실제로 동일한 소스 코드를 .NET Core 3.1 이하의 환경에서 실행하면 SIGINT (Ctrl+C)에 반응해 Console.CancelKeyPress 이벤트 핸들러가 잘 실행이 됩니다.

즉, 원칙적으로 보면 KillMode의 mixed/control-group 설정에 관계없이 (.NET Core 3.1 이하에서 그랬듯이) 잘 동작해야 하는 것이 아닌가... 라는 것이 의문입니다.
정성태
2021-04-22 06시38분
[----] > C# - .NET Core Console로 리눅스 daemon 프로그램 만드는 방법
> ; https://www.sysnet.pe.kr/2/0/11958

> 즉, 원칙적으로 보면 KillMode의 mixed/control-group 설정에 관계없이 (.NET Core 3.1 이하에서 그랬듯이) 잘 동작해야 하는 것이 아닌가... 라는 것이 의문입니다.

과연 그렇네요, 제가 "C# - .NET Core Console로 리눅스 daemon 프로그램 만드는 방법" 글을 보지 않아 KillSignal=SIGINT 로 되어 있다는 사실을 몰랐네요. 성태님의 말씀이 옳습니다.

https://github.com/dotnet/runtime/issues/51221#issuecomment-823043104 에 이 글과 유사한 문제제기가 있네요.

> Feels like this should be reverted to 3.1 behavior and focus on this #50527 for signal handling improvements. Using SIGTERM to shutdown currently in .NET Core requires more code and blocking code in process exit in order to let other code run (and can result in deadlocks if done incorrectly).


좋은 글 올려주셔서 늘 감사해하고 있습니다. 감사합니다.
[guest]
2021-04-22 09시24분
저도 감사드립니다. ^^
정성태
2021-05-29 05시54분
아래의 이슈가 해결되었다고 나옵니다.

.NET 5 apps can no longer intercept SIGINT signals (receive CancelKeyPress events) when running under Docker #51221
https://github.com/dotnet/runtime/issues/51221

따라서, 다음 버전의 .NET 5 업그레이드에서는 아마도 이 글에서 다룬 문제가 해결될 것입니다.
정성태
2022-06-30 10시19분
Running .NET Core Applications as a Windows Service
; https://code-maze.com/aspnetcore-running-applications-as-windows-service/

Story about graceful termination with modern .NET
; https://blog.kbegiedza.eu/dotnet-and-story-about-graceful-termination
정성태
2023-02-03 09시01분
A Noob Introduction to Hosted Services in ASP.NET Core
; https://mbarkt3sto.hashnode.dev/a-noob-introduction-to-hosted-services-in-aspnet-core

How to start using .NET Background Services
; https://blog.jetbrains.com/dotnet/2023/05/09/dotnet-background-services/

--------------------

Concurrent Hosted Service in .NET 8 | .NET Conf 2023
; https://youtu.be/sD_-XwauabE
정성태

... 46  47  48  49  50  51  52  [53]  54  55  56  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
12325정성태9/12/20209553개발 환경 구성: 515. OpenVPN - 재부팅 후 ICS(Internet Connection Sharing) 기능이 동작 안하는 문제
12324정성태9/11/202010837개발 환경 구성: 514. smigdeploy.exe를 이용한 Windows Server 2016에서 2019로 마이그레이션 방법
12323정성태9/11/20209752오류 유형: 649. Copy Database Wizard - The job failed. Check the event log on the destination server for details.
12322정성태9/11/202010957개발 환경 구성: 513. Azure VM의 RDP 접속 위치 제한 [1]
12321정성태9/11/20209002오류 유형: 648. netsh http add urlacl - Error: 183 Cannot create a file when that file already exists.
12320정성태9/11/202010173개발 환경 구성: 512. RDP(원격 데스크톱) 접속 시 비밀 번호를 한 번 더 입력해야 하는 경우
12319정성태9/10/20209972오류 유형: 647. smigdeploy.exe를 Windows Server 2016에서 실행할 때 .NET Framework 미설치 오류 발생
12318정성태9/9/20209386오류 유형: 646. OpenVPN - "TAP-Windows Adapter V9" 어댑터의 "Network cable unplugged" 현상
12317정성태9/9/202011748개발 환경 구성: 511. Beats용 Kibana 기본 대시 보드 구성 방법
12316정성태9/8/202010140디버깅 기술: 170. WinDbg Preview 버전부터 닷넷 코어 3.0 이후의 메모리 덤프에 대해 sos.dll 자동 로드
12315정성태9/7/202012474개발 환경 구성: 510. Logstash - FileBeat을 이용한 IIS 로그 처리 [2]
12314정성태9/7/202011127오류 유형: 645. IIS HTTPERR - Timer_MinBytesPerSecond, Timer_ConnectionIdle 로그
12313정성태9/6/202012196개발 환경 구성: 509. Logstash - 사용자 정의 grok 패턴 추가를 이용한 IIS 로그 처리
12312정성태9/5/202016064개발 환경 구성: 508. Logstash 기본 사용법 [2]
12311정성태9/4/202011325.NET Framework: 937. C# - 간단하게 만들어 보는 리눅스의 nc(netcat), json_pp 프로그램 [1]
12310정성태9/3/202010594오류 유형: 644. Windows could not start the Elasticsearch 7.9.0 (elasticsearch-service-x64) service on Local Computer.
12309정성태9/3/202010329개발 환경 구성: 507. Elasticsearch 6.6부터 기본 추가된 한글 형태소 분석기 노리(nori) 사용법
12308정성태9/2/202011618개발 환경 구성: 506. Windows - 단일 머신에서 단일 바이너리로 여러 개의 ElasticSearch 노드를 실행하는 방법
12307정성태9/2/202012345오류 유형: 643. curl - json_parse_exception / Invalid UTF-8 start byte
12306정성태9/1/202010494오류 유형: 642. SQL Server 시작 오류 - error code 10013
12305정성태9/1/202011409Windows: 172. "Administered port exclusions"이 아닌 포트 범위 항목을 삭제하는 방법
12304정성태8/31/202010334개발 환경 구성: 505. 윈도우 - (네트워크 어댑터의 우선순위로 인한) 열거되는 IP 주소 순서를 조정하는 방법
12303정성태8/30/202010553개발 환경 구성: 504. ETW - 닷넷 프레임워크 기반의 응용 프로그램을 위한 명령행 도구 etrace 소개
12302정성태8/30/202010438.NET Framework: 936. C# - ETW 관련 Win32 API 사용 예제 코드 (5) - Private Logger파일 다운로드1
12301정성태8/30/202010754오류 유형: 641. error MSB4044: The "Fody.WeavingTask" task was not given a value for the required parameter "IntermediateDir".
12300정성태8/29/202010141.NET Framework: 935. C# - ETW 관련 Win32 API 사용 예제 코드 (4) CLR ETW Consumer파일 다운로드1
... 46  47  48  49  50  51  52  [53]  54  55  56  57  58  59  60  ...