2 added 2 characters in body
source | link

Discovered one that works across Linux (probably all versions) and Solaris (at least, 5.9) - no access to other OS's to test it and too lazy to check docs :)

boottime=$(who -b | awk '/ /{print $3 " " $4}')")

is the time the system was booted in yyyy/mm/dd HH:MM:SS format, so converting to epoch-seconds is:

bootepoc=date -d "$boottime" +%s

and time its been up in seconds is obviously just current epoc-time minus that ie. $(( $(date +%s) - $bootepoc ))$(( $(date +%s) - $bootepoc ))

If you want that as a more formatted date, might be easier going with another answer although other scripting languages can get you there (probably possible other ways too).

(you didnt say how you wanted it but gave a different example).

Discovered one that works across Linux (probably all versions) and Solaris (at least, 5.9) - no access to other OS's to test it and too lazy to check docs :)

boottime=$(who -b | awk '/ /{print $3 " " $4}')")

is the time the system was booted in yyyy/mm/dd HH:MM:SS format, so converting to epoch-seconds is:

bootepoc=date -d "$boottime" +%s

and time its been up in seconds is obviously just current epoc-time minus that ie. $(( $(date +%s) - $bootepoc ))

If you want that as a more formatted date, might be easier going with another answer although other scripting languages can get you there (probably possible other ways too).

(you didnt say how you wanted it but gave a different example).

Discovered one that works across Linux (probably all versions) and Solaris (at least, 5.9) - no access to other OS's to test it and too lazy to check docs :)

boottime=$(who -b | awk '/ /{print $3 " " $4}')")

is the time the system was booted in yyyy/mm/dd HH:MM:SS format, so converting to epoch-seconds is:

bootepoc=date -d "$boottime" +%s

and time its been up in seconds is obviously just current epoc-time minus that ie. $(( $(date +%s) - $bootepoc ))

If you want that as a more formatted date, might be easier going with another answer although other scripting languages can get you there (probably possible other ways too).

(you didnt say how you wanted it but gave a different example).

1
source | link

Discovered one that works across Linux (probably all versions) and Solaris (at least, 5.9) - no access to other OS's to test it and too lazy to check docs :)

boottime=$(who -b | awk '/ /{print $3 " " $4}')")

is the time the system was booted in yyyy/mm/dd HH:MM:SS format, so converting to epoch-seconds is:

bootepoc=date -d "$boottime" +%s

and time its been up in seconds is obviously just current epoc-time minus that ie. $(( $(date +%s) - $bootepoc ))

If you want that as a more formatted date, might be easier going with another answer although other scripting languages can get you there (probably possible other ways too).

(you didnt say how you wanted it but gave a different example).