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.