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
  • 211
  • 1
  • 10

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
  • 36,399