Click here to Skip to main content
Licence 
First Posted 5 Dec 2002
Views 104,010
Bookmarked 40 times

Detecting the UpTime of Web Server

By | 5 Dec 2002 | Article
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.

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

Web Developer

India India

Member

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







Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalany one have aspx file which can work with remote computer. PinmemberMember 266788218:45 5 Jan '09  
GeneralRe: any one have aspx file which can work with remote computer. Pinmembermbaocha13:13 29 Apr '09  
QuestionDid I get it wrong? PinmemberRobMeade23:06 30 May '06  
AnswerRe: Did I get it wrong? Pinmembermbaocha19:19 9 May '09  
QuestionHow to use my web application on another computer PinmemberNisarat18:25 30 Jun '05  
GeneralUptime with WMI Pinmemberjserban4:46 23 May '05  
QuestionMilliseconds instead of Seconds? PinmemberSunB12:12 21 Apr '05  
GeneralVery good PinmemberFFSysCode19:06 12 Jan '05  
GeneralEasier way, without worrying about the "Access Denied" message. PinmemberClinton Frankland9:44 14 Jan '04  
GeneralRe: Easier way, without worrying about the "Access Denied" message. PinmemberMatt Philmon7:56 11 Feb '04  
GeneralRe: Easier way, without worrying about the "Access Denied" message. PinmemberRheal_Notes15:25 3 Jul '06  
Questionplease, what's your system setup? PinsussAnonymous16:26 12 Dec '02  
QuestionCode does not work? Pinmembersmday110:37 10 Dec '02  
AnswerRe: Code does not work? PinsussAnonymous16:24 12 Dec '02  
AnswerRe: Code does not work? Pinmemberevansc19:45 25 Dec '03  
AnswerRe: Code does not work? PinmemberClinton Frankland9:45 14 Jan '04  
GeneralRe: Code does not work? PinmemberVDragon10:49 26 Oct '06  
I Get these results
 
This system GIMPY has been up for 4733 days 3 hours, 9 and 8 seconds.
 
thats a little under 13 Years
 
Changed .FromSeconds to .FromMilliseconds I Get This
 
This system GIMPY has been up for 4 days 17 hours, 39 and 7 seconds.
 
thats more like it
AnswerRe: Code does not work? PinmemberMatt Philmon7:57 11 Feb '04  
GeneralUptime.exe PinmemberAnders Molin21:56 5 Dec '02  
GeneralRe: Uptime.exe PinmemberDeepak Kumar Vasudevan22:25 5 Dec '02  
GeneralRe: Uptime.exe PinmemberMegan Forbes1:58 9 Dec '02  
GeneralRe: Uptime.exe PinsussAnonymous14:07 24 Sep '03  
GeneralRe: Uptime.exe PinmemberRheal_Notes15:35 3 Jul '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120529.1 | Last Updated 6 Dec 2002
Article Copyright 2002 by Vasudevan Deepak Kumar
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid