Running a python script on a raspberry pi
-
@daproduct, this worked for me on my Raspberry Pi:
{"trigger":"python test","command":"python3 /root/test.py","ground":"background","voice":""},
I also tried it with python 2.7 with this commands.json entry:
{"trigger":"python test","command":"python /root/test.py","ground":"background","voice":""},
This is my test python script:
f = open("/tmp/test.txt", "a") f.write("Now the file has more content!") f.close()
Maybe you're setting the ground to foreground? Usually on a Raspberry Pi it needs to be background because it's running in the background service.
-
Thank you Russ. I can't either command to work (both the python3 or python command). I saw these commans from your posts using that command from the LED strips and I tried it based on that awhile ago. I tried it again, just to be sure. The python script runs when I test it in the console and I know the command is being triggered, but still nothing happens on the PI. I did all the commands on background as well.
The script I had running is simply to perform a button click (actually just to pause a video). I even tried a script that outputs appends to a file but still nothing. I even tried EXPORT.DISPLAY=0.0 to no avail.
I assumed the issue was down to the agent being in root and the applications being under the user (based on previous posts). I previously had an issue of launching applications but I found the following command did the trick
"command":"echo user | sudo -S su user -c '<app-location>'"
For some reason though, this doesn't work for python scripts. So <'python script.py'> or even <'python' script.py> or with the python location obtained from "which python". It is the same with python3. I even tried bash scripts and still nothing.
-
@daproduct, you wrote, "I even tried bash scripts and still nothing." Do you mean you had the TRIGGERcmd agent run a bash script, and the bash script should have ran the python script, and it didn't? I'm glad you tried that because I was going to suggest it.
My suggestion at this point is to repeat that experiment, but keep the bash and python script super simple, and output to a file in /tmp from both so you can see if either one ran. So you could make this your bash script:
#!/bin/bash echo "Hello from bash" >> /tmp/bash.log /usr/bin/env python3 /root/test.py
You could use the test.py example above. It's simple and writes a log file.
Don't forget to make the bash script executable with chmod +x test.sh
If we can get a super simple python script to run, that would narrow down the problem.
-
Thank you again Russ. I have some good news to report. I forgot to make the bash script executable. Now, the test.py works and the .txt file is appended in the tmp folder. However, there is now a problem with a particular line of code not executing.
So after the test.py worked, I tried to change the script to just having an xdotools command. I tried it and that particular line of code is not being executed. I then tried the original test.py with the line added at the bottom. The script appended into the tmp folder again but did not execute the line of code with xdotools in it.
Thank you for the help so far though. I am glad that at least the appending to file is working now
-
@daproduct, that's good news.
If you want to share your python script I might be able to figure out what's happening with xdotools.
-
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.