Running a python script on a raspberry pi
-
Of course. The python script was simply this to begin with:
import subprocess subprocess.call(["xdotool", "mousemove", "945", "132"])
I basically took it from here on stackoverflow - https://stackoverflow.com/questions/9681959/how-can-i-use-xdotool-from-within-a-python-module-script/44608709. This was just something basic for now to make sure it was working okay with TriggerCMD. The script executes and the cursor moves when called from console or through an editor. With TriggerCMD, it doesn't get launched for some reason, but the append to txt file still happens.
-
@daproduct, I think I found the problem.
Here's how to fix it:
- In your bash script, add this command before it runs your Python script:
export DISPLAY=:0.0 - In one of your bash shells running as the pi user, run:
xhost +
By default, root doesn't have a DISPLAY variable set, so xdotool doesn't know what X windows to talk to.
X windows also doesn't allow other users to interact with the X windows desktop by default. That xhost + command allows it.
One thing to keep in mind - if you're running a command in the background service, it will run as root, so will have root's environment variables. The pi user has a DISPLAY variable set, but root doesn't have it by default.
- In your bash script, add this command before it runs your Python script:
-
Thank you kindly Russ. It is working perfectly now and this is all pretty neat so far. For the bash script issue, I was putting the "export DISPLAY" line in the wrong place.
I just had one question. Is it possible to chain these commands in one line in the TriggerCMD command line? so for example "command":"export DISPLAY=:0.0 | xdotool mousemove 500 500" or something along those lines. I was just wondering for the sake of minimizing the number of scripts for certain things.
-
@daproduct, good question. I think this should work:
DISPLAY=:0.0; xdotool mousemove 500 500
-
Thank you Russ. It works as expected and I was able to write in the commands. They are all working. I just needed to add export to the command. However, I am having one problem with chromium-browser though. When it is launched, it's launched in root I believe because all the bookmarks and everything are not there. I had the same issue with a media player but I was able to launch it with this command no problem:
"command":"echo pi | sudo -S su pi-c '/usr/bin/<app-location>'"
I tried the same command with chromium (and with --no-sandbox) and nothing happens (also tried with the export DISPLAY command). Is there a way to do this at all? I just wanted to launch a website from user and still have access to what I saved on the user chromium? I tried --user-data-dir and --add-user commands as well.
-
@daproduct, this worked for me:
su - pi -c 'export DISPLAY=:0.0; /usr/bin/chromium https://google.com'
-
@Russ Thank you kindly Russ. Everything is running properly and I have been able to program in what I wanted. Thank you and I apologize for the trouble. I don't know how many times you have dealt with these issues specifically
-
@daproduct , no problem. Thanks for sharing your TRIGGERcmd use cases.
-
-
Hello Russ,
I hope you are doing well! I had an issue arise with "xhost +". Previously, my scripts would run using xdotool without issue. I would simply say the particular command (through assistant or wherever) and the xdotool command would be executed. Now, I can only get the xdotool commands executed after I type "xhost +" in the linux command terminal, but not after the initial startup of the Raspberry Pi.
I am curently running a Raspberry Pi 4 with debian (i can't recall which version but can provide the specifics if necessary). I recently updated through apt-get etc and that is when the issue arose. Thank you in advance and any help would be much appreciated.
-
@daproduct, please try this:
Add this line to your /etc/profile file:
xhost +
I think that will make it persist.
-
@Russ
Thank you Russell. I had xhost + in that location (ie. etc/profile and at the end of the file) and I believe I had it there based on your previous instructions. I tinkered around further this evening and I found that problem only occurs after an initial startup. If the command is started again after occurring a start-up (ie. run again after startup) then it works without issue. I tried to circumvent the issue with a delay and that did not work. I found a workaround of commanding the script to run again, but I wanted to get the script working as it previously was and if it was possible. I was wondering if you happen to know why this might be the case. Thank you in advance -
@daproduct, I'm not sure why that didn't work. I thought what's in /etc/profile runs for every user, so when your pi user logs in it should run. Have you tried adding it to pi's ~/.bashrc file? That's another thing you could try.
If it still doesn't work, let me know. I'll setup one of my Pi's with XWindows and see if I can figure it out.
-
@Russ
Thank you again Russ. Unfortunately, that did not work either. The script only executes after run it again. -
@daproduct, can you share your scripts and commands.json entry? I'll see if I can reproduce the problem and solve it this weekend.
-
@Russ
Hello again Russ. I have the command simply running from the commands.json. The command is just the one you had helped me with previously. The command opens a website, waits for a few seconds, and then its a xdotool command.The command I am trying to run is:
"su - pi -c 'export DISPLAY=0.0; /usr/bin/chromium-browser https://www.yt.com/subscriptions/; sleep 5; xdotool mousemove 300 400; xdotool click 1;'"The first its run after startup, nothing happens. If I run the same command again, it executes without issue. I tried to tinker with it this weekend to save some time on diagnostics, but the same issue continues to persist.
-
I tested it on my Raspberry Pi, except I used DISPLAY=:0 instead of DISPLAY=0.0.
"su - pi -c 'export DISPLAY=:0; /usr/bin/chromium-browser https://www.yt.com/subscriptions/; sleep 5; xdotool mousemove 300 400; xdotool click 1;'"
It worked every time after I ran triggercmdagent as root on my Raspberry Pi.
You wrote, "The first its run after startup, ..." What's startup? Do you mean startup of the triggercmd agent, or startup of the Raspberry Pi?
Edit: I just realized you're probably having your RPi login automatically as pi, and you've installed the background agent. I'll try that next.
I tried it and it works every time for me. I installed the background agent with this command:
/usr/share/triggercmdagent/app/src/installdaemon.sh
I switched the "ground" field to "background" of course, so my entry looks like this:
{ "trigger": "Test", "command": "su - pi -c 'export DISPLAY=:0; /usr/bin/chromium-browser https://www.yt.com/subscriptions/; sleep 5; xdotool mousemove 300 400; xdotool click 1'", "ground": "background", "voice": "test", "allowParams": "false" }
I also added xhost + to the end of my /etc/profile so it's enabled during boot.
-
@Russ
Thank you Russ for looking into it.With the startup, I was referring to the RPI starting / booting up from being turned off. I noticed that the issue only happens from the RPI starting up and on the first instance a command is run. The next time a command is run, the command executes without issue. The main problem is the xdotool command not being executed on this first instance. However, the page is loaded.
Also, I do have TriggerCMD installed as a background agent using that command and I do have xhost+ addeed to the end of the /etc/profile