2

Script lck.sh checks for presence of file_name.txt , removes the file and locks the screen. This works perfectly when the script is run from terminal.

FILE=/path_to_file/file_name.txt
if test -f "$FILE"; then
    rm $FILE
    /usr/bin/gnome-screensaver-command -l
fi

However, when crontab runs the scripts, the file gets deleted, but gnome-screensaver-command shows no effect. The last line of crontab reads: I appended the crontab with the following line

* * * * * sh /etc/profile.d/lck.sh

I use ubuntu 16.04

kksagar
  • 201
  • 1
  • 10
  • Likely it's for the same reason as here [Background not changing using gsettings from cron](https://askubuntu.com/questions/742870/background-not-changing-using-gsettings-from-cron) i.e. that the `cron` environment doesn't provide access to your user's desktop session – steeldriver Jul 12 '19 at 13:09
  • that's right. It worked – kksagar Jul 12 '19 at 13:27
  • This script uses a different method to lock screen: https://askubuntu.com/questions/837078/application-that-will-lock-screen-after-a-set-amount-of-time-for-ubuntu?noredirect=1&lq=1 – WinEunuuchs2Unix Jul 14 '19 at 21:45

1 Answers1

1

Your problem is due to a difference in "execution environments".

Your shell script, when run "from terminal" (under the GUI environment), has several environment variables defined in its execution environment that point to the screen you're trying to lock.

When you run from cron (a non-GUI environment), these variables are not defined, and /usr/bin/gnome-screensaver-command cannot find the screen to lock.

You can cheat, and pass GUI information to your cron environment by defining the DISPLAY environment variable in your script, near the beginning:

# check with echo $DISPLAY in GUI
export DISPLAY=${DISPLAY:-":0"}

You can compare execution environments by executing the following command in each environment:

(echo "=== set ===";set;echo "===env ==="; env | sort;echo "=== alias ===";alias) 

redirect the output to a file, then compare (diff) with other environments's results.

Here's how I found out about /usr/bin/gnome-screensaver-command:

walt@bat:~(0)$ strings /usr/bin/gnome-screensaver-command | grep DISPLAY
walt@bat:~(1)$ ldd /usr/bin/gnome-screensaver-command
    linux-vdso.so.1 =>  (0x00007ffdda145000)
    libgio-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0 (0x00007faac6507000)
    libgobject-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0 (0x00007faac62b4000)
    libglib-2.0.so.0 => /lib/x86_64-linux-gnu/libglib-2.0.so.0 (0x00007faac5fa3000)
    libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007faac5bd9000)
    libgmodule-2.0.so.0 => /usr/lib/x86_64-linux-gnu/libgmodule-2.0.so.0 (0x00007faac59d5000)
    libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007faac57bb000)
    libselinux.so.1 => /lib/x86_64-linux-gnu/libselinux.so.1 (0x00007faac5599000)
    libresolv.so.2 => /lib/x86_64-linux-gnu/libresolv.so.2 (0x00007faac537e000)
    libffi.so.6 => /usr/lib/x86_64-linux-gnu/libffi.so.6 (0x00007faac5176000)
    libpcre.so.3 => /lib/x86_64-linux-gnu/libpcre.so.3 (0x00007faac4f06000)
    libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007faac4ce9000)
    /lib64/ld-linux-x86-64.so.2 (0x00007faac688f000)
    libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007faac4ae5000)
walt@bat:~(0)$ grep -l DISPLAY /usr/bin/gnome-screensaver-command /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0 /usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0 /lib/x86_64-linux-gnu/libglib-2.0.so.0 /lib/x86_64-linux-gnu/libc.so.6 /usr/lib/x86_64-linux-gnu/libgmodule-2.0.so.0 /lib/x86_64-linux-gnu/libz.so.1 /lib/x86_64-linux-gnu/libselinux.so.1 /lib/x86_64-linux-gnu/libresolv.so.2 /usr/lib/x86_64-linux-gnu/libffi.so.6 /lib/x86_64-linux-gnu/libpcre.so.3 /lib/x86_64-linux-gnu/libpthread.so.0 /lib/x86_64-linux-gnu/libdl.so.2
/usr/lib/x86_64-linux-gnu/libgio-2.0.so.0
walt@bat:~(0)$ strings /usr/lib/x86_64-linux-gnu/libgio-2.0.so.0 |grep DISPLAY
_u == G_FILE_ATTRIBUTE_ID_STANDARD_DISPLAY_NAME
Cannot autolaunch D-Bus without X11 $DISPLAY
DISPLAY
walt@bat:~(0)$ 
waltinator
  • 35,099
  • 19
  • 57
  • 93