C# 进程不在Ubuntu上工作,但在Windows上工作
我有以下代码,可通过windows和linux的命令行运行csv导入:C# 进程不在Ubuntu上工作,但在Windows上工作,c#,asp.net-core,.net-core,C#,Asp.net Core,.net Core,我有以下代码,可通过windows和linux的命令行运行csv导入: var isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux); ProcessStartInfo startInfo = null; if (isLinux) { startInfo = new ProcessStartInfo { FileName = @"/bin/bash", Arguments = $"
var isLinux = RuntimeInformation.IsOSPlatform(OSPlatform.Linux);
ProcessStartInfo startInfo = null;
if (isLinux)
{
startInfo = new ProcessStartInfo
{
FileName = @"/bin/bash",
Arguments = $"-c 'cat \"{filePath}\" | psql -h 127.0.0.1 -U {user} -d {dbname} -w -c \"copy data_temp from stdin csv header\"' ",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
};
}
else
{
startInfo = new ProcessStartInfo
{
FileName = @"cmd.exe",
Arguments = $"/c cat \"{filePath}\" | psql -h 127.0.0.1 -U {user} -d {dbname} -w -c \"copy data_temp from stdin csv header\" ",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
};
}
using (var process = new Process { StartInfo = startInfo})
{
process.Start();
string result = process.StandardOutput.ReadToEnd();
process.WaitForExit();
}
它在Windows上运行良好,但在Linux(Ubuntu18.04)上,结果始终为空,导入不起作用
我曾尝试在终端中自己编写查询:
/bin/bash -c 'cat "/path/file.csv" | psql -h 127.0.0.1 -U user -d user -w -c "copy data_temp from stdin csv header"'
它工作正常,但从我的代码运行时,它只返回空字符串,不导入
我做错了什么
编辑:我觉得这可能是一个权限问题。这段代码在我的asp.net核心web应用程序中,因此它将作为用户www-data
运行www-data
设置为nologin,因此这可能会导致问题。不确定解决方案的问题是单引号不起作用。我改成了双引号,效果很好