more G-Labs products

Author Topic: How to execute a shell command?  (Read 3896 times)

September 14, 2014, 12:32:56 PM
Read 3896 times

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211
Hello,

is there a way to execute a shell command in CSharp? I was searching on the net already but every code snippet I found was not working and generating lots of errors.

Does anybody have a working example on how to execute e.g. "ls -la"?

Thanks for posting

regards
Jens
 

« Last Edit: September 14, 2014, 12:46:27 PM by Jens »

September 15, 2014, 09:29:56 PM
Reply #1

flybob

  • *
  • Information
  • Newbie
  • Posts: 15
I just found out while coding a script execution, this is for C#:

System.Diagnostics.Process.Start("mycommand", "arg1 arg2");

Enjoy!

September 16, 2014, 12:26:56 AM
Reply #2

Gene

  • *****
  • Information
  • Administrator
  • Posts: 1472
  • Tangible is the future!
    • Yet Another Programmer
If you also want to read the command output:

Code: [Select]

var proc = new System.Diagnostics.Process {
    StartInfo = new System.Diagnostics.ProcessStartInfo {
        FileName = "ls",
        Arguments = "-la",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};
proc.Start();
while (!proc.StandardOutput.EndOfStream) {
    string line = proc.StandardOutput.ReadLine();
    //
    Program.Notify("SHELL COMMAND", line);
    Pause(1);
}


g.

September 18, 2014, 07:53:24 PM
Reply #3

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211
Thanks a lot - works

Regards
Jens

January 10, 2016, 08:58:33 PM
Reply #4

KaZe

  • ****
  • Information
  • Sr. Member
  • Posts: 219
Hi Gene!

I'd like to run this shell script. In linux command line works fine.
Code: [Select]
free | awk '{ print $1,$2 }' | grep Mem: | cut -d' ' -f2It is give me back the total memory size.

I try run in HG. But I just can run from bash file.
Bash file (totalmem.sh) contain:
Code: [Select]
free | awk '{ print $1,$2 }' | grep Mem: | cut -d' ' -f2
The c# program code:
Code: [Select]
var proc = new System.Diagnostics.Process {
    StartInfo = new System.Diagnostics.ProcessStartInfo {
        FileName = "bash",
        Arguments = "/usr/local/bin/homegenie/totalmem.sh",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true
    }
};
proc.Start();
while (!proc.StandardOutput.EndOfStream) {
    string line = proc.StandardOutput.ReadLine();
    //
    Program.Notify("Total Memory", line);
    Pause(1);
}
It's works. HG is show memory size in notify. But I'd like to run this command (free | awk '{ print $1,$2 }' | grep Mem: | cut -d' ' -f2) directy from C# code  without bash file (totalmem.sh)

Is that possible?
« Last Edit: January 11, 2016, 12:37:30 PM by KaZe »

June 12, 2016, 12:25:03 PM
Reply #5

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211
Hello,

anybody any idea, why this code

     var proc = new System.Diagnostics.Process {
      StartInfo = new System.Diagnostics.ProcessStartInfo {
        FileName = "bash",
        Arguments = "/home/pi/scripttoexecute.sh",
        UseShellExecute = false,
        RedirectStandardOutput = true,
        CreateNoWindow = true

as part of the code snippet by KaZE changes the rights of /dev/null?

Before executing the code the settings are correct

crw-rw-rw- 1 root root 1, 3 Jan  1  1970 /dev/null

after they are not and it has something to do with using FileName = "bash"

crw------- 1 root root 1, 3 Jan  1  1970 /dev/null

This causes trouble with other scripts

Many thanks
Regards

Jens



June 12, 2016, 08:33:32 PM
Reply #6

KaZe

  • ****
  • Information
  • Sr. Member
  • Posts: 219
Hi!
Look at my HomeGeni Load (Linux) program this topic. Last comment. It's works for me.
http://www.homegenie.it/forum/index.php?topic=1186.msg8558#msg8558

June 25, 2016, 08:46:53 PM
Reply #7

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211
Thanks a lot for your post, but the basics aren't the problem.

My script contains some /dev/null stuff like

script.sh:
...command 2>&1 1>/dev/null

When I execute the script as user pi everything is fine, runs fine and no rights change on /dev/null

But when I execute this from homegenie it changes

StartInfo = new System.Diagnostics.ProcessStartInfo {
        FileName = "bash",
        Arguments = "/home/pi/script.sh",


I think I found the root cause, homegenie bash executes the given arguments as root, this happens, too, if I execute it as root from commandline.

Any idea to make homegenie execute the script as user pi and not root?

Thanks
Jens


July 19, 2016, 10:13:53 PM
Reply #8

nolio

  • *****
  • Information
  • Global Moderator
  • Posts: 544
Hi,

Did you try with something like that ?
Code: [Select]
StartInfo = new System.Diagnostics.ProcessStartInfo {
        FileName = "sudo -H -u pi bash",
        Arguments = "/home/pi/script.sh",

Bye