C# execute a terminal command in linux
I have never used ProcessStartInfo
on Mono / Linux, but have you tried calling via bash?
ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "/bin/bash", Arguments = "/dev/init.d/mnw stop", };
Process proc = new Process() { StartInfo = startInfo, };
proc.Start();
Also, there is no issues with the executable bit on mnw
?
how to execute a terminal command in c#
You should probably try setting the full path the executable.
proc.StartInfo.FileName = "C:/SOMEPATH/Bash.exe";
I'm assuming as you are specifying a relative path, it's not resolving it. Possibly because you aren't setting a working directory for the process so it's current dir and the current dir you think it has, are different.
How to execute Linux command line in C# Mono
You can only use the <
, >
and |
operators inside a shell. The shell (such as bash) is what actually parses these and performs the redirection. Try this code:
...
ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = "/usr/bin/iconv";
psi.UseShellExecute = false;
psi.Arguments = "-f unicode -t utf8 /tmp/test.txt";
psi.RedirectStandardOutput = true;
Process p = Process.Start(psi);
Console.WriteLine(p.StandardOutput.ReadToEnd());
p.WaitForExit();
p.Close();
...
I apologize if this code doesn't work properly, I personally rarely program in C#.
You may also be able to just set psi.UseShellExecute = true
. According to MSDN, this will start the program with the system shell (cmd.exe). This may work for you, although I have not tested.
Best of luck!
Exectute a Linux Shell command from ASP.NET Core 3 app
Assume you want to run echo hello
in bash, then
Process process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = "bash",
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false
}
};
process.Start();
await process.StandardInput.WriteLineAsync("echo hello");
var output = await process.StandardOutput.ReadLineAsync();
Console.WriteLine(output);
How to open Ubuntu's bash.exe shell using C# on Windows?
You could pass everything in the command line instead of piping into the process. I think it would save you a lot of trouble. Try bash.exe -c "cd /mnt/c/Users/shho3/Desktop; python some_script.py arg1 arg2"
:
Process.Start("bash.exe", "-c \"cd /mnt/c/Users/shho3/Desktop; python some_script.py arg1 arg2\"").WaitForExit()
Alternatively you can also just set the working directory to C:\Users\shho3\Desktop
(instead of C:\Windows\System32
) and call bash.exe -c "python some_script.py arg1 arg2"
, then you don't even have to convert the path:
Process.Start(new ProcessStartInfo("bash.exe", "-c \"python some_script.py arg1 arg2\"") {
WorkingDirectory = "C:\\Users\\ssho3\\Desktop"
}).WaitForExit()
How to chain Linux commands in .net 5
Found the answer
Process process = new Process();
process.StartInfo.FileName = "/bin/sh";
string cmd = "cat /proc/meminfo | grep MemTotal";
process.StartInfo.Arguments = $"-c \"{cmd}\"";
process.StartInfo.RedirectStandardOutput = true;
process.Start();
process.WaitForExit();
Console.WriteLine(process.StandardOutput.ReadToEnd());
How to execute a linux shell script from a C# application?
After doing some google search, I found a way to do this. All I had to do is change "--login -c"
to "--login -i"
ProcessStartInfo psi = new ProcessStartInfo(@"C:\cygwin64\bin\bash.exe");
psi.Arguments = "--login -i E://Work/sync.sh E://Work/Voice/Temp";
Process p = Process.Start(psi);
Related Topics
Why Can't I Use the 'Await' Operator Within the Body of a Lock Statement
Why Is Httpclient Baseaddress Not Working
How to Check If a Type Is a Subtype or the Type of an Object
How to Convert a Stream into a Byte[] in C#
Update Row If It Exists Else Insert Logic with Entity Framework
What Difference Does .Asnotracking() Make
What's the Difference Between Dynamic (C# 4) and Var
How to Specify the Exit Code of a Console Application in .Net
Entity Framework Async Operation Takes Ten Times as Long to Complete
How to Get a List of Users from Active Directory
How to Convert Seconds into (Hour:Minutes:Seconds:Milliseconds) Time
Resize Image Proportionally with Maxheight and Maxwidth Constraints