只需几秒钟即可获得 SunOS UNIX 机器的正常运行时间
如何确定 SunOS UNIX 机器上的正常运行时间(仅以秒为单位)?
在 Linux 上,我可以简单地 cat /proc/uptime &采用第一个参数:
cat /proc/uptime | awk '{print $1}'
我试图在 SunOS UNIX 机器上执行相同的操作,但没有 /proc/uptime。 有一个 uptime 命令显示以下输出:
$ uptime
12:13pm up 227 day(s), 15:14, 1 user, load average: 0.05, 0.05, 0.05
我真的不想编写代码将日期仅转换为秒 &我确信以前一定有人有过这个要求,但我在互联网上找不到任何东西。
谁能告诉我如何在几秒钟内获得正常运行时间?
TIA
How do I determine the uptime on a SunOS UNIX box in seconds only?
On Linux, I could simply cat /proc/uptime & take the first argument:
cat /proc/uptime | awk '{print $1}'
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您不介意编译一个小型 C 程序,您可以使用:
来源: http://xaxxon.slackworks .com/rsapi/
If you don't mind compiling a small C program, you could use:
Source: http://xaxxon.slackworks.com/rsapi/
这是已经给出的一些答案的混合。只需两个命令即可输出正常运行时间(以秒为单位)。在 Solaris 9 和 10 上测试。
This is a mix of some answers already given. Outputs uptime in seconds with only two commands. Tested on Solaris 9 and 10.
一个相当非正统的方法可能是这样的(帮助我,因为 kstat 产生了一些没有通过 cron 的奇怪结果:
希望它能帮助你 -
好的副作用:您有可用于计算的时间位。
A rather unorthodox method might be this (helped me out since the kstat yielded some weired results that did not pass cron:
Hope it helps you out -
Nice side effect: You have the time bits available for calculations.
感谢安德烈提供了一个可以提供几秒钟的解决方案。
如果有人想在不编译的情况下寻找答案,可以使用这个脚本。请注意,由于“uptime”命令不提供秒数,因此解决方案是运行时的 0 到 59 秒:
希望这对某人有用:-)
Thanks to Andre for a solution that will provide seconds.
If anyone is looking for an answer without compiling, this script can be used. Note, as the "uptime" command does not provide seconds the solution is anything from 0 to 59 seconds out when it is run:
Hope that's of use to someone :-)
Perl:CPAN 提供 unix::uptime - 然而,它目前与 SunOS 不兼容,但将来可能会兼容。
Perl: CPAN provides unix::uptime - however, it is not currently compatible with SunOS but may be in the future.
这是提供第二个解决方案的 shell 脚本。请注意,您需要成为 root 才能使其工作。
Here is a shell script that provides second resolution. Note that you need to be root for it to work.
您可以使用 kstat 来查找系统正常运行时间。
这将返回 unix 格式的值。下面是对我在秒内获取值非常有用的代码。
You can use kstat to find the system uptime.
This will return the values in unix format. Below is the code which was very useful for me to get the values in sec.
使用truss提取目录/proc/0的创建时间。 (必须以 root 身份运行。)
Use truss to extract the creation time of the directory /proc/0. (Must be run as root.)
在 date 命令上使用 truss 来获取纪元时间并减去从 kstat 获得的启动时间。
Use truss on the date command to get epoch time and subtract boot time obtained from kstat.