0

I have a simple script in python to change the desktop wallpaper. It works when executed in terminal as: ./script.py because it has a shebang for python3. But if i try running it as sudo ./script.py or try and schedule the script with cron, the script does not work. I am a newbie to linux and python, ive done some reading and think it has something to do with the PATH or env but im not sure.

Here is the python script to change my wallpaper on raspberry pi:

#!/usr/bin/env python3


import os,random
import subprocess as sub


random_pic = random.choice(os.listdir('/usr/share/rpd-wallpaper/'))

print(random_pic)

shell_command = ("pcmanfm --set-wallpaper /usr/share/rpd-wallpaper/" + str(random_pic))


# open a terminal and changes wallpaper with path of picture above

sub.call('lxterminal -e bash -c "{0}; sleep 5; echo background changed...; pwd; whoami; sleep 1; exec bash"'.format(shell_command), shell=True)

here is the crontab line i added:

*/1 * * * * env DISPLAY=:0 /usr/bin/python3 woll.py

When the script runs as sudo or called by cron every minute it gives me a weird error in terminal:

** Message: 08:41:35.152: x-terminal-emulator has very limited support, consider choose another terminal

What could be the problem is it the PATH of the program "pcmanfm" (the default file/window manager of raspberry pi) cant be found when run as sudo/cron? Does cron run as sudo? I tried debugging this by telling the same terminal to return "whoami" and it returns pi, which should work fine, as i can execute the script through a normal terminal as my normal user account "pi", not as root and the script works. Much COnfused i am.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • Can you test `which pcmanfm` and `sudo which pcmanfm`? – Roelant Aug 13 '19 at 06:49
  • Both return: /usr/bin/pcmanfm – BetweenBeltSizes95 Aug 13 '19 at 06:51
  • 1
    The solution to the proposed duplicate is to add `DISPLAY` but the OP here is already doing that. This is probably a duplicate of some other question, and probably the correct answer is "for heaven's sake don't run a *terminal* from `cron` to change your wallpaper". – tripleee Aug 13 '19 at 06:56
  • Thank you alfasin, that link helped me! <3 – BetweenBeltSizes95 Aug 13 '19 at 07:23
  • What possible benefit does the terminal bring? Just run the command directly from your Python script. – tripleee Aug 13 '19 at 07:26
  • @tripleee the python script opens terminal and tells it to runa a command to change my wallpaper, this could have been done purely as a bash script i think, but with my python script i can add more features such as download new pictures for my wallpaper from online sources not just local files which would be stupid to do purely in terminal or bash script. Python can do easy web scraping and allows me to scale this simple program and learn more about programming, not hacking around linux and its quirks ;) thanks for the help yo <3 – BetweenBeltSizes95 Aug 13 '19 at 07:37
  • Yeah but there is no reason for either Bash or Python to require a terminal to do any of this. – tripleee Aug 13 '19 at 07:48
  • @tripleee but its a terminal command which changes wallpaper, how else can i change my linux wallpaper using python without use of terminal? there isnt a universal python module which can change any systems wallpaper with ease – BetweenBeltSizes95 Aug 13 '19 at 07:58
  • Why do you think you need a terminal? The command you are attempting to run changes the wallpaper, with or without a terminal around it. (Though you should probably avoid the `shell=True` if you can; see also https://stackoverflow.com/a/51950538/874188) – tripleee Aug 13 '19 at 08:47
  • thanks tripleee you are a beast – BetweenBeltSizes95 Aug 13 '19 at 14:37

1 Answers1

0

So now the way my background changes is: crontab --> executes bash script which has special commands, see:

Pcmanfm set wallpaper fails on Raspbian stretch in cron

--> bash script executes python script --> python script opens terminal with change background command.

Thanks for the help guys! learnt a bit about linux and python along the way, who knew automatically changing a wallpaper could be so tough. Took me 2 days but damn it feels good get ready elon here i come