1

I want to set a daily limit as to how long my computer can be on in a single day. In order to do so, I'm writing a cron script that will take appropriate action when certain milestones are reached (showing an alert when time is running out; shutting down the computer when time is up).

The part I'm struggling with is getting the total uptime in minutes for the day. I have looked at this uptime script, but I feel that using last is simplest.

$ last -s today

Gives me something like:

user     tty1                          Sat Jul 22 14:09   still logged in
user     tty1                          Sat Jul 22 11:50 - down   (01:56)

I am currently trying to convert the hours in parentheses to minutes, and also converting the 'still logged in' session to minutes. It can work, but I feel like all these options are a bit convoluted.

Is there a simpler way to get the total uptime (excluding suspend/sleep) in minutes, or should I find a workaround for these types of outputs?

0

1 Answer 1

0

Execute this on the shell, to get the total uptime in minutes for the current session:

echo "Total uptime today (minutes): $(( ($(date +%s) - $(date -d "$(uptime -s)" +%s)) / 60 ))"

Execute this with awk and /proc/uptime:

awk '{print "Total uptime for the machine (minutes):", int($1 / 60)}' /proc/uptime

Update:

The first two commands only display the runtime of the current session.

I have now implemented it in a small bash script, combining last and /proc/uptime. Maybe you can achieve it with a one-liner for the shell.

#!/bin/bash

sessions_uptime=$(last | grep "$(date '+%b %d')" | grep 'reboot' | grep -v 'still running' | awk '{print $NF}' | tr -d '()' | awk -F: '{sum_minutes+=$1*60+$2} END {print sum_minutes}')
current_time=$(awk '{print int($1 / 60)}' /proc/uptime)

total_uptime=$((sessions_uptime + current_time))
echo "TOTAL CURRENT RUNTIME OF THE DAY: ${total_uptime}"

sessions_uptime

  • Filters out all reboots from last except for still running and calculates the time in minutes from all sessions of the day.

current_time

  • Calculates the current runtime of the session.

total_uptime

  • Outputs the sum of both values, i.e., the total uptime of the day, including the current session. I have executed this on Debian 11.7; I cannot say if it works on all systems.

Update2:

#!/bin/bash

get_minutes() {
  local h=${1%%:*}
  local m=${1#*:}
  echo $((10#$h * 60 + 10#$m))
}

sessions_uptime=$(last | grep "$(date '+%b %d')" | grep -E 'reboot|suspended' | grep -v 'still running' | awk '{print $NF}' | tr -d '()' | awk -F: '{sum_minutes+=$1*60+$2} END {print sum_minutes}')

current_uptime_seconds=$(awk '{print int($1)}' /proc/uptime)

total_uptime=$((sessions_uptime + current_uptime_seconds / 60))
echo "TOTAL CURRENT RUNTIME OF THE DAY: ${total_uptime} minutes"

Identify which eventualities need to be considered and adjust the scripts if the approach is suitable.

I found these status messages for last on the internet, but I cannot confirm if all of them are accurate.

"reboot"

The system has been rebooted properly

"shutdown"

The system has been shut down properly

"suspended"

The system has been put into suspend or hibernation mode

"resumed"

The system has been restored from suspend or hibernation mode

"crash"

The system has been rebooted due to an error or crash

"down"

The system has been shut down, which can also occur due to power failures or hardware issues

"gone - no logout"

Can be displayed when a user abruptly ends the connection without logging out properly

8
  • Fantastic, that is SO much simpler than the scripts I found!
    – macwirb
    Commented Jul 22, 2023 at 17:39
  • Wait... this only shows the uptime of the current session. It does NOT calculate the total uptime of today. Any way to get today's total uptime?
    – macwirb
    Commented Jul 22, 2023 at 17:41
  • Somehow it doesn't seem to be the case. If you look at my output from last in the original question, you will see I have two sessions (one currently active, and one where I started and turned off the computer in the morning... a session lasting 1h56). This first session of the day is not included in neither the first or second script. Both return the same value for me.
    – macwirb
    Commented Jul 22, 2023 at 17:44
  • This will break spectacularly if the system is suspended and resumed daily. This will break spectacularly if the system is never power cycled. In short it will break more often than not. Commented Jul 22, 2023 at 19:08
  • @Artem S. Tashkinov | Are you referring to the first two commands or the bash script in the update?
    – Z0OM
    Commented Jul 22, 2023 at 19:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .