仅以秒为单位获取SunOS UNIX盒的正常运行时间 [英] Getting the uptime of a SunOS UNIX box in seconds only

查看:159
本文介绍了仅以秒为单位获取SunOS UNIX盒的正常运行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何确定以秒为单位的SunOS UNIX机器的正常运行时间?

How do I determine the uptime on a SunOS UNIX box in seconds only?

在Linux上,我可以简单地将/proc/uptime&接受第一个参数:

On Linux, I could simply cat /proc/uptime & take the first argument:

cat /proc/uptime | awk '{print $1}'

我试图在SunOS UNIX上执行相同的操作,但是没有/proc/uptime. 有一个正常运行时间命令,显示以下输出:

I'm trying to do the same on a SunOS UNIX box, but there is no /proc/uptime. There is an uptime command which presents the following output:

$ uptime
12:13pm  up 227 day(s), 15:14,  1 user,  load average: 0.05, 0.05, 0.05

我真的不想写代码将日期仅转换为秒&;我确定有人一定曾经有此要求,但我无法在互联网上找到任何东西.

I don't really want to have to write code to convert the date into seconds only & I'm sure someone must have had this requirement before but I have been unable to find anything on the internet.

有人可以告诉我如何在几秒钟内获得正常运行时间吗?

Can anyone tell me how to get the uptime in just seconds?

TIA

推荐答案

如果您不介意编译小型C程序,则可以使用:

If you don't mind compiling a small C program, you could use:

#include <stdio.h>
#include <string.h>
#include <utmpx.h>

int main()
{
  int nBootTime = 0;
  int nCurrentTime = time ( NULL );
  struct utmpx * ent;

  while ( ( ent = getutxent ( ) ) ) {
    if ( !strcmp ( "system boot", ent->ut_line ) ) {
      nBootTime = ent->ut_tv.tv_sec;
    }
  }

  printf ( "System was booted %d seconds ago\n", nCurrentTime - nBootTime );
  endutxent();

  return 0;
}

来源: http://xaxxon.slackworks.com/rsapi/

这篇关于仅以秒为单位获取SunOS UNIX盒的正常运行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆