【问题标题】:C# execute a terminal command in linuxC#在linux中执行终端命令
【发布时间】:2014-07-03 23:05:51
【问题描述】:

我希望我的 c# 应用程序(我在树莓派上执行)在启动时运行 bash 脚本..
基本上:脚本位于/etc/init.d 并命名为mnw。我希望每当我的 c# 应用程序启动时,它都应该执行 mnw 脚本的一部分。
如果它是在终端中写的,它看起来像:

cd /etc/init.d
./mnw stop

我希望这发生在public static void Main() 的开头,我一直在尝试

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

但它说 stop 是一个意想不到的论点,有什么想法吗?

【问题讨论】:

  • 一个明显的错误 - FileName = "/dev/init.d/./mnw" 应该是 FileName = "/etc/init.d/mnw",但我认为这是粘贴此问题时的拼写错误。
  • 为什么不创建 bash 脚本呢?
  • 这是 mnw if [ "$1" = "start" ] then start-stop-daemon --start --background -m --oknodo --pidfile /home/pi/p$ elif [ "$1" = "startnbg" ] 然后 start-stop-daemon --start -m --oknodo --pidfile /home/pi/pid/MonoDaemon$ elif [ "$1" = "stop" ] 然后 start-stop-daemon --stop --retry=TERM/30/KILL/5 --pidfile /home/pi/pid/$ else echo Unknown command fi
  • 无需论证ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "/etc/init.d/mnw stop"}; 或尝试 ProcessStartInfo startInfo = new ProcessStartInfo() { FileName = "/bin/bash", Arguments = "/etc/init.d/mnw stop", };

标签: c# linux bash terminal


【解决方案1】:

我从未在 Mono / Linux 上使用过ProcessStartInfo,但您尝试过通过 bash 调用吗?

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

另外,mnw 上的可执行位没有问题?

【讨论】:

  • 评论说的一样:)
  • 啊,完全错过了,那个,一个小时前开始写,忘记按add:)
猜你喜欢
  • 1970-01-01
  • 2015-08-16
  • 2021-06-11
  • 1970-01-01
  • 2022-01-06
  • 1970-01-01
  • 2011-05-09
  • 1970-01-01
  • 2015-05-21
相关资源
最近更新 更多