Search code examples
c#.netlinux.net-coredaemon

Dotnet app started as daemon doesn't working properly


I developed a simple application using NetCore, the app write the content on a file every time is executed, this is the code:

using System;
using System.IO;

namespace SimpleApp
{
   class Program
   {
       static void Main(string[] args)
       {
          using (StreamWriter writer = new StreamWriter("log.txt", true))
          {
              writer.WriteLine("Hello World");
          }
       }
   }
}

so if I start the application in this way: dotnet SimpleApp.dll I get a log.txt file with Hello World in it.

Now, I'm trying to create a linux daemon, I have no experience in that, so I wrote what I learned on Internet, I created a service cakked console.service which contains this structure:

[Unit]
Description = Hello World Daemon

[Service]
ExecStart = /usr/bin/dotnet /home/my username/Desktop/publish/SimpleApp.dll
Restart = on-failure

[Install]
WantedBy = multi-user.target

So essentially I have a description, and I setted in ExecStart the path of my dotnet installation and the path of the application.

Later I have an Install that if understood well tell to the systemctl that the service can run for each user, right?

Later, I copied the service in the system folder:

sudo cp console.service /lib/systemd/system

and I enabled it:

sudo systemctl daemon-reload 
sudo systemctl enable console.service

so, I executed the service:

sudo systemctl start console.service

and when I print the status:

systemctl status console.service

will is displayed this:

enter image description here

the problem's that inside the folder publish (the path of the application specified in ExecStart) I doesn't have any log.txt at this time.

Why?


Solution

  • Since you only specified a relative path for the file, it will be created in the working directory of the service.

    You can either change "log.txt" to a full path or set the working directory in the .service file:

    [Service]
    WorkingDirectory=/path/you/want/the/file/to/be/in
    …