21

I want my c# application (which I execute on a raspberry pi) to run a bash script whenever it starts..
basically : the script is located in /etc/init.d and is named mnw. I want whenever my c# application starts, it should execute a part of the mnw script.
If it was written it in the terminal would look like :

cd /etc/init.d
./mnw stop

I want this to happen right at the start of public static void Main(), I've been trying

ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "/dev/init.d/./mnw", Arguments = "stop", }; 
Process proc = new Process() { StartInfo = startInfo, };
proc.Start();

but it says that stop is a unexpected argument, any ideas?

4
  • One obvious error - FileName = "/dev/init.d/./mnw" should have been FileName = "/etc/init.d/mnw", but I assume that it's a typo while pasting in this question. Commented May 15, 2014 at 13:28
  • why not create bash script instead ? Commented May 15, 2014 at 13:35
  • this is mnw if [ "$1" = "start" ] then start-stop-daemon --start --background -m --oknodo --pidfile /home/pi/p$ elif [ "$1" = "startnbg" ] then start-stop-daemon --start -m --oknodo --pidfile /home/pi/pid/MonoDaemon$ elif [ "$1" = "stop" ] then start-stop-daemon --stop --retry=TERM/30/KILL/5 --pidfile /home/pi/pid/$ else echo Unknown command fi Commented May 15, 2014 at 14:01
  • 2
    No need of argument ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "/etc/init.d/mnw stop"}; or try ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "/bin/bash", Arguments = "/etc/init.d/mnw stop", }; Commented May 15, 2014 at 14:41

2 Answers 2

17

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?

Sign up to request clarification or add additional context in comments.

2 Comments

comment says the same :)
Ah, totally missed, that, started writing like an hour ago and forgot to press add:)
13

While using the Process class like in the accepted answer works, I' d like to add that there is a library called CliWrap that makes CLI calls like this much easier:

  • fluent API to build the calls
  • async support
  • call with custom environment variables
  • set working directory
  • ability to pipe stdout/stderr into a string
  • retrieve exit code
  • etc.

using OPs question as example:

var result = await Cli
                  .Wrap("/dev/init.d/mnw")
                  .WithArguments("stop")
                  .ExecuteBufferedAsync();

// result contains:
// -- result.StandardOutput  (string)
// -- result.StandardError   (string)
// -- result.ExitCode        (int)
// -- result.StartTime       (DateTimeOffset)
// -- result.ExitTime        (DateTimeOffset)
// -- result.RunTime         (TimeSpan)

if (result.ExitCode != 0)
    Console.WriteLine("Command failed")

Thank you @Tyrrrz for this library!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.