Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Getting system uptime on Windows

132 views
Skip to first unread message

walto...@gmail.com

unread,
Nov 28, 2005, 9:07:41 PM11/28/05
to
Does anyone know of a way to find the system uptime or the time of the
last boot on Windows?

With TWAPI, I can get the time that different users logged on. I´ve
also found a registry key that stores the time of the last system
shutdown. But still I can´t find any way to get the system uptime.

I can parse the time from the output of the following system commands,
but running these commands with [exec] gives no return value, so I
can´t access the output within Tcl:
net statistics workstation
net statistics server


Now I´m trying to do it by reading the Windows event log through
TWAPI, but still no luck.

Any ideas? Thanks in advance.


Paul

walto...@gmail.com

unread,
Nov 29, 2005, 1:08:23 AM11/29/05
to
Something strange... This command works on Windows XP with tclsh, but
not with wish:

exec net statistics server

With tclsh, the exec command returns the output, but in wish it just
returns an empty string,

Leopold Gerlinger

unread,
Nov 29, 2005, 3:24:26 AM11/29/05
to
Puh!

It seems that the 'NET' command is only working from a console window
correctly. I tried several ways to capture the output (e.g. redirecting to a
file) but none of them worked. Also one can see a DOS box flicker when
calling the command from wish. For me the NET command is programmed in a way
like: "If I'm not started from the console, let's open it by myself"

That way it sure prevents capturing the output. I also tried opening a pipe
or calling it indirectly with 'exec cmd.exe /c ...'. None of them worked.
Seems we have to thank Big Bill once more ;-)

Regards - Leo


Ilya Ginzburg

unread,
Nov 29, 2005, 4:19:21 AM11/29/05
to
I used [net.exe use] from wish (WinXP). It was work ok.
Now I use [twapi::connect_share], but it is an other saga...

Ilya


"Leopold Gerlinger" <leopold....@siemens.com> wrote in message
news:dmh37r$el5$1...@mail1.sbs.de...

walto...@gmail.com

unread,
Nov 29, 2005, 9:26:58 AM11/29/05
to
I was able to capture the output by first writing the command to a
batch file and then using exec on the .bat file.

set token [open uptime.bat w]
puts $token "net statistics server"
close $token

set data [exec uptime.bat]

file delete uptime.bat

And then it´s just a matter of parsing the output to get the last boot
time. But if anyone else uses this method, be advised that the output
is in the user´s preferred language, so some fancy parsing is required
to be international.

Also, I don´t think this command is available on Windows 98.

David Gravereaux

unread,
Nov 29, 2005, 12:08:18 PM11/29/05
to
walto...@gmail.com wrote:
> Does anyone know of a way to find the system uptime or the time of the
> last boot on Windows?
>
> With TWAPI, I can get the time that different users logged on. I扉e

> also found a registry key that stores the time of the last system
> shutdown. But still I can愒 find any way to get the system uptime.

>
> I can parse the time from the output of the following system commands,
> but running these commands with [exec] gives no return value, so I
> can愒 access the output within Tcl:

> net statistics workstation
> net statistics server
>
>
> Now I惴 trying to do it by reading the Windows event log through

> TWAPI, but still no luck.
>
> Any ideas? Thanks in advance.
>
>
> Paul
>


Hmm.. uptime.. Your answer is:

% winutils::duration [winutils::uptime]

proc ::winutils::duration { int_time } {
set timeList [list]
foreach div {86400 3600 60 1} mod {0 24 60 60} name {day hr min sec} {
set n [expr {$int_time / $div}]
if {$mod > 0} {set n [expr {$n % $mod}]}
if {$n > 1} {
lappend timeList "$n ${name}s"
} elseif {$n == 1} {
lappend timeList "$n $name"
}
}
return [join $timeList]
}


The performance counter used is "\\System\\System Up Time", FYI. If
TWAPI can query performance counter bits, you won't need my C source.

UpTime.c

pal...@yahoo.com

unread,
Dec 1, 2005, 6:39:12 AM12/1/05
to
Using twapi, the following returns up time in seconds.

package require twapi
twapi::get_counter_path_value {\\.\System\System Up Time} -interval 0

Twapi has a full interface to the Windows performance counters but it
is undocumented as I'm not entirely happy with its complexity and am
planning to modify it at some point.

/Ashok

0 new messages