1

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.

Karim
  • 43
  • 5
  • 2
    http://mattdeboard.net/2012/10/19/how-to-run-windows-service-as-linux-daemon/ and http://linux.die.net/man/1/mono-service – Ben Voigt Apr 07 '14 at 17:11
  • possible duplicate of [using mono-service to wrap a windows service on linux](http://stackoverflow.com/questions/351971/using-mono-service-to-wrap-a-windows-service-on-linux) – Lex Li Apr 08 '14 at 00:02

1 Answers1

2

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();
Reousa Asteron
  • 519
  • 1
  • 3
  • 18