Skip to content

Instantly share code, notes, and snippets.

@allenmichael
Created March 22, 2023 21:31
Show Gist options
  • Save allenmichael/507da07a564f58b5f551e55e4840c825 to your computer and use it in GitHub Desktop.
Save allenmichael/507da07a564f58b5f551e55e4840c825 to your computer and use it in GitHub Desktop.
All the resources needed to create a .NET worker app to run on Amazon Linux through systemd
<Project Sdk="Microsoft.NET.Sdk.Worker">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<PublishTrimmed>true</PublishTrimmed>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting.Systemd" Version="7.0.0" />
</ItemGroup>
</Project>
[Unit]
Description=Continuously running daemon from the `dotnet new worker` template
[Service]
Type=notify
# the self-contained, single-file executable that systemd runs to start the service
ExecStart=/home/ec2-user/DaemonWorker/DaemonWorker
# used to identify logs when using journalctl
SyslogIdentifier=DaemonWorker
# don't forget to use chmod +x /srv/DaemonWorker/DaemonWorker add execution permissions to the executable file
User=ec2-user
# restart if crashes
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
# Assumed install within Amazon Linux environment, your commands might vary based on OS
dotnet publish -c Release -r linux-x64
mkdir -p /home/ec2-user/DaemonWorker
cp ./bin/Release/net7.0/linux-x64/publish/DaemonWorker /home/ec2-user/DaemonWorker
chmod u+x /home/ec2-user/DaemonWorker/DaemonWorker
sudo cp DaemonWorker.service /etc/systemd/system/DaemonWorker.service
sudo systemctl daemon-reload
sudo systemctl start DaemonWorker
systemctl status DaemonWorker
journalctl -u DaemonWorker -f
using DaemonWorker;
IHost host = Host.CreateDefaultBuilder(args)
.UseSystemd()
.ConfigureServices(services =>
{
services.AddHostedService<Worker>();
})
.Build();
host.Run();
{
"runtimeOptions": {
"configProperties": {
"System.Globalization.Invariant": true
}
}
}
# Assumed uninstall within Amazon Linux environment, your commands might vary based on OS
sudo systemctl stop DaemonWorker
sudo systemctl disable DaemonWorker
sudo rm /etc/systemd/system/DaemonWorker.service
rm -r /home/ec2-user/DaemonWorker
sudo systemctl daemon-reload
sudo systemctl reset-failed DaemonWorker
namespace DaemonWorker;
public class Worker : BackgroundService
{
private readonly ILogger<Worker> _logger;
public Worker(ILogger<Worker> logger)
{
_logger = logger;
}
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
var time = DateTime.Now;
_logger.LogInformation($"Checking for the daemon hour: {time}");
if (time.Hour >= 0 && time.Hour <= 5 || time.Hour >= 21 && time.Hour <= 23)
{
_logger.LogCritical($"It's time for night of the daemons!: {time}");
_logger.LogWarning($"What a horrible night to have a curse: {time}");
}
else
{
_logger.LogWarning($"The morning sun has vanquished the horrible night: {time}");
}
await Task.Delay(5000, stoppingToken);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment