linux - 如何获取我上次使用电脑的时间

标签 linux bash shell xorg

在 GNU/Linux on xorg session 中,我想要做的是获取 self 停止使用计算机以来已经过去了多少秒(即没有按下任何键和/或移动光标)。

在后台运行时,下面的脚本将在状态栏中显示

但问题是 THE_COMMAND 是什么。

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

最佳答案

我记得不久前也问过同样的问题。 这是我发现的,

last -aiF -n 1 userName

命令可以为您提供当前 session 。

awk结合可以得到如下结果

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

这里的行是 session 时间。

这些时间算作现在 - 首次启动登录

但我相信这些并不最适合您。 所以这是一个 8 年前的问题,可能对你有帮助。

User Idle time in Linux

使用 python 你可以计算经过的空闲时间。也许这样,你就可以简单地用 python 得到你想要的东西。

关于linux - 如何获取我上次使用电脑的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67207591/

相关文章:

mysql - 如何将 bash 命令的变量值输入 MySQL?

node.js - 打开 localhost :3000 in kiosk mode after the Node. js 服务器已完成旋转

bash - 如何使用 sed 将路径字符串上的 "/"替换为 "\/"?

linux - shell 导出变量没有生效

python - 如何比较hdfs文件和unix文件?

linux - 如果每个结果使用多个命令,则无法获得 bash

linux - 理解 linux 参数和管道

python - 从 Python 启动一个可以解释函数和别名的 shell

python - 如何让 python 窗口以 "Always On Top"运行?

linux - 为什么非图片代码不能完全使用运行时修复程序进行ASLR?