小编IX9*_*X92的帖子

如何从 Linux 上运行的 .NET Core 应用程序关闭计算机

我有一个在 Linux (Ubuntu Server 16.04 LTS) 上运行的 .net core 2.0 程序。

我尝试通过使用以下命令调用进程来关闭计算机:sudo shutdown -h now,但是当程序作为守护程序服务在后台运行时,关闭进程不起作用。

这是代码:

var process = new Process
{
    StartInfo =
    {
        CreateNoWindow = true,
        RedirectStandardError = true,
        RedirectStandardInput = true,
        RedirectStandardOutput = true,
        UseShellExecute = false,
        FileName = Environment.GetEnvironmentVariable("SHELL"),
        Arguments = "-s"
    },
    EnableRaisingEvents = true
};

if (process.Start())
{
    process.BeginErrorReadLine();
    process.BeginOutputReadLine();
    process.StandardInput.WriteLine("sudo shutdown -h now");
}
Run Code Online (Sandbox Code Playgroud)

我的假设是该服务作为单独的会话运行,因此它没有任何控制权。当应用程序作为 Linux 守护进程运行时,如何让应用程序关闭计算机?

c# linux daemon .net-core ubuntu-16.04

6
推荐指数
1
解决办法
2941
查看次数

标签 统计

.net-core ×1

c# ×1

daemon ×1

linux ×1

ubuntu-16.04 ×1