nino
nino

Reputation: 678

How to get the time when I last used the computer

In GNU/Linux on xorg session, what I want to do is to get how many seconds have passed since I stopped working with the computer (i.e. no keys pressed and/or cursor moved).

Running in the background, the script below will display secs in the status bar.

But the question is what THE_COMMAND will be.

While true; do
    last_touched="$(THE_COMMAND)"
    now="$(date +%s)"
    secs=$((now - last_touched))
    echo "${secs} seconds ago"
    sleep 3
done

Upvotes: 2

Views: 302

Answers (1)

Catastrophe
Catastrophe

Reputation: 350

I remember asking the same question a while back. Here is what I found,

last -aiF -n 1 userName

command can give you the current session.

When combined with awk you can get the result as follows

$ last -aiF -n2 username
username  :1           Wed Apr 21 13:09:00 2021   still logged in                       0.0.0.0
username  :1           Wed Apr 21 07:28:47 2021 - down                      (05:39)     0.0.0.0


$ last -aiF -n 2 ogulcan | awk '{print $10}'
in
(05:39)

the lines here are the session times.

These times are counted as now - first boot login

But I believe these does not work best for you. So here is the 8 year old question that may be helpful to you.

User Idle time in Linux

Using python you can calculate the idle time passed. Maybe this way, you can simply get what you want with python.

Upvotes: 2

Related Questions