Search code examples
bashcronpcmanfm

Changing wallpaper using pcmanfm by crontab


I made a simple bash script that changes the wallpaper for a random picture from my wallpapers directory using pcmanfm. It's something like that:

#!/bin/bash

pcmanfm -w "$(find /home/likewise-open/MAPS/lucas.cardeal/Pictures/Wallpapers -type f | shuf -n1)"

I want that automatically, so u put the script on crontab. But it has no effect when its called by crontab. What's wrong with my script? How can I fix it?

Thanks


Solution

  • That script will give you a X11 authorization error when is set as cron job. To prevent this, just add export DISPLAY=:0 and export XAUTHORITY=/home/username/.Xauthority (change username with your user name) in your script:

    #!/bin/bash
    
    export DISPLAY=:0
    export XAUTHORITY=/home/username/.Xauthority   #change `username` with your user name
    
    pcmanfm -w "$(find /home/likewise-open/MAPS/lucas.cardeal/Pictures/Wallpapers -type f | shuf -n1)"

    ADDENDUM: An update caused the above script to break in Lubuntu 16.04 and above. See this stackoverflow answer https://stackoverflow.com/a/46259031/5895207 for the additional environment variable that needs to be specified in the script.