Search code examples
windowsservicecmduptime

Cmd/PowerShell/SQL server: Is there any way to see how long a windows service has been running?


So I managed to check if a sevice is running with sc query "ServiceName" | find "RUNNING" or net start | find "Service Name", or in SQL Server using xp_servicecontrol. Is there any way to see the uptime of a service? How can I see the uptime of a service?


Solution

  • As long as your service has it's own process name, this should work.

    PowerShell_v4> (Get-Process lync).StartTime
    
    Friday, October 17, 2014 11:46:04
    

    If you're running under svchost.exe, i think you need to grab that from Event Log.

    PowerShell_v4> (Get-WinEvent -LogName System | ? Message -match 'DHCPv6 client service is started' | select -First 1).TimeCreated
    
    Friday, October 17, 2014 10:10:56
    

    For Uptime, just compute time diff.

    $Start = (Get-Process Outlook).StartTime
    $Now = Get-Date
    $Now - $Start | Format-Table Days, Hours, Minutes, Seconds -AutoSize
    
    Days Hours Minutes Seconds
    ---- ----- ------- -------
       0     0       2       8
    

    or as a one-liner:

    (Get-Date) - (Get-Process Outlook).StartTime | Format-Table Days, Hours, Minutes, Seconds -AutoSize
    
    Days Hours Minutes Seconds
    ---- ----- ------- -------
       0     0       2       8