Process not working on Ubuntu but works on Windows

Viewed 173

I have this code that runs a csv import via the command line for windows and linux:

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

It works fine on Windows but on Linux (Ubuntu 18.04) the result is always blank and the import doesn't work.

I have tried writing query myself in terminal:

 /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"'

And it works fine but when run from my code it just returns empty string and doesn't import.

What am I doing wrong here?

Edit: It occurs to me that it could be a permission issue. This code is inside my asp.net core web application so it will be run as user www-data I think. www-data is set to nologin so this might be causing issue. Not sure of solution

1 Answers

Issue was that single quotes do not work. I changed to double quotes and it worked.

Related