'Self Contained .NET Core console app as a service in Linux

I am creating a simple console application in .NET Core that needs to run as a service on Linux

The application is compiled with the following command:

dotnet publish -c release -r linux-x64 --self-contained

And then run it without problem from the terminal with:

cd publish

./MyAppName

Now I am trying to register it as a service by making its definition file as follows

[Unit]
Description=Foo Bar Service
[Service]
WorkingDirectory=/home/Desktop/publish
ExecStart=./home/Desktop/publish/MyAppName
Restart=always
RestartSec=10
SyslogIdentifier=dotnet-postservice
User=myuser
Environment=ASPNETCORE_ENVIRONMENT=Production

[Install]
WantedBy=multi-user.target

then register the service with

sudo systemctl enable myappname.service

but when I check the status it gives me the following error

Unit myappname.service has a bad unit file setting.

Neither a valid executable name nor an absolute path: ./home/Desktop/publish/MyAppName

Unit configuration has a faltal error, unit will not be started

If there is a solution where I don't have to install dotnet I would greatly appreciate it.

Thanks



Solution 1:[1]

You're setting your working directory to

/home/Desktop/publish

Then you're doing

ExecStart=./home/Desktop/publish/MyAppName

Which means the command being run would be

/home/Desktop/publish/home/Desktop/publish/MyAppName

Take out the ./home/Desktop/publish on your ExecStart portion of the config and it should work.

Alternatively, drop the WorkingDirectory config and just give an absolute path to your executable like /home/Desktop/publish/MyAppName (notice it doesn't start with ./ and instead is just /.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Zach Sexton