Module svcmon11
Public svcmon As New Process ' This is located outside of any methods because it is shared across the entire code
Sub Main()
Console.Title = "IRCServices Monitor v1.1"
Console.Out.WriteLine(Date.Now & " - IRCServices Monitor v1.1 starting up...")
Console.Out.Write(Date.Now & " - [INFO] Detecting IRCServices directory: ")
ChDir("C:\services") ' This will throw an error if dir doesn't exist.
Console.Out.WriteLine("C:\services")
Console.Out.Write(Date.Now & " - [INFO] Detecting name of binary: ")
Console.Out.WriteLine("ircservices.exe") ' This is pretty much standard. Nothing else is accepted.
Console.Out.WriteLine(Date.Now & " - [INFO] Command line options: -dir='lib' -nofork -noexpire")
Console.Out.WriteLine(Date.Now & " - [INFO] Executing: ircservices.exe -dir='lib' -nofork -noexpire")
SetProcessInfo() ' Next step, anyone? Let's get this process stuff set up!
End Sub
Sub SetProcessInfo()
svcmon.StartInfo.CreateNoWindow = True ' Don't allow a new window to be spawned
svcmon.StartInfo.Arguments = "-dir='lib' -nofork -noexpire" ' Set command line arguments (duh)
svcmon.StartInfo.FileName = "C:\services\ircservices.exe" ' Set filename to be called (...duh)
svcmon.StartInfo.WorkingDirectory = "C:\services" ' Set relative directory (so -dir='lib' works)
svcmon.StartInfo.UseShellExecute = False ' Don't use explorer.exe as parent; start under svcmonitor.exe
StartProc() ' Now that we've set up our info, let's start the process!
End Sub
Sub StartProc()
svcmon.Start()
Console.Title = "IRCServices Monitor v1.1 (active PID: " & svcmon.Id & ")"
Console.Out.WriteLine(Date.Now & " - [INFO] Process monitoring (re)started for PID " & svcmon.Id)
svcmon.WaitForExit() ' Wait infinitely for the process to exit...then...
If svcmon.ExitCode = 1536 Then ' If /operserv restart was issued...
Console.Out.WriteLine(Date.Now & " - [INFO] RESTART command detected")
StartProc()
ElseIf svcmon.ExitCode = 1 Then ' If IRCServices failed to start for any reason...
Console.Out.WriteLine(Date.Now & " - [ERROR] IRCServices encountered an error and failed to start!")
System.Threading.Thread.Sleep(0) ' Manual restart is required
Else ' Some other exit code (most likely 0, which would indicate a normal shutdown/termination)...
Console.Out.WriteLine(Date.Now & " - [INFO] SHUTDOWN command detected.")
System.Threading.Thread.Sleep(0) ' Manual restart is required
End If
End Sub
End Module