/proc/uptime in Mac OS X

后端 未结 4 712
不知归路
不知归路 2021-02-08 19:55

I need the EXACT same output as Linux\'s \"cat /proc/uptime\".

For example, with /proc/uptime, you\'d get

1884371.64 38646169.12

4条回答
  •  北海茫月
    2021-02-08 20:29

    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
    

    Will return something like 1 Day, 20 hours, 10 minutes, 55 seconds

提交回复
热议问题