Search code examples
c#linuxioazure-functions

Permission denied on executable in Azure Functions (C#, Linux)


I am trying to run packed to a single file ffmpeg on Linux on Azure function:

 var ffmpeg = $"/home/site/wwwroot/ffmpeg";
 var videoPath = Path.GetFullPath($"SampleVideo1.mp4");
 var ffmpegCommand = $"{ffmpeg} -i \"{videoPath}\"";

 using Process process = new();
 process.StartInfo.FileName = "bash";
 process.StartInfo.Arguments = $"-c \"{ffmpegCommand}\"";
 process.StartInfo.UseShellExecute = false;
 process.StartInfo.CreateNoWindow = true;
 process.StartInfo.RedirectStandardOutput = true;
 process.StartInfo.RedirectStandardError = true;

 process.Start();
 var output = await process.StandardOutput.ReadToEndAsync();
 var error = await process.StandardError.ReadToEndAsync();

 await process.WaitForExitAsync();

This should be a generic solution to be able to get output from any executable, not only ffmpeg.

Problems I encountered:

  1. Using script/executable directly (without bash) with UseShellExecute = false causes permission denied error (Using Process.Start() says permission denied)
  2. Using script/executable directly with UseShellExecute = true causes the impossibility of fetching output/error data from process.

What I need is to be able to run a script/executable and be able to get it's output. I found a solution that I can use bash without permission denied, run my aim file with -c parameter, UseShellExecute = false and I will be able to get it's output.

As side solution I am trying to run bash with UseShellExecute = true and trying to save output to a file with -c "commandhere > somefile.txt.

In both cases I get an error:

bash: line 1: /home/site/wwwroot/ffmpeg: Permission denied\n

I tried combinations mentioned before, also I tried

 var chmodStartInfo = new ProcessStartInfo
 {
     FileName = "chmod",
     Arguments = $"+x {ffmpegPath}",
     RedirectStandardOutput = false,
     RedirectStandardError = false,
     UseShellExecute = true,
     CreateNoWindow = true
 };

 using (var chmodProcess = new Process { StartInfo = chmodStartInfo })
 {
     chmodProcess.Start();
     await chmodProcess.WaitForExitAsync();
 }

before the bash process, but it results with same permission denied problem.


Solution

  • Turns out this is a common problem on building app in Pipelines in Azure Devops. Files did not have +x permission, after we added chmod +x to particular files as deployment stages, it helped.