我需要与Linux的“cat / proc / uptime”完全相同的输出。
例如,使用/ proc / uptime,你会得到
1884371.64 38646169.12
但是对于任何Mac替代品,例如“正常运行时间”,你会得到
20:25 up 20:26,6位用户,平均负载:3.19 2.82 2.76
我需要完全,如cat / proc / uptime,但在Mac OS X上。
答案 0 :(得分:6)
得到了......
$sysctl -n kern.boottime | cut -c14-18
87988
然后我将其转换为可读格式(不记得如何):
1天00:26:28
答案 1 :(得分:3)
Macintosh上没有“/ proc”目录。
On MacOS, you can do a command like:
sysctl kern.boottime
你会收到如下回复:
kern.boottime: { sec = 1362633455, usec = 0 } Wed Mar 6 21:17:35 2013
答案 2 :(得分:1)
boottime=`sysctl -n kern.boottime | awk '{print $4}' | sed 's/,//g'`
unixtime=`date +%s`
timeAgo=$(($unixtime - $boottime))
uptime=`awk -v time=$timeAgo 'BEGIN { seconds = time % 60; minutes = int(time / 60 % 60); hours = int(time / 60 / 60 % 24); days = int(time / 60 / 60 / 24); printf("%.0f days, %.0f hours, %.0f minutes, %.0f seconds", days, hours, minutes, seconds); exit }'`
echo $uptime
将返回类似的内容 1天,20小时,10分钟,55秒
答案 3 :(得分:0)
这里是我要获取值而不是Cut方法的方法
sysctl kern.boottime | awk '{print $5}'
其中$ 5是字符串的范围
示例
$1 Gives you "sysctl kern.boottime"
$2 Gives you "{"
$3 Gives you "sec"
来自字符串
kern.boottime: { sec = 1604030189, usec = 263821 } Fri Oct 30 09:26:29 2020