0

I need to get the number of seconds since the last reboot, using ksh.

What is the command or function to achieve this?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Venkat Teki
  • 337
  • 1
  • 3
  • 13

3 Answers3

1

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.

Romeo Ninov
  • 16,541
  • 5
  • 32
  • 44
0

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

SHW
  • 14,454
  • 14
  • 63
  • 101
0

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).

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250