[FreeBSD 5.2.1] Uptime in seconds

Discussion in 'Linux & BSD' started by Glaanieboy, Oct 4, 2004.

  1. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    Is it possible to display the uptime in FreeBSD in seconds only? Now I get a whole bunch of information, so it's very difficult to format it in a PHP script, like you see on my new homepage: http://glaanieboy.dyndns.org/develop (bottom right). When I get the uptime in seconds, I can format the time/date format using date(). Thanks :)
     
  2. X-Istence

    X-Istence * Political User

    Messages:
    6,498
    Location:
    USA
    sedated# sysctl -a | grep kern.boottime
    kern.boottime: { sec = 1096732600, usec = 885425 } Sat Oct 2 10:56:40 2004

    Parse it with PHP, and you are set. That was the exact second that the server was booted. take a look at phpSysInfo for more clues on how to do this.
     
  3. j79zlr

    j79zlr Glaanies script monkey Political User

    Messages:
    2,725
    Location:
    Chicago
    I was bored, here ya go,

    PHP:
    <?php 
      
    // Get boottime in seconds
      
    $boottime shell_exec('sysctl kern.boottime');

      
    // Explode and return array 
      
    $a explode (" "$boottime);

      
    // Remove trailing comma
      
    $b ereg_replace('\,'''$a[4]);

      
    // Find uptime in seconds
      
    $uptime time() - $b;

      
    // Determine days, hours, etc
      
    $days floor($uptime/60/60/24);
      
    $hours $uptime/60/60%24;
      
    $mins $uptime/60%60;
      
    $secs $uptime%60;

      
    // Display time
      
    echo "Uptime $days days $hours hours $mins minutes and $secs seconds.";
    ?>
    http://j79zlr.homeunix.com/misc/uptime.php
     
  4. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    :p Thanks! Save me some time :)
    But now another question: I restarted the server yesterday at 7:10pm, yet the system boottime is set at 5:10pm? How can that happen? How can I solve that?
     
  5. Reg

    Reg eXperienced!

    Messages:
    639
    Location:
    Arlington, TX
    This would be caused by the time on the machine (in the BIOS) not being correct. Check to make sure that the system time is correct.

    From the shell, run "uptime." The number on the far left would be the current system time.
     
  6. Geffy

    Geffy Moderator Folding Team

    Messages:
    7,805
    Location:
    United Kingdom
  7. Glaanieboy

    Glaanieboy Moderator

    Messages:
    2,626
    Location:
    The Netherlands
    *bump* Well, I finally integrated j79zlr's code into my site and it works like a charm. About the 2 hours difference, the BIOS is set at the correct time, so I guess the server's time is set at GMT, eventhough I set the time at GMT+1 (+ another 1 for the daylight savings) at install. I solved it by adding $b+=date("Z") in the code, date("Z") is the time difference in seconds from GMT, so that was easily solved :)