0

There are two USERS on this computer: jdl and experiment

CRONTAB setup (for experiment(executed every minute):

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

From the command prompt running a script(monitor) with: (yields Inactive)

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

But from CRONTAB(running the script: monitor)... running the same script: (yields N/A) as the scope is not a particular user

How to get the screensaver status for a particular user when the script is run from CRONTAB?

jdl
  • 153
  • 4
  • Not sure, but I have 3 things to try: ☐in your script, use /usr/bin/gnome-screensaver-command rather than gnome-screensaver-command; ☐begin your script with export DISPLAY=':0.0'; ☐set $PATH at the top of your script – bitinerant Dec 23 '20 at 05:49
  • Think a better choice would be to use a systemd.timer and start it as a user service. –  Dec 23 '20 at 09:13

1 Answers1

0

SOLVED:

Using Notify-send doesn't work from crontab

NEED TO SET DBUS:

Using the gnome-session first results pid for DBUS_SESSION_BUS_ADDRESS lookup:

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

jdl
  • 153
  • 4