Need Script Or Command to get BootUp time duration and Startup duration

Manivannan
New Contributor

Hello Team,

Can anyone help me with a Script Or Command to get BootUp time duration and Startup duration .

I tried using the below command ,but it just gives me the up time but I need calculate the duration time of the boot and startup.

sysctl -n kern.boottime

Thanks,

3 REPLIES 3

dmccluskey
Contributor II

This is a EA I use.

Time Since Last Reboot

 

 

#!/bin/bash

lastBootTime=$(sysctl kern.boottime | awk -F'[ |,]' '{print $5}')
currentTime=$(date +"%s")
upTimeRaw=$((currentTime-lastBootTime))
upTimeMin=$((upTimeRaw/60))
upTimeHours=$((upTimeMin/60))
upTimeDays=$((upTimeHours/24))
minusMinutes=$((((upTimeDays*24))*60))
remainingMin=$((upTimeMin-minusMinutes))
remainingHrs=$((remainingMin/60))
minusHours=$((upTimeHours-remainingHrs))

total1=$((upTimeDays*24*60))
total2=$((remainingHrs*60))
minusMinutes2=$((total1+total2))
remainingMinFin=$((upTimeMin-minusMinutes2))

UptimeThreshold1alt=$((UptimeThreshold1-1))
UptimeThreshold2alt=$((UptimeThreshold2-1))

if [ "$upTimeDays" == "1" ]; then
daysText="Day"
else
daysText="Days"
fi

if [ "$remainingHrs" == "1" ]; then
hrsText="Hour"
else
hrsText="Hours"
fi

if [ "$remainingMinFin" == "1" ]; then
minsText="Minute"
else
minsText="Minutes"
fi

echo "<result>$upTimeDays $daysText, $remainingHrs $hrsText, $remainingMinFin $minsText</result>"

Thank you for Sharing the script,

but I am looking for a script which gives me Boot duration time and Startup speed time.

I am kind of trying to measure the speed of boot time of my mac and similarly startup time from login page of the user

mm2270
Legendary Contributor III

This isn't an easy thing to capture. Honestly the only thing that comes to mind to do this would be to have a LaunchDaemon crafted and installed, which would run a script. The script will get called after boot up but before a user logs in, or the login window appears, since these daemons are called by the system even before the OS has completed a bootup.

You can have the script first capture the boot up time using the sysctl command you already know about. This would constitute your "start time". Then have the script run a loop that keeps checking for either the loginwindow process running, or for something like the Dock, which would indicate a successful log in, and then capture the unix timestamp for when one (or both) of those conditions are true. These would constitute the end or stop time. This can all be written into a local log file on the device and the EA can simply scrape the data out of that file. Or, have the EA do some math that would calculate the difference between the boot time as registered by sysctl kern.boottime and the end or stop times. You'll end up with some value in seconds by subtracting the former from the latter.

The only complication you may run into here is if you have FileVault enabled. I'm not sure when the kern.boottime gets recorded, but I suspect/hope it's after a successful FV2 authentication. But I could be wrong. If it happens before someone logs in, it's going to throw your calculations way off.

This is all just back of the napkin thinking. I haven't done any of the above, so it may not work as I'm describing.