30

What would be the best approach to migrate a .NET Windows Service to Linux using mono? I've been trying to avoid executing the application as a scheduled command.

Is it possible to obtain a service/system daemon(in linux) like behavior?

Raúl Roa
  • 12,061
  • 13
  • 49
  • 64

3 Answers3

34

Under Linux, deamons are simple background processes. No special control methods (e.g start(), stop()) are used as in Windows. Build your service as a simple (console) application, and run it in the background. Use a tool like daemonize to run a program as a Unix daemon, and remember to specify mono as the program to be activated.

As noted by others, mono-service is a host to run services built with the ServiceProcess assembly. Services built for Windows can use this method to run unmodified under Linux. You can control the service by sending signals to the process (see man page).

gimel
  • 83,368
  • 10
  • 76
  • 104
  • No special control methods? Doesn't it contradicts the very service definition and sense? Start and stop are not special methods, these are the most basic methods. Daemons and services are synonyms, they do exactly the same in completely the same manner. Practically all implement start and stop methods, many of them are started and stopped by OS on startup and shutdown sequence. It's not true the Linux lacks this capability Windows has. The systems are more similar than most people think. Also with .NET 5.0 you don't have to rely on ServiceProcess anymore. – Harry Apr 01 '21 at 10:57
3

Can you use mono-service to wrap it?

See this question.

Community
  • 1
  • 1
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
2

The way I have done it in the past is to compile the .Net application as a console application and then on the Linux server create a startup script in the initscripts directory.

Linux obviously does not have Windows services and the daemons that are initiated from the rc.d directories on startup are its equivalent. All most of the rc.d scripts do is start the different applications on a background thread so there's nothing really complicated to it. The only bit of extra work is that you will need to write a Linux shell script to start and if you want stop the service.

sipsorcery
  • 30,273
  • 24
  • 104
  • 155