从Linux上的预定任务检测不活动。

时间:2022-08-03 02:32:09

I need to detect user inactivity on my Linux system, to poweroff it (quite headless wife, and quite expensive electric bills... :-).

我需要检测我的Linux系统上的用户不活跃,以关闭它(相当无头的妻子,相当昂贵的电费……):-)。

I need to schedule the script (in crontab), so no X-depending tool will work, I suppose (no $DISPLAY available).

我需要安排脚本(在crontab中),所以没有x -依赖工具可以工作,我想(没有$DISPLAY可用)。

Any thoughts?

任何想法吗?

UPDATE

更新

For "user inactivity" I mean user input inactivity (mouse and keyboard).

对于“用户不活动”,我指的是用户输入不活动(鼠标和键盘)。

2 个解决方案

#1


2  

Xautolock may the right tool for you. It allows you to specify a amount of minutes of inactivity after which a command should get triggered.

索托洛克可能是你的合适工具。它允许您指定一段不活动的时间,在此之后应该触发一个命令。

#2


1  

You might consider checking how long the screen saver has been running.

您可以考虑检查屏幕保护程序运行了多长时间。

#!/bin/bash

screensaver="atlantis"

t=$(
    # check for the screensaver
    ps h -o start -C $screensaver          |\
    # hh:mm:ss -> seconds
    awk -F: '{print $1"*3600+"$2"*60+"$3}' |\
    bc -l  2>/dev/null  | sort -n | tail -1
)

if [ "$t" == "" ]
then
    exit 0
fi

n=$(
    date "+%T"                             |\
    awk -F: '{print $1"*3600+"$2"*60+"$3}' |\
    bc -l  2>/dev/null
)

runtime=$(( $n - $t ))

if [ $runtime -gt 3600 ] || [ $runtime -lt 0 ]
then
    echo shutdown -h now 
fi

Using the time value requires subtracting now from then to get the run time. Also, in my case, the screensaver program which appears in the process table will vary depending on which screensaver is selected. So, the above program assumes that 'atlantis' is the current screen saver.

使用时间值需要现在减去时间才能获得运行时间。同样,在我的例子中,出现在过程表中的屏幕保护程序将根据选择的屏幕保护程序而变化。因此,上面的程序假设“亚特兰蒂斯”是当前的屏幕保护程序。

#1


2  

Xautolock may the right tool for you. It allows you to specify a amount of minutes of inactivity after which a command should get triggered.

索托洛克可能是你的合适工具。它允许您指定一段不活动的时间,在此之后应该触发一个命令。

#2


1  

You might consider checking how long the screen saver has been running.

您可以考虑检查屏幕保护程序运行了多长时间。

#!/bin/bash

screensaver="atlantis"

t=$(
    # check for the screensaver
    ps h -o start -C $screensaver          |\
    # hh:mm:ss -> seconds
    awk -F: '{print $1"*3600+"$2"*60+"$3}' |\
    bc -l  2>/dev/null  | sort -n | tail -1
)

if [ "$t" == "" ]
then
    exit 0
fi

n=$(
    date "+%T"                             |\
    awk -F: '{print $1"*3600+"$2"*60+"$3}' |\
    bc -l  2>/dev/null
)

runtime=$(( $n - $t ))

if [ $runtime -gt 3600 ] || [ $runtime -lt 0 ]
then
    echo shutdown -h now 
fi

Using the time value requires subtracting now from then to get the run time. Also, in my case, the screensaver program which appears in the process table will vary depending on which screensaver is selected. So, the above program assumes that 'atlantis' is the current screen saver.

使用时间值需要现在减去时间才能获得运行时间。同样,在我的例子中,出现在过程表中的屏幕保护程序将根据选择的屏幕保护程序而变化。因此,上面的程序假设“亚特兰蒂斯”是当前的屏幕保护程序。