HomeGenie Forum

Automation Program Plugins and Wizard Scripting => Help => Topic started by: Jens on September 14, 2014, 12:32:56 PM

Title: How to execute a shell command?
Post by: Jens on September 14, 2014, 12:32:56 PM
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
 

Title: Re: How to execute a shell command?
Post by: flybob on September 15, 2014, 09:29:56 PM
I just found out while coding a script execution, this is for C#:

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

Enjoy!
Title: Re: How to execute a shell command?
Post by: Gene on September 16, 2014, 12:26:56 AM
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.
Title: Re: How to execute a shell command?
Post by: Jens on September 18, 2014, 07:53:24 PM
Thanks a lot - works

Regards
Jens
Title: Re: How to execute a shell command?
Post by: KaZe on January 10, 2016, 08:58:33 PM
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?
Title: Re: How to execute a shell command?
Post by: Jens on June 12, 2016, 12:25:03 PM
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


Title: Re: How to execute a shell command?
Post by: KaZe on June 12, 2016, 08:33:32 PM
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 (http://www.homegenie.it/forum/index.php?topic=1186.msg8558#msg8558)
Title: Re: How to execute a shell command?
Post by: Jens on June 25, 2016, 08:46:53 PM
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

Title: Re: How to execute a shell command?
Post by: nolio on July 19, 2016, 10:13:53 PM
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