我有一个在 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 守护进程运行时,如何让应用程序关闭计算机?