1

I have this script to change the background and screensaver from my gnome desktop. Works fine when executing manually, but when I put it in cron it doesn't execute it. The file is executable.

I added the cron job with crontab -e.

This is the script:

#!/bin/bash

# change_background - Change desktop background and lockscreen background randomly

# Export DBUS_SESSION_BUS_ADDRESS environment variable
euid=$(id --real --user)
pid=$(pgrep --euid $euid gnome-session)
export DBUS_SESSION_BUS_ADDRESS=$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$pid/environ|cut -d= -f2-)

# Wallpapers directory
dir="/home/myuser/Pictures/Wallpapers"

# Wallpaper and screensaver files
background=$(ls $dir/* | shuf -n1)
screensaver=$(ls $dir/* | shuf -n1)

# Set the wallpaper and screensaver
gsettings set org.gnome.desktop.background picture-uri file://$background
gsettings set org.gnome.desktop.screensaver picture-uri file://$screensaver

My script is in my bin directory /home/myuser/bin which is added to the PATH variable.

crontab -l output:

# ┌───────────── minute (0 - 59) 
# │ ┌───────────── hour (0 - 23) 
# │ │ ┌───────────── day of month (1 - 31) 
# │ │ │ ┌───────────── month (1 - 12) 
# │ │ │ │ ┌───────────── day of week (0 - 6) (Sunday to Saturday; 
# │ │ │ │ │ 7 is also Sunday on some systems) 
# │ │ │ │ │ 
# │ │ │ │ │ 
# * * * * * command
#
# --- Change background every minute --- #
#
* * * * * change_background 
#
# --- ------------------------------ --- #

My question is: why cron is not executing my script? what I'm doing wrong?.

  • Note that if you added /home/myuser/bin to PATH in myuser's .bashrc file (or .bash_profile or similar), then this may not be parsed. Instead, modify PATH in the crontab. Do you get any errors from the cron daemon emailed to you? (these ought to have been sent to the myuser account). – Kusalananda Feb 5 at 21:23
  • Where do you modify PATH? What errors do you get? – Kusalananda Feb 5 at 21:50
  • @Kusalananda thank you. I checked the email file which is located in /var/spool/mail/myuser and the output says that it doesn't found the command, as you said the problem was in the PATH variable. So with crontab -e I added the correct PATH and also set the SHELL variable to /bin/bash and now cron execute the script. – Andrés Ruiz Feb 5 at 22:12
1

The issue seems to have been that the environment in the crontab was not set up with a correct PATH, so the script was never found. The user's shell initialisation files are not run by cron, so setting the PATH or other variables therein is useless for a cron job.

This can be solved in a number of ways.

One is to simply set PATH (and any other variables that needs specific values) in the crontab (this would also change the value of these variables for the script and all other jobs in the crontab):

PATH=/home/myuser/bin:$PATH

Another is to execute the script with an absolute path:

* * * * * /home/myuser/bin/change_background

This may be preferable if other jobs are executed that need an individually modified PATH variable for specific things that the scripts themselves are using (the scripts themselves would then set PATH early on, or be started with e.g. env PATH=... /some/path/program).

  • Yes that was the problem, thank you!. I'll better go with the absolute path. – Andrés Ruiz Feb 5 at 22:24
0

try :

   * * * * * env DISPLAY=:0 /path/to/bash/script.sh 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.