十年河东,十年河西,莫欺少年穷

学无止境,精益求精

在mongodb 分片集群搭建过程中,每次都需要启动多条相关指令,在服务器部署时,一旦服务器关机重启,则意味着mongodb服务器不可用,因此,有必要写一个windows服务来统一执行这些指令。

程序如下:

C# 执行mongodb 分片集群CMD指令,用于开机自启动_其它C# 执行mongodb 分片集群CMD指令,用于开机自启动_其它_02
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:\tool\mongodbmy\mongo\mongo1\conf\config.conf"); });
            System.Threading.Thread.Sleep(1000);
            Task.Run(delegate { cmd_1(@"mongod -f D:\tool\mongodbmy\mongo\mongo2\conf\config.conf"); });
            System.Threading.Thread.Sleep(1000);
            Task.Run(delegate { cmd_1(@"mongod -f D:\tool\mongodbmy\mongo\mongo3\conf\config.conf"); });
            System.Threading.Thread.Sleep(1000);

            Task.Run(delegate { cmd_1(@"mongod -f D:\tool\mongodbmy\mongo\mongo1\conf\shard1.conf"); });
            System.Threading.Thread.Sleep(1000);
            Task.Run(delegate { cmd_1(@"mongod -f D:\tool\mongodbmy\mongo\mongo2\conf\shard1.conf"); });
            System.Threading.Thread.Sleep(1000);
            Task.Run(delegate { cmd_1(@"mongod -f D:\tool\mongodbmy\mongo\mongo3\conf\shard1.conf"); });
            System.Threading.Thread.Sleep(1000);



            Task.Run(delegate { cmd_1(@"mongod -f D:\tool\mongodbmy\mongo\mongo1\conf\shard2.conf"); });
            System.Threading.Thread.Sleep(1000);
            Task.Run(delegate { cmd_1(@"mongod -f D:\tool\mongodbmy\mongo\mongo2\conf\shard2.conf"); });
            System.Threading.Thread.Sleep(1000);
            Task.Run(delegate { cmd_1(@"mongod -f D:\tool\mongodbmy\mongo\mongo3\conf\shard2.conf"); });
            System.Threading.Thread.Sleep(1000);


            Task.Run(delegate { cmd_1(@"mongod -f D:\tool\mongodbmy\mongo\mongo1\conf\shard3.conf"); });
            System.Threading.Thread.Sleep(1000);
            Task.Run(delegate { cmd_1(@"mongod -f D:\tool\mongodbmy\mongo\mongo2\conf\shard3.conf"); });
            System.Threading.Thread.Sleep(1000);
            Task.Run(delegate { cmd_1(@"mongod -f D:\tool\mongodbmy\mongo\mongo3\conf\shard3.conf"); });
            System.Threading.Thread.Sleep(1000);



            Task.Run(delegate { cmd_1(@"mongos -f D:\tool\mongodbmy\mongo\mongo1\conf\mongos.conf"); });
            System.Threading.Thread.Sleep(1000);
            Task.Run(delegate { cmd_1(@"mongos -f D:\tool\mongodbmy\mongo\mongo2\conf\mongos.conf"); });
            System.Threading.Thread.Sleep(1000);
            Task.Run(delegate { cmd_1(@"mongos -f D:\tool\mongodbmy\mongo\mongo3\conf\mongos.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;
        }


    }

}
View Code