On 2020-03-19 20:33, Thomas Bohl wrote:
> Hello,
>
> for a shell script I needed the uptime in seconds. I came up with the
> following. I'm just wondering, is there an one-liner that does the
> same thing?

$ echo $(( $(date +%s) - $(sysctl -n kern.boottime) ))
221493


> # Inspired by https://unix.stackexchange.com/a/270454
> uptime=`ps -o etime= -p 1`
> uptd=`echo $uptime | grep '-' | awk -F "-" '{print $1}'`
>
> if [ "$uptd" == "" ]
> then
>     upth=`echo $uptime | awk -F ":" '{print $1}'`
>     uptm=`echo $uptime | awk -F ":" '{print $2}'`
>     upts=`echo $uptime | awk -F ":" '{print $3}'`
>
>     if [ "$upts" == "" ]
>     then
>       upts=$uptm
>       uptm=$upth
>       upth=0
>     fi
>
>     uptimeseconds=$((10#$upts + 10#$uptm * 60 + 10#$upth * 3600))
> else
>     upth=`echo $uptime | awk -F "-" '{print $2}' | awk -F ":" '{print
> $1}'`
>     uptm=`echo $uptime | awk -F "-" '{print $2}' | awk -F ":" '{print
> $2}'`
>     upts=`echo $uptime | awk -F "-" '{print $2}' | awk -F ":" '{print
> $3}'`
>     uptimeseconds=$((10#$upts + 10#$uptm * 60 + 10#$upth * 3600 +
> $uptd * 86400))
> fi
>
> echo $uptimeseconds

Reply via email to