HomeGenie Forum
Automation Program Plugins and Wizard Scripting => Help => Topic started 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
-
I just found out while coding a script execution, this is for C#:
System.Diagnostics.Process.Start("mycommand", "arg1 arg2");
Enjoy!
-
If you also want to read the command output:
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.
-
Thanks a lot - works
Regards
Jens
-
Hi Gene!
I'd like to run this shell script. In linux command line works fine.
free | awk '{ print $1,$2 }' | grep Mem: | cut -d' ' -f2
It 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:
free | awk '{ print $1,$2 }' | grep Mem: | cut -d' ' -f2
The c# program code:
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?
-
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
-
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)
-
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
-
Hi,
Did you try with something like that ?
StartInfo = new System.Diagnostics.ProcessStartInfo {
FileName = "sudo -H -u pi bash",
Arguments = "/home/pi/script.sh",
Bye