How to start a service in C# on Linux

会有一股神秘感。 提交于 2020-01-05 04:52:07

问题


I would like to start a service on my Linux server using a C# console application, through Mono.

public static void StartService(string serviceName, int timeoutMilliseconds)
{
  ServiceController service = new ServiceController(serviceName);
  try
  {
    TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds);

    service.Start();
    service.WaitForStatus(ServiceControllerStatus.Running, timeout);
  }
  catch
  {
    // ...
  }
}

Would this work?

As an alternative, is there a way to send a command to Linux through C# like you can send it on Windows systems?

I'm trying to start a Linux service using a C# Executable.


回答1:


You can execute a command by doing this;

Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "/bin/bash";
proc.StartInfo.Arguments = "-c 'your command here'";
proc.StartInfo.UseShellExecute = false; 
proc.StartInfo.RedirectStandardOutput = true;
proc.Start();


来源:https://stackoverflow.com/questions/22918711/how-to-start-a-service-in-c-sharp-on-linux

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!