Home > Mobile >  Running a Linux executable via Process.Start gives: Exec format error
Running a Linux executable via Process.Start gives: Exec format error

Time:06-01

I have a small C#/.NET6.0 commandline application that should run a certain linux command via System.Diagnostics.Process.

var processStartInfo = new ProcessStartInfo
{
    FileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), @"Compressonator/compressonatorcli_linux_x86_64_4.2.5150/compressonatorcli"),
    ArgumentList = { "-fd", compression, "-noprogress", fileName, outputFilename },
    RedirectStandardOutput = true,
    RedirectStandardError = true,
    UseShellExecute = false
};

var process = Process.Start(processStartInfo);
Console.WriteLine(process.StandardOutput.ReadToEnd());
Console.WriteLine(process.StandardError.ReadToEnd());
process.WaitForExit();

However this fails with the following exception on an Ubuntu 64bit running in a VirtualMachine:

System.ComponentModel.Win32Exception (8): An error occurred trying to start process '/media/sf_imageconverter/net6.0/Compressonator/compressonatorcli_linux_x86_64_4.2.5150/compressonatorcli' with working directory '/media/sf_imageconverter/net6.0'. Exec format error 
   at System.Diagnostics.Process.ForkAndExecProcess(ProcessStartInfo startInfo, String resolvedFilename, String[] argv, String[] envp, String cwd, Boolean setCredentials, UInt32 userId, UInt32 groupId, UInt32[] groups, Int32& stdinFd, Int32& stdoutFd, Int32& stderrFd, Boolean usesTerminal, Boolean throwOnNoExec)
   at System.Diagnostics.Process.StartCore(ProcessStartInfo startInfo)
   at System.Diagnostics.Process.Start()
   at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)

The strange thing is, that when running the process directly from a terminal on the same machine it works fine:

$/media/sf_imageconverter/net6.0/Compressonator/compressonatorcli_linux_x86_64_4.2.5150/compressonatorcli

The process is https://github.com/GPUOpen-Tools/compressonator/releases/tag/V4.2.5185

My .NET6.0 app is set to compile to AnyCPU but I have also tried the other options without success.

CodePudding user response:

This error means that you are not launching an executable but a script. If you open the script in a text editor, you can see that the second line contains this code

#!/bin/bash

This is called a shebang and specifies what executable is used to run this script. If your system does not have /bin/bash you will get this error message.

Either make sure that bash is available or change the line to the shell you are using to execute the script directly.

  • Related