如何将 gnome-screensaver-command 信息传递给 CRONTAB 调用的脚本

如何将 gnome-screensaver-command 信息传递给 CRONTAB 调用的脚本

这台电脑上有两个用户:jdl 和 experiment

CRONTAB 设置(用于实验(每分钟执行一次):

USER=experiment
HOME=/home/experiment
SHELL=/bin/bash
PATH=/home/experiment/bin:/home/experiment/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
XAUTHORITY=/home/experiment/.Xauthority
DISPLAY=:0

*/1 * * * * /home/experiment/monitor

从命令提示符运行脚本(监视器):(产生非活动状态)

if (gnome-screensaver-command -q | grep "is inactive"); then
    # Inactive screensaver
    echo "screensaver: Inactive">>log_log.txt
elif (gnome-screensaver-command -q | grep "is active"); then
    # Active screensaver
    echo "screensaver: Active">>log_log.txt
else
    # N/A active screensaver
    echo "screensaver: N/A">>log_log.txt
fi

但是从 CRONTAB(运行脚本:监视器)...运行相同的脚本:(产生 N/A)因为范围不是特定用户

当脚本从 CRONTAB 运行时,如何获取特定用户的屏幕保护程序状态?

答案1

解决了:

使用crontab 中的 Notify-send 功能无法运行

需要设置DBUS:

使用 gnome-session 第一个结果 pid 进行 DBUS_SESSION_BUS_ADDRESS 查找:

source /home/experiment/.bashrc
pid=$(pgrep -u experiment gnome-session | head -n 1)
dbus=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ | sed 's/DBUS_SESSION_BUS_ADDRESS=//' )
export DBUS_SESSION_BUS_ADDRESS=$dbus
export HOME=/home/experiment
export DISPLAY=:0
export USER=experiment


if (gnome-screensaver-command -q | grep "is active"); then
    echo $(date) :::: $USER :::: Active>> $HOME/crondate.txt 
    uu="user: $USER   date: $(date)   screensaver: ACTIVE"
elif (gnome-screensaver-command -q | grep "is inactive"); then
    echo $(date) :::: $USER :::: InActive>> $HOME/crondate.txt 
    uu="user: $USER   date: $(date)   screensaver: INACTIVE"
else
    echo $(date) :::: $USER :::: N/A>> $HOME/crondate.txt 
    uu="user: $USER   date: $(date)   screensaver: N/A"
fi

相关内容