Skip to content

Uptime Linux #18469

Closed
Closed

Description

@237dmitry

Prerequisites

Steps to reproduce

There is too much difference in the readings of the /usr/bin/uptime and the Get-Uptime cmdlet

 PS > uptime -p
up 2 days, 16 hours, 26 minutes
 
PS > (Get-Uptime).ToString()
1.13:45:12

Expected behavior

Expected equal values

Actual behavior

Different values

Error details

No response

Environment data

Name                           Value
----                           -----
PSVersion                      7.3.0-rc.1
PSEdition                      Core
GitCommitId                    7.3.0-rc.1
OS                             Linux 6.0.6-arch1-1 #1 SMP PREEMPT_DYNAMIC Sat, 29 Oct 2022 14:08:39 +0000
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

up

Activity

  1. added
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    on Nov 4, 2022
  2. SteveL-MSFT commented on Nov 5, 2022

    @SteveL-MSFT
    Member

    From what I can tell, the current implementation is simply relying on .NET which calls the native clock_gettime() function. However, it may request CLOCK_MONOTONIC which gives the time since the system booted, but does NOT include any time when the system was suspended. It seems that it should be CLOCK_BOOTTIME which includes any suspended time. Looking through the .NET source, they only use CLOCK_BOOTTIME to calculate the start time of processes and is not public. So it seems the best option is to P/Invoke the API on non-Windows.

  3. SteveL-MSFT commented on Nov 5, 2022

    @SteveL-MSFT
    Member

    I think it might depend on your distro as the reference code does have different API calls depending on what is available on the system. On my Ubuntu 22.04 install, uptime -p and get-uptime match.

  4. 237dmitry commented on Nov 5, 2022

    @237dmitry
    Author

    but does NOT include any time when the system was suspended.

    Yes, after reboot values was identical, when I closed the lid of laptop for three minutes I got the difference in values.
    Perhaps it would be correct to take timespan from the last boot time?

     $ who -b
             system boot  2022-11-05 11:39
    
     $ (last -x reboot)[0]
    reboot   system boot  6.0.7-arch1-1    Sat Nov  5 11:39   still running
    
  5. iSazonov commented on Nov 5, 2022

    @iSazonov
    Collaborator

    Looking through the .NET source, they only use CLOCK_BOOTTIME to calculate the start time of processes and is not public. So it seems the best option is to P/Invoke the API on non-Windows.

    This doesn't work on all platforms.

    Best option is to request new API in .Net System.Environment.Uptime.
    Until that, since it is not critical, we could add a workaround for Linux systems and use /proc/uptime.

  6. 237dmitry commented on Nov 5, 2022

    @237dmitry
    Author

    we could add a workaround for Linux systems and use /proc/uptime

    This works.

     $ [timespan]::FromMilliseconds(([int](gc /proc/uptime).Split()[0] * 1000)).ToString()
    01:33:18
     
     $ uptime -p
    up 1 hour, 33 minutes
  7. iSazonov commented on Nov 5, 2022

    @iSazonov
    Collaborator

    We use Stopwatch.GetTimestamp() and if it takes into account suspend time on Windows but not on Unix this looks like a bug. It makes sense to open new issue in .Net Runtime repository.

  8. SteveL-MSFT commented on Nov 7, 2022

    @SteveL-MSFT
    Member

    Ilya (@iSazonov) I tend to agree that this seems like a bug that should be fixed in .NET

  9. microsoft-github-policy-service commented on Nov 15, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  10. microsoft-github-policy-service commented on Nov 15, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  11. microsoft-github-policy-service commented on Nov 15, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  12. microsoft-github-policy-service commented on Nov 22, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  13. added
    Resolution-ExternalThe issue is caused by external component(s).
    .NETPull requests that update .NET code
    and removed
    Needs-TriageThe issue is new and needs to be triaged by a work group.
    Resolution-No ActivityIssue has had no activity for 6 months or more
    on Dec 2, 2023
  14. xtqqczze commented on Oct 8, 2025

    @xtqqczze
    Contributor

    Ryan Yates (@kilasuit) Please reopen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETPull requests that update .NET codeResolution-ExternalThe issue is caused by external component(s).

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions