5,696,038 members and growing! (10,859 online)
Email Password   helpLost your password?
Web Development » Site & Server Management » General     Intermediate

Detecting the UpTime of Web Server

By Vasudevan Deepak Kumar

Sometimes it would be necessary to check how long our web server has been running or if our web server is being restarted too many times. Here we discuss a simple solution for the same.
C#.NET 1.0, Win2K, WinXP, Windows, .NET, ASP.NET, Visual Studio, Dev

Posted: 5 Dec 2002
Updated: 5 Dec 2002
Views: 80,767
Bookmarked: 35 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
15 votes for this Article.
Popularity: 2.94 Rating: 2.50 out of 5
4 votes, 26.7%
1
4 votes, 26.7%
2
1 vote, 6.7%
3
4 votes, 26.7%
4
2 votes, 13.3%
5

Introduction

Occasionally, we would be in need to find how long our web server has been running and to find out whether the web server has been restarted or not, if it had experienced prolonged slowness for an extended period of time, say a couple of hours etc. In most of the hosting cases, we normally do not get a full console access to the web server and we might not be able to run the uptime.exe script that is normally available to check the uptime of a Windows system. Recently we experienced this issue of Ping timing out, SMTP not responding with our domain for a while and we suspected whether the server is going for a restart or not.

Peek Into System Environment Variables and Counters

After a brief search into MSDN, a small code trick did the job of finding how long the web server had been running. Moreover, this research also revealed one more way of getting the name of the machine from environment variables, besides the usual way.

Server.MachineName

The Code

The actual code that does this trick is:

PerformanceCounter pc = new PerformanceCounter("System",
    "System Up Time");

//Normally starts with zero. do Next Value always.

pc.NextValue(); 
TimeSpan ts = TimeSpan.FromSeconds(pc.NextValue());
    
Response.Write("This system " + Environment.MachineName + 
    " has been up for " + ts.Days + " days " + ts.Hours + 
    " hours, " + ts.Minutes + " and " + ts.Seconds + 
    " seconds.");

I hope this piece of code would really be useful as a ready-reckoner to find out how long the web server has been running.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Vasudevan Deepak Kumar


Vasudevan Deepak Kumar is from Chennai, India who has been in the programming career since 1994, when he was 15 years old. He has his Bachelors of Engineering (in Computer Science and Engineering) from Vellore Engineering College (now VIT University). He also has a MBA in Systems from Alagappa University, Karaikudi, India.


He started his programming career with GWBasic and then in his college was involved in developing programs in Fortran, Cobol, C++. He has been developing in Microsoft technologies like ASP, SQLServer 2000. For sometime, he has also been with PHP and MySQL based development in one of his previous organizations. Now currently his focus is on Microsoft .NET World (ASP.NET, C# and Whidbey)


In his past-time, he listens to polite Carnatic Music.

Web Presence



Homepage

http://www.lavanyadeepak.tk/

Blogs



Technical




Gossips




Spiritual







Occupation: Web Developer
Location: India India

Other popular Site & Server Management articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 20 of 20 (Total in Forum: 20) (Refresh)FirstPrevNext
GeneralDid I get it wrong?memberRobMeade0:06 31 May '06  
GeneralHow to use my web application on another computermemberNisarat19:25 30 Jun '05  
GeneralUptime with WMImemberjserban5:46 23 May '05  
GeneralMilliseconds instead of Seconds?memberSunB13:12 21 Apr '05  
GeneralVery goodmemberFFSysCode20:06 12 Jan '05  
GeneralEasier way, without worrying about the "Access Denied" message.memberClinton Frankland10:44 14 Jan '04  
GeneralRe: Easier way, without worrying about the "Access Denied" message.memberMatt Philmon8:56 11 Feb '04  
GeneralRe: Easier way, without worrying about the "Access Denied" message.memberRheal_Notes16:25 3 Jul '06  
Generalplease, what's your system setup?sussAnonymous17:26 12 Dec '02  
GeneralCode does not work?membersmday111:37 10 Dec '02  
GeneralRe: Code does not work?sussAnonymous17:24 12 Dec '02  
GeneralRe: Code does not work?memberevansc20:45 25 Dec '03  
GeneralRe: Code does not work?memberClinton Frankland10:45 14 Jan '04  
GeneralRe: Code does not work?memberVDragon11:49 26 Oct '06  
GeneralRe: Code does not work?memberMatt Philmon8:57 11 Feb '04  
GeneralUptime.exememberAnders Molin22:56 5 Dec '02  
GeneralRe: Uptime.exememberDeepak Kumar Vasudevan23:25 5 Dec '02  
GeneralRe: Uptime.exememberMegan Forbes2:58 9 Dec '02  
GeneralRe: Uptime.exesussAnonymous15:07 24 Sep '03  
GeneralRe: Uptime.exememberRheal_Notes16:35 3 Jul '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Dec 2002
Editor: Smitha Vijayan
Copyright 2002 by Vasudevan Deepak Kumar
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project