[SOLVED] Can't get screensaver script to run at start up

Questions about applications and software
Forum rules
Before you post please read how to get help
Post Reply
Huxley
Level 1
Level 1
Posts: 7
Joined: Fri Aug 17, 2018 3:54 pm

[SOLVED] Can't get screensaver script to run at start up

Post by Huxley » Sun Feb 10, 2019 6:02 pm

Linux Mint Mate 19

Hi,
I'm trying to run a script (screensaver.sh) which uses 'feh' and 'xprintidle' to act as a slideshow screensaver. The script is required to be in the same directory as the images to be used in the slldeshow, which on my pc is in Pictures/Wallpapers/. I have given the script the permissions 755, and have disabled the mate-screensaver.

To run screensaver.sh in terminal I use the command:

Code: Select all

 ~/Pictures/Wallpapers/screensaver.sh n
where n is the number of seconds of inactivity before the screensaver starts. When 'n' is omitted, the default time of 60 seconds is applied. This runs perfectly without issues. However I can't make it work as a cron job.

I've tried (as user as root):

Code: Select all

crontab -e
@reboot /full/path/to/screensaver.sh
@reboot /full/path/to/screensaver.sh n e.g. 60
@reboot '/full/path/to/screensaver.sh 60'
@reboot "/full/path/to/screensaver.sh 60" 
I've also tried using a sleep delay for all of the above to make sure my DE was fully up and unnng before starting the script, e.g.

Code: Select all

@reboot sleep 90 && /full/path/to/screensaver.sh
None of which worked. I then tried adding the full path of the script to 'Startup Applications Preferences' GUI. This did work, but without being able to set 'n' I'm stuck with the screensaver starting after the default 60 seconds, which is too fast. I tried adding 'n' after /screensaver.sh and putting the entire string into single or double quotation marks, but neither helped.

Thirdly, I found that it's possible to autostart a script by placing it in:

Code: Select all

/etc/rc.d/rc.local
However, I don't have /etc/rc.d/. I have:

Code: Select all

/rc0.d, rc1.d rc2.d ....rc6.d
None of which contain a /rc.local/ diectory

Any ideas what I'm doing wrong?

Here is the screensaver.sh script:

Code: Select all

#!/bin/bash
# screensaver.sh

# url: ###
# This script is licensed under GNU GPL version 2.0 or above

# Uses elements from lightsOn.sh
# Copyright (c) 2011 iye.cba at gmail com
# url: https://github.com/iye/lightsOn
# This script is licensed under GNU GPL version 2.0 or above

# Description: ####
# screensaver.sh needs xprintidle and feh to work.

# HOW TO USE: Start the script with the number of seconds you want the checks
# for fullscreen to be done. Example:
# "./screensaver.sh 120"

# enumerate all the attached screens
displays=""
while read id
do
    displays="$displays $id"
done< <(xvinfo | sed -n 's/^screen #\([0-9]\+\)$/\1/p')

DIR=$( cd "$( dirname "$0" )" && pwd)
delay=$1

checkFullscreen()
{

    # loop through every display looking for a fullscreen window
    for display in $displays
    do
        #get id of active window and clean output
        activ_win_id=`DISPLAY=:0.${display} xprop -root _NET_ACTIVE_WINDOW`
        activ_win_id=${activ_win_id:40:9}
        
        # Check if Active Window (the foremost window) is in fullscreen state
        isActivWinFullscreen=`DISPLAY=:0.${display} xprop -id $activ_win_id | grep _NET_WM_STATE_FULLSCREEN`
            if [[ "$isActivWinFullscreen" != *NET_WM_STATE_FULLSCREEN* ]];then
                feh -x -F -r -Y -Z -z -A slideshow -D 7 -d "$DIR"
	    fi
    done
}

# If argument empty, use 60 seconds as default.
if [ -z "$1" ];then
    delay=60
fi

# If argument is not integer quit.
if [[ $1 = *[^0-9]* ]]; then
    echo "The Argument \"$1\" is not valid, not an integer"
    exit 1
fi

IDLE_TIME=$(($delay*1000))

cd "$DIR"
while sleep $((1)); do
    idle=$(xprintidle)
    if [ $idle -ge $IDLE_TIME ]; then
        checkFullscreen
    fi
done
Last edited by Huxley on Mon Feb 11, 2019 9:30 am, edited 1 time in total.

User avatar
smurphos
Level 13
Level 13
Posts: 4610
Joined: Fri Sep 05, 2014 12:18 am
Location: Britisher
Contact:

Re: Can't get screensaver script to run at start up

Post by smurphos » Mon Feb 11, 2019 3:43 am

In Cinnamon it works fine from Startup applications. I've named my copy of the script fehscreensaver.sh and used a very short idle delay for testing purposes.

Image

The associated desktop file that is created.
~/.config/autostart/fehscreensaver.desktop

Code: Select all

[Desktop Entry]
Type=Application
Exec=/mnt/backup_drive/Pictures/Wallpapers/Desktop/City_&_Modern/fehscreensaver.sh 5
X-GNOME-Autostart-enabled=true
NoDisplay=false
Hidden=false
Name[en_GB]=fehscreensaver
Comment[en_GB]=No description
X-GNOME-Autostart-Delay=20
This also works fine and would be needed if the path included any spaces.

Code: Select all

Exec="/mnt/backup_drive/Pictures/Wallpapers/Desktop/City_&_Modern/fehscreensaver.sh" 5
Why is it not working from a crontab?

Using a root crontab will be one issue. The script appears to be looking for displays associated with the user running the script. There won't be any associated with root. I suspect it wouldn't work from a user crontab either without specifying environment variables ...

Anyway an autostart entry is the right place for this as I think it might have issues running if it kicks off before a user has logged in.

Alternatively just edit the default display value in the script so it's not 60.

# If argument empty, use 60 seconds as default.

Code: Select all

if [ -z "$1" ];then
    delay=60
fi
For custom Nemo actions, useful scripts for the Cinnamon desktop, and Cinnamox themes visit my Github pages.

Huxley
Level 1
Level 1
Posts: 7
Joined: Fri Aug 17, 2018 3:54 pm

Re: Can't get screensaver script to run at start up

Post by Huxley » Mon Feb 11, 2019 9:30 am

Thanks, smurphos. I amended ~/.config/autostart/screensaver.sh.desktop with the line:

Code: Select all

 X-MATE-Autostart-Delay=30 
and now everything works great.

I kicked myself for not finding the default delay in the script, but will pay closer attention next time.
Thanks again for your help.

Post Reply

Return to “Software & Applications”