Gracefully handle AWS ECS stop task with .NET Core Application


I wrote a .NET core console application to run as a task in AWS ECS Fargate. Basically, the console application starts a processing thread and then needs to just keep running (much like a Kestrel web host.)

Initially, I drop in a Console.ReadKey() thinking it will never hit and the app will keep running. Well that leads to .NET exception…

So I then replace ReadKey with Console.Read(). The application acts like it isn’t even there, keeps on moving, and the app exits as soon as it starts.

Through research, I discover the Console.CancelKeyPress event. I implement that, and it works in the fact that the console app will keep running.

But I would like to gracefully handle shutdown of the application when that does occur.

Through additional research, I find that when AWS ECS stops a task, it doesn’t send Ctrl-C (SIGINT) it instead will send SIGTERM.

I then implement what I think is the standard event handling for SIGTERM in .NET (seen below.)

This compiles and runs fine in ECS. BUT… I hit the “stop” button through AWS ECS console, it stops the task and relaunches a new task. When I look at the log of that stopped task, there is no shutdown log message, no indication of a graceful shutdown, nothing. It appears that it was just force killed.

Am I missing something here? What can I do differently to gracefully catch the stop-task in AWS ECS to gracefully shut down the task?


It seems like it has do with your .net code of handling graceful shutdown. I just tested with following code locally, with docker and with ECS Stop-Task. At all places, I received graceful shutdown. I don’t have your full code so I created my own example.

Full github repo reference if you want to try by yourself.

Sample Logs you should see for container.

Waiting for signals

Unloding fired

Received signal gracefully shutting down

Hello World!

Note – To test SIGTERM signal locally, you will need to run kill -SIGTERM {PID} instead of CTRL+C on dotnet CLI.

Leave a Reply