Product Engineer, CTO & a Beer Enthusiast
Experiments, thoughts and scripts documented for posterity.
# Create application
mkdir dnsvc
cd dnsvc
dotnet new console
# Change Program.cs
cat > Program.cs <<EOF
using System;
using System.Threading;
namespace dnsvc
{
class Program
{
static void Main(
string[] args)
{
var sleep = 3000;
if (args.Length > 0) { int.TryParse(args[0], out sleep); }
while (true)
{
Console.WriteLine($"Working, pausing for {sleep}ms");
Thread.Sleep(sleep);
}
}
}
}
EOF
# Restore dependencies
dotnet restore
# Publish to a local bin sub directory
dotnet publish --configuration Release --output bin
# Run local to verify all is good
dotnet ./bin/dnsvc.dll
Will run the application from the bin sub directory for now
cat > dnsvc.service <<EOF
[Unit]
Description=Demo service
After=network.target
[Service]
ExecStart=/usr/bin/dotnet $(pwd)/bin/dnsvc.dll 5000
Restart=on-failure
[Install]
WantedBy=multi-user.target
EOF
# Copy service file to a System location
sudo cp dnsvc.service /lib/systemd/system
# Reload SystemD and enable the service, so it will restart on reboots
sudo systemctl daemon-reload
sudo systemctl enable dnsvc
# Start service
sudo systemctl start dnsvc
# View service status
systemctl status dnsvc
stdout output can be examined with journalctl
journalctl --unit dnsvc --follow
# Stop service
sudo systemctl stop dnsvc
systemctl status dnsvc
# Restart the service
sudo systemctl start dnsvc
systemctl status dnsvc