I need to get the number of seconds since the last reboot, using ksh.
What is the command or function to achieve this?
I need to get the number of seconds since the last reboot, using ksh.
What is the command or function to achieve this?
If you check the psinfo for process with PID 1 from /proc directory you can get this structure:
struct pr_timestruc64_t pr_start; /* process start time, time since epoch */
Here is the webpage you can use for reference.
For Ubuntu, Redhat, Suse, we can have this file. Not much sure about AIX, though
See the output of:
cat /proc/uptime
If this is not available on AIX, then try to execute command uptime
This answer is blatantly copied from Gilles' answer at How to get a process uptime under different OS?:
t=$(LC_ALL=POSIX ps -o etime= -p 1)
d=0 h=0
case $t in *-*) d=$((0 + ${t%%-*})); t=${t#*-};; esac
case $t in *:*:*) h=$((0 + ${t%%:*})); t=${t#*:};; esac
s=$((10#$d*86400 + 10#$h*3600 + 10#${t%%:*}*60 + 10#${t#*:}))
This leaves you with the number of seconds of uptime in $s.
We're making the assumption that init's start time is when we start counting "uptime" and also that init is PID 1 (true in my limited testing).