Daemon monitor Windows Services Status

Source: Internet
Author: User
Namespace microshaoft {using system; using system. timers; using system. management; using system. serviceprocess; using system. diagnostics; using system. componentmodel; using system. collections. generic; using system. security. principal; using system. configuration. install; using microshaoft; public class simpleservice: servicebase // inherits from servicebase {public const string servicename = "servicesdaemon "; Private timer _ timer; public static void main (string [] ARGs) {simpleservice x = new simpleservice (); int L = 0; If (ARGs! = NULL) {L = args. length;} If (L> 0) // run {console in Console mode when parameters exist. title = "servicename"; console. writeline ("Run as console"); X. onstart (null); console. readline ();} else // After intallutil is set to a service // when no parameter is set, run {console in service mode. writeline ("Run as service"); servicebase. run (x) ;}} public simpleservice () {canpauseandcontinue = true; servicename = simpleservice. servicename;} protected override void onstart (string [] ARGs) {console. writeline (". net version: {0} ", environment. version. tostring (); console. writeline ("Current identity: {0}", windowsidentity. getcurrent (). name); console. writeline ("{0} started, at {1}", simpleservice. servicename, datetime. now. tostring ("yyyy-mm-dd hh: mm: Ss. SS "); _ timer = new timer (); _ timer. interval = 60*1000; _ timer. elapsed + = new elapsedeventhandler (_ timer_elapsed); _ timer. start (); _ timer_elapsed (null, null); // write your Program } Void _ timer_elapsed (Object sender, elapsedeventargs e) {_ timer. stop (); try {process ();} catch {} finally {_ timer. start () ;}} private void process () {managementobjectsearcher searcher = new managementobjectsearcher ("Root \ cimv2", "select * From win32_service where name like 'Hello % '"); managementobjectcollection MOC = searcher. get (); foreach (managementobject Mo in MoC) {string logmessa GE; string service = NULL; try {service = Mo ["name"]. tostring (); If (Mo ["state"]. tostring (). tolower ()! = "Running") {logmessage = string. format ("host [{0}], [{1}], [{2}] serving [{3}] Not running", environment. machinename, service, Mo ["startmode"]. tostring (), datetime. now); eventloghelper. writeeventlogentry (this. servicename + "log", service + "Source", logmessage, eventlogentrytype. error); If (Mo ["startmode"]. tostring (). tolower () = "Auto") {servicecontroller controller = new servicecontroller (service ); If (controller. status! = Servicecontrollerstatus. running) {controller. start ();} logmessage = string. format ("host [{0}], [{1}], [{2}] serving [{3}] successfully restarted", environment. machinename, service, Mo ["startmode"]. tostring (), datetime. now); eventloghelper. writeeventlogentry (this. servicename + "log", service + "Source", logmessage, eventlogentrytype. information) ;}} catch (managementexception me) {logmessage = string. forma T ("host [{0}], [{1}], [{2}] Service Query [{3}] exception [{4}], in [{5}] ", environment. machinename, service, Mo ["startmode"]. tostring (), "managementexception", me. tostring (), datetime. now); eventloghelper. writeeventlogentry (this. servicename + "log", service + "Source", logmessage, eventlogentrytype. error);} catch (exception e) {logmessage = string. format ("host [{0}], [{1}], [{2}] Service Query [{3}] exception [{4}], in [{5}] ", Environment. machinename, service, Mo ["startmode"]. tostring (), "exception", E. tostring (), datetime. now); eventloghelper. writeeventlogentry (this. servicename + "log", service + "Source", logmessage, eventlogentrytype. error) ;}}}// the project installer [runinstallerattribute (true)] public class projectinstaller: installer {private serviceinstaller; private Serviceprocessinstaller processinstaller; Public projectinstaller () {processinstaller = new serviceprocessinstaller (); serviceinstaller = new serviceinstaller (); // service will run under SYSTEM account processinstaller. account = serviceaccount. localSystem; // service will have start type of manual serviceinstaller. starttype = servicestartmode. manual; serviceinstaller. servicename = simpleservice. Servicename; installers. add (serviceinstaller); installers. add (processinstaller) ;}}namespace microshaoft {using system; using system. diagnostics; Class eventloghelper {public static void writeeventlogentry (string LOGNAME, string sourcename, string logmessage, eventlogentrytype logentrytype) {If (! EventLog. sourceexists (sourcename) {EventLog. createeventsource (sourcename, LOGNAME);} EventLog log = new EventLog (); log. source = sourcename; log. writeentry (logmessage, logentrytype );}}}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.