Jump to content

System Uptime


msb5150
 Share

Recommended Posts

Written on a whim, needs further developement, but instead of doing it myself, I think I'll enjoy some sleep and let others decide how to implement the month change-overs, and, dare I say, year change-overs. :whistle: I just hope that its actually displaying something similar to the actual up time. By the way, to check the uptime in windows xp, start a command prompt and type 'net statistics workstation' As I said before, there is another variable with a similar time that could be the correct system start time.

;In theory this code should display the system uptime, however certain factors can affect its accuracy:  The first of which is there is a;similar registry variable I found when searching for the system start time.  The second factory that will adversely affect this script is;that I did not feel like concidering that a machine may have been started at the begining of the month, and the script run during the;preceding month, thus completely messing up the uptime calculation.  Sorry if the code is a bit messy, I wrote this instead of sleeping and my knowelege of computer science was obviously affected.

$start = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Prefetcher", "StartTime")
$temp = StringSplit($start, "-")
$sdate = $temp[1]
$sd = StringSplit($sdate, "/")
;$year = Number($sd[1])
;$year = @YEAR - $year
;$month = Number($sd[2])
;$month = @MON - $month
$day = Number($sd[3])
$day = @MDAY - $day
$stemp = $temp[2]
$temp = StringSplit($stemp, ":")
$hour = Number($temp[1])
$hour = @HOUR - $hour
If $hour < 0 Then
    $hour = $hour + 24
EndIf
$min = Number($temp[2])
$min = @MIN - $min
If $min < 0 Then
    $min = $min + 60
EndIf
$sec = Number($temp[3])
$sec = @SEC - $sec
If $sec < 0 Then
    $sec = $sec + 60
EndIf
MsgBox(1, "System Uptime", "This system has been up for " & $day & "day(s), " & $hour & " hour(S), " & $min & " minute(s), and " & $sec & " second(S). Start Time: " & $start)
Link to comment
Share on other sites

Vastly updated, and more legible if I might say so my self. When compared to some of the system monitors I found, this script was off by approximately 30 seconds. I suspect that this is so because the system monitors are reading some windows file, as opposed to the registry, which is where the discrepency is created.

$start = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Prefetcher", "ExitTime");Switched "ExitTime" for "StartTime"
$temp = StringSplit($start, "-")
$date = $temp[1]
$time = $temp[2]
$td = StringSplit($date, "/")
$tt = StringSplit($time, ":")

$year = Number($td[1])
$month = Number($td[2])
$day = Number($td[3])
$year = @YEAR - $year
If Not $month = @MON Then
    Monchange()
Else
    $day = @MDAY - $day
EndIf


$hour = Number($tt[1])
$hour = (@HOUR - 1) - $hour
If $hour < 0 Then
    $hour = $hour + 24
EndIf
$min = Number($tt[2])
$min = @MIN - $min
If $min < 0 Then
    $min = $min + 60
EndIf
$sec = Number($tt[3])
$sec = @SEC - $sec
If $sec < 0 Then
    $sec = $sec + 60
EndIf
MsgBox(1, "System Uptime", "This system has been up for " & $day & "day(s), " & $hour & " hour(s), " & $min & " minute(s), and " & $sec & " second(S). Start Time: " & $start)

;Compensates for month changes
Func Monchange()
    Select
  Case $month = 1
    $day = 31 - $day
  Case $month = 2
    $day = 28 - $day
  Case $month = 3
    $day = 31 - $day
  Case $month = 4
    $day = 30 - $day
  Case $month = 5
    $day = 31 - $day
  Case $month = 6
    $day = 30 - $day
  Case $month = 7
    $day = 31 - $day
  Case $month = 8
    $day = 31 - $day
  Case $month = 9
    $day = 30 - $day
  Case $month = 10
    $day = 31 - $day
  Case $month = 11
    $day = 30 - $day
  Case $month = 12
    $day = 31 - $day
    EndSelect
                $mons = (@MON - $month) - 1
    If $mons > 1 Then
  While $mons > 0
    Select
    Case $mons = 1
        $day = 31 + $day
    Case $mons = 2
        $day = 28 + $day
    Case $mons = 3
        $day = 31 + $day
    Case $mons = 4
        $day = 30 + $day
    Case $mons = 5
        $day = 31 + $day
    Case $mons = 6
        $day = 30 + $day
    Case $mons = 7
        $day = 31 + $day
    Case $mons = 8
        $day = 31 + $day
    Case $mons = 9
        $day = 30 + $day
    Case $mons = 10
        $day = 31 + $day
    Case $mons = 11
        $day = 30 + $day
    Case $mons = 12
        $day = 31 + $day
    EndSelect
    $mons = $mons - 1
  WEnd
    EndIf
    $day = $day + @MDAY
EndFunc
Edited by msb5150
Link to comment
Share on other sites

  • 2 months later...

msb5150,

I think I have found a bug in your uptime script. If you run it at 12 pm it shows that your system has been up for 23 hours, even if you have just recently restarted it. Will do some more testing on this and let you know what I find.

Link to comment
Share on other sites

Vastly updated, and more legible if I might say so my self.  When compared to some of the system monitors I found, this script was off by approximately 30 seconds.  I suspect that this is so because the system monitors are reading some windows file, as opposed to the registry, which is where the discrepency is created.

$start = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Prefetcher", "ExitTime");Switched "ExitTime" for "StartTime"
:D Just a question, what is the difference between the "exit time" and the "start time" entry?? If exit time was the last exit time (just guessing), won't the timer be inaccurate if I did shut down (to go to lunch) and come back instead of rebooting? (I found my exit time was a minute or so earlier than the start time).
Link to comment
Share on other sites

This is the last thing I wanted...I had hoped that this post would just go away. I've been meaning to update it with one of the latest time difference udf's (which are very cool by the way, and put my 3 minute coded one to shame) I know that its innacurate, I discovered this shortly after my second post, but I have sence stopped using this script in favor of an uptime and system information screensaver I found. Despite my wishes this post has reapeared so I will now my an honest attempt at making this script accurate, with much help from the latest udf's of course. @R.L. I am not quite sure what you mean...I am not even sure if those registry variables are accurate, I just did a basic search of the registry of my current system uptime in hope of finding a simple way to discover it via autoit, after other reboots I checked the uptime versus this registry entry to determine its accuracy, and I found that it was decently accurate, so I wrote to script based off of its entry. If someone can find a better way to determine the system start time, that does not involve some third-party app, or grabbing it from the command prompt, I ask that they post it.

Link to comment
Share on other sites

  @R.L. I am not quite sure what you mean...I am not even sure if those registry variables are accurate, I just did a basic search of the registry of my current system uptime in hope of finding a simple way to discover it via autoit, after other reboots I checked the uptime versus this registry entry to determine its accuracy, and I found that it was decently accurate, so I wrote to script based off of its entry.  If someone can find a better way to determine the system start time, that does not involve some third-party app, or grabbing it from the command prompt, I ask that they post it.

Actually, I *think* you might be up to something. The hearsay I got is that window OS may be keeping an uptime -timer itself. However, I have heard repeatedly that this "timer" would reset itself after fourty some days to zero. So, if you have your OS uptime for longer than that fourty some days, you won't be having an accurate reading. That is, in fact, what I read in several readme files in some uptime display programs. However, your method may be valid except that it won't be display as a "timer" with real time counting, but it may be able count over more than 40 some days, if Windows do not refresh this entry. One potiential problem of this method perhaps is that since the entry is part of the "prefretch", it may not work if the user turn off that feature in their system or it won't work in 9x (they don't have prefretch). I play with your idea a bit and think it may be a better solution than just using the internal timer. For the fun of it, I use the registry entry to dug out and take your script as a reference to make this:

http://www.autoitscript.com/fileman/users/public/RL/uptimequickie.au3

....and..., while I was having fun with it, I also gave it a GUI :D

Edited by R.L.
Link to comment
Share on other sites

Maybe it's just me, but wouldn't it just be easier to open a dos window and do a "Time" {enter} and just copy the results?

That's the clock time, not system uptime. Uptime is the amount of time the system has been running (IE the time since it was last booted.)

A quick serach on google turned up this MS app. If you were willing to FileInstall a file for use on systems, it might prove very handy.

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

A quick serach on google turned up this MS app.  If you were willing to FileInstall a file for use on systems, it might prove very handy.

I have this MS one, too. The thing is that I don't like Dos based ones because I want to be able to paste it to a file (and it does also just count to fourty some days).

In fact, before the orginal poster made his post in this thread, I had been actually trying to find a way to do it in AutoIt, too. I found several freeware applies but they all have the same limitation, the timer will "reset" after fourty some days. In addition, I don't like their interfaces, either.

And thanks to the orginal poster of this thread who found that entry in the registry. In fact, based on the script I posted I made one that can put a line on the clipboard. Here it is:

(I gave a name to it and call it Uptime Quickie :D

)

Uptime Quickie

homepage

Screenshot here

direct download:

direct dowload complied version

http://www.autoitscript.com/fileman/users/public/RL/uptimequickie.au3

Since that registry entry is available only in XP (don't know about NT), I'm afraid it would work just for XPs. For 9x user, I used this one when I was using 9x:

http://www.rjlsoftware.com/software/utility/uptime/

Edited by R.L.
Link to comment
Share on other sites

Why someone should need to know the system uptime?

Sometimes a user is just interested to know how long the system has been running for.

Another use is for server tracking and logs. I currently co-administrate a windows 2003 server machine for recreation, and knowing the uptime is very nice. It tells me if the system was restarted since my last log on, and allows me to keep track of performance verses uptime.

[font="Optima"]"Standing in the rain, twisted and insane, we are holding onto nothing.Feeling every breath, holding no regrets, we're still looking out for something."[/font]Note: my projects are off-line until I can spend more time to make them compatable with syntax changes.

Link to comment
Share on other sites

For my script to work properly, I need to run it after the users has had a problem and restarted there computer. I was just using it to check to see if they had indeed restarted there computer like they were supposed to.

Link to comment
Share on other sites

Why someone should need to know the system uptime?

(I do not mean bash your job, I am really curious)

In my case, I am not the only user of my PC, but nevertheless its troubleshooter, so it is good to know if the pc has been restarted while I am not using it.

Fixed a few bugs on the script (fixed that some variables were two far away before I used them which caused skips of calculations).

http://www.autoitscript.com/fileman/users/public/RL/uptimequickie.au3

Link to comment
Share on other sites

Here is my solution to the problem with the help from this-is-me and a leson on mod() I came up with this.

$temp =  timerstop(0)
$Daytemp = Int($Temp / 86400000)
$HourTemp = Int(Mod($Temp , 86400000) / 3600000)
$MinTemp = Int(Mod(Mod($Temp , 86400000) , 3600000) / 60000)
$SecTemp = Int(Mod(Mod(Mod($Temp , 86400000) , 3600000) , 60000) / 1000)

Msgbox(0,'Uptime', ' Days: ' & $DayTemp & @CRLF &_
                   ' Hours: '& $hourTemp & @CRLF &_
                   ' Minutes: ' & $MinTemp & @CRLF &_
                   ' Seconds: ' & $SecTemp)

This is consistant with the times I am getting from the uptime command in windows.

@this-is-me

Thanks for pointing me in the right direction.

-magicboy-

Edited by magicboy
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...