Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Net;
- using System.Net.Sockets;
- using System.Threading;
- using System.IO;
- using System.Diagnostics;
- using System.ComponentModel;
- namespace restarter
- {
- class Program
- {
- static void Main()
- {
- loadConfig();
- // First interval = 1000ms; subsequent intervals = 15mins
- Timer timer = new Timer(Tick, "Time to check", 1000, 900000);
- Console.ReadLine();
- timer.Dispose();
- }
- static void Tick(object data)
- {
- Console.WriteLine(data);
- checkifdown();
- }
- static String startupFile;
- static IPAddress address;
- static int port;
- static void loadConfig()
- {
- try
- {
- // Create an instance of StreamReader to read from a file.
- // The using statement also closes the StreamReader.
- using (StreamReader sr = new StreamReader("config.txt"))
- {
- String line;
- // Read and display lines from the file until the end of
- // the file is reached.
- while ((line = sr.ReadLine()) != null)
- {
- if (line.StartsWith("serverIP="))
- address = IPAddress.Parse(line.Substring(9));
- if (line.StartsWith("port="))
- port = Int32.Parse(line.Substring(5));
- if(line.StartsWith("run="))
- startupFile = line.Substring(4);
- }
- }
- }
- catch (Exception e)
- {
- // Let the user know what went wrong.
- //Console.WriteLine("The file could not be read:");
- Console.WriteLine(e.Message);
- }
- }
- static void checkifdown()
- {
- TcpClient client;
- try
- {
- client = new TcpClient();
- client.Connect(new IPEndPoint(address, port));
- byte[] garbagehandshake = { 0, 9, 0, 80, 0, 108, 0, 97, 0, 121, 0, 101, 0, 114, 0, 57, 0, 51, 0, 54 };
- NetworkStream stream = client.GetStream();
- stream.Write(garbagehandshake, 0, garbagehandshake.Length);
- Console.WriteLine("Server seems to be up! ;D");
- //were not expecting a responce as it will disconnect us with a protocol error
- client.Close();
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- Console.WriteLine("Server down Q_Q.. restating it");
- foreach (Process p in System.Diagnostics.Process.GetProcessesByName("java"))
- {
- try
- {
- p.Kill();
- p.WaitForExit(); // possibly with a timeout
- }
- catch (Exception e2)
- {
- }
- }
- foreach (Process p in System.Diagnostics.Process.GetProcessesByName("javaw"))
- {
- try
- {
- p.Kill();
- p.WaitForExit(); // possibly with a timeout
- }
- catch (Exception e2)
- {
- }
- }
- Console.WriteLine("Launching server now");
- Console.WriteLine(startupFile);
- System.Diagnostics.Process.Start(startupFile);
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment