十年河东,十年河西,莫欺少年穷
学无止境,精益求精
在mongodb 分片集群搭建过程中,每次都需要启动多条相关指令,在服务器部署时,一旦服务器关机重启,则意味着mongodb服务器不可用,因此,有必要写一个windows服务来统一执行这些指令。
关于mongodb 集群搭建,可参考:https://www.cnblogs.com/chenwolong/p/mongod.html
程序如下:
using Newtonsoft.Json; using Newtonsoft.Json.Converters; using System; using System.Collections.Generic; using System.ComponentModel; using System.Configuration; using System.Data; using System.Diagnostics; using System.Linq; using System.Net; using System.Net.Http; using System.Text; using System.Threading.Tasks; namespace SktServer { class Program { static void Main(string[] args) { Task.Run(delegate { cmd_1(@"mongod -f D: oolmongodbmymongomongo1confconfig.conf"); }); System.Threading.Thread.Sleep(1000); Task.Run(delegate { cmd_1(@"mongod -f D: oolmongodbmymongomongo2confconfig.conf"); }); System.Threading.Thread.Sleep(1000); Task.Run(delegate { cmd_1(@"mongod -f D: oolmongodbmymongomongo3confconfig.conf"); }); System.Threading.Thread.Sleep(1000); Task.Run(delegate { cmd_1(@"mongod -f D: oolmongodbmymongomongo1confshard1.conf"); }); System.Threading.Thread.Sleep(1000); Task.Run(delegate { cmd_1(@"mongod -f D: oolmongodbmymongomongo2confshard1.conf"); }); System.Threading.Thread.Sleep(1000); Task.Run(delegate { cmd_1(@"mongod -f D: oolmongodbmymongomongo3confshard1.conf"); }); System.Threading.Thread.Sleep(1000); Task.Run(delegate { cmd_1(@"mongod -f D: oolmongodbmymongomongo1confshard2.conf"); }); System.Threading.Thread.Sleep(1000); Task.Run(delegate { cmd_1(@"mongod -f D: oolmongodbmymongomongo2confshard2.conf"); }); System.Threading.Thread.Sleep(1000); Task.Run(delegate { cmd_1(@"mongod -f D: oolmongodbmymongomongo3confshard2.conf"); }); System.Threading.Thread.Sleep(1000); Task.Run(delegate { cmd_1(@"mongod -f D: oolmongodbmymongomongo1confshard3.conf"); }); System.Threading.Thread.Sleep(1000); Task.Run(delegate { cmd_1(@"mongod -f D: oolmongodbmymongomongo2confshard3.conf"); }); System.Threading.Thread.Sleep(1000); Task.Run(delegate { cmd_1(@"mongod -f D: oolmongodbmymongomongo3confshard3.conf"); }); System.Threading.Thread.Sleep(1000); Task.Run(delegate { cmd_1(@"mongos -f D: oolmongodbmymongomongo1confmongos.conf"); }); System.Threading.Thread.Sleep(1000); Task.Run(delegate { cmd_1(@"mongos -f D: oolmongodbmymongomongo2confmongos.conf"); }); System.Threading.Thread.Sleep(1000); Task.Run(delegate { cmd_1(@"mongos -f D: oolmongodbmymongomongo3confmongos.conf"); }); System.Threading.Thread.Sleep(1000); Console.ReadKey(); } private static void cmd_1(string cmd) { var p = GetProcess(); //启动程序 p.Start(); //向cmd窗口发送输入信息 p.StandardInput.WriteLine(cmd + "&exit"); p.StandardInput.AutoFlush = true; //获取输出信息 string strOuput = p.StandardOutput.ReadToEnd(); Console.WriteLine(cmd); //等待程序执行完退出进程 p.WaitForExit(); p.Close(); Console.WriteLine(strOuput); } private static Process GetProcess() { Process p = new Process(); //设置要启动的应用程序 p.StartInfo.FileName = "cmd.exe"; //是否使用操作系统shell启动 p.StartInfo.UseShellExecute = false; // 接受来自调用程序的输入信息 p.StartInfo.RedirectStandardInput = true; //输出信息 p.StartInfo.RedirectStandardOutput = true; // 输出错误 p.StartInfo.RedirectStandardError = true; //不显示程序窗口 p.StartInfo.CreateNoWindow = true; return p; } } }
@天才卧龙的博客