0

I have a PHP panel that has been ported from a linux box to a windows box. In the panel it shows the uptime for a specific process to let the use know how long the program on the system has been running. In linux this was easy because I could just pipe and regex a PS Aux with some flags to output the time. However, in Windows I can't figure out how to do it. I'm using this command to output information about the program thinking that CPU Time would be what I want: tasklist /fi "imagename eq program.exe" /v

Well, as it turns out CPU time measures how much time the process has ran on any and all CPUs, which of course could be vastly different than real time depending on how many threads it's running on at any given time during the process.

So how can I use CMD to output a process' run time in real world terms?

3

1 Answer 1

4

How can I get a process' run time in real world terms?

Assumption:

  • By "real world terms" you actually mean Elapsed Time

There are two possibilities:


pslist solution

Use pslist from Windows SysInternals.

Example output for notepad++:

F:\test>"c:\apps\WSCC\Sysinternals Suite"\pslist notepad++

pslist v1.3 - Sysinternals PsList
Copyright (C) 2000-2012 Mark Russinovich
Sysinternals - www.sysinternals.com

Process information for HAL:

Name                Pid Pri Thd  Hnd   Priv        CPU Time    Elapsed Time
notepad++          8064   8   9  508  46828     0:02:05.003   148:52:29.866

Use the following batch file (example.cmd) to clean up the output:

@echo off
setlocal enabledelayedexpansion
for /f "usebackq skip=3 tokens=8" %%i in (`pslist notepad++ 2^> nul`) do (
  echo elapsed time: %%i
  )
endlocal

Output:

F:\test>example.cmd
elapsed time: 148:54:15.628

F:\test>

PowerShell solution

Use the following powershell script (GetProcessElapsedTime.ps1):

$ts=((get-date) - (get-process notepad++).StartTime)
$hours = ($ts.Days * 24 + $ts.Hours).ToString("00")
$minutes = $ts.Minutes.ToString("\:00")
$seconds = $ts.Seconds.ToString("\:00")
$milliseconds = $ts.Milliseconds.ToString("\.000")
Write-Output ($hours + $minutes + $seconds + $milliseconds)

Example output for notepad++:

PS F:\test> .\GetProcessElapsedTime
148:54:46.667
PS F:\test>

Disclaimer

I am not affiliated with pslist in any way, I am just an end user of the software.

1
  • Answer updated to add PowerShell solution.
    – DavidPostill
    Apr 6, 2016 at 13:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .