.Net Core Process.Start on Linux

24

我在Linux上运行我的.Net Core应用程序时遇到以下问题。

这是异常:

System.ComponentModel.Win32Exception (0x80004005): Permission denied
   at Interop.Sys.ForkAndExecProcess(String filename, String[] argv, String[] envp, String cwd, Boolean redirectStdin, Boolean redirectStdout, Boolean redirectStderr, Int32& lpChildPid, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd)
   at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at Ombi.Schedule.Jobs.Ombi.OmbiAutomaticUpdater.<Update>d__18.MoveNext() in C:\projects\requestplex\src\Ombi.Schedule\Jobs\Ombi\OmbiAutomaticUpdater.cs:line 218

这是代码:

var updaterFile = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
     "TempUpdate", $"Ombi.Updater");

var start = new ProcessStartInfo
{
    UseShellExecute = false,
    CreateNoWindow = true,
    FileName = updaterFile,
    Arguments = GetArgs(settings), // This just gets some command line arguments for the app i am attempting to launch
    WorkingDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "TempUpdate"),
};

using (var proc = new Process { StartInfo = start })
{
    proc.Start();
}

当我们调用.Start()时,似乎会抛出异常。

我不确定为什么会发生这种情况,文件和文件夹的权限已经设置为777。


1
这是一个自包含的部署。 - Jamie Rees
3
如果有人需要帮助,可以参考此帖子最后一个回答 https://dev59.com/PlcO5IYBdhLWcg3w3VLU#47918132 - Yitzak Hernandez
你的.NET代码看起来很好。我建议你先使用一些已知的内置工具,比如“/bin/ping google.com”来测试你的代码,以确认问题与ACL有关。此外,请检查Ombi树中更高级别的所有父目录。更新程序具有执行权限。你可以在Unix&LinuxAsk Ubuntu StackExchange网站上寻找更多信息。 - Vitaliy Shibaev
也许上传一个tarball? - kettle
你有检查过你的SELinux或者你发行版上的其他安全机制吗?为了测试,先关闭它,看看程序是否能正常运行。 - NewRK
2个回答

1
多年后,我遇到了同样的错误,并不喜欢制作.dll可执行文件的方法。相反,将FileName设置为“dotnet”,并将dll路径作为第一个参数传递即可解决问题。
var updaterFile = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location),
     "TempUpdate", $"Ombi.Updater");

var start = new ProcessStartInfo
{
    UseShellExecute = false,
    CreateNoWindow = true,
    FileName = "dotnet", // dotnet as executable
    Arguments = updaterFile + " " +GetArgs(settings),  // first argument as dll filepath
    WorkingDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "TempUpdate"),
};

using (var proc = new Process { StartInfo = start })
{
    proc.Start();
}

2
我猜这只能在机器上安装了dotnet的情况下才能运行,如果这是一个自包含的应用程序,那么这将无法工作。 - Jamie Rees
那是我的愚蠢。你说得完全正确。@JamieRees - Haldun

0
一个好的诊断工具是看你是否可以直接从Shell(如果你在Windows上使用WSL)执行文件。
从Windows的角度来看,很容易忘记在linux上需要将可执行文件标记为可执行。如果你自己无法执行它,chmod 775 /your/file可能会有所帮助。

在执行了chmod命令之后,我可以手动执行该文件,但仍无法通过Process.Start执行。我尝试使用strace进行调查,但并没有帮助,最终放弃了。您可以在此处查看strace输出:https://github.com/JeroenBos/JBSnorro.LayoutEngine/issues/17 - JBSnorro

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接