答案1
这就是我解决问题的方法(这将在空闲时间至少 30 分钟后注销空闲用户):
首先创建一个文件并将此代码放入其中(您可能需要在开始之前安装 xprintidle):
#!/bin/bash
# Written by cz0 2010, adapted by dror 2013
# Distributed under the terms of the GNU General Public License v2
HALFHOUR=1800000
IDLETIME=`xprintidle`
QDBUS="/usr/bin/qdbus"
if [ $IDLETIME -gt $HALFHOUR ]
then
logger timeout of $HALFHOUR expired. idle is $IDLETIME
KDEPID=$(ps aux | grep 'startkde' | grep -v 'grep' | awk '{print $2}')
KDEUSER=$(ps u $KDEPID | grep 'startkde' | awk '{print $1}')
# If the DBUS_SESSION_BUS_ADDRESS environment variable is not already set correctly
# then set it by finding the environment file for the startkde process in proc and
# parsing it to get get the correct setting.
if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then
ENVIRON_FILE=/proc/$(ps h --ppid $KDEPID -o pid | awk '{print $1}')/environ
CURRENT_DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS $ENVIRON_FILE | sed -e 's/DBUS_SESSION_BUS_ADDRESS=//')
export DBUS_SESSION_BUS_ADDRESS=$CURRENT_DBUS_SESSION_BUS_ADDRESS
fi
$QDBUS org.kde.ksmserver /KSMServer logout 1 0 2
else
logger timeout is $HALFHOUR not expired $IDLETIME
fi
现在将其复制到 /bin/
sudo cp myfile /bin/logoutonidle
接下来使其可执行
sudo chmod +x /bin/logoutonidle
接下来针对您想要自动注销的用户运行(如果需要,则对每个用户运行它):
crontab -e
并添加以下行(这将使 cron 每 15 分钟检查一次,因此最坏的情况是用户将在约 45 分钟空闲时间后被注销):
*/15 * * * * export DISPLAY=:0 && /bin/logoutonidle 2>&1