如何在Linux上的C#中启动服务 [英] How to start a service in C# on Linux

查看:702
本文介绍了如何在Linux上的C#中启动服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过Mono通过C#控制台应用程序在Linux服务器上启动服务.

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
  {
    // ...
  }
}

这项工作可以吗?

作为替代方案,是否可以像在Windows系统上一样通过C#向Linux发送命令?

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

我正在尝试使用C#可执行文件启动Linux服务.

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

推荐答案

您可以通过执行此命令来实现;

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();

这篇关于如何在Linux上的C#中启动服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆