0

I am quite a n00b, and this task is not easy as I thought at a first glance. I am on a raspberryPi4 with Raspberry Pi OS Desktop with recommended software installed (really similar to every ubuntu, but the GPIO works from the first boot).

With success, I have written a .py program (named button_tab.py) that changes my chromium tab when a physical button, connected via GPIO pins of the raspberry, is pressed.
I need to start this .py script at the startup of the system

#name of the file: button_tab.py
import RPi.GPIO as GPIO
import subprocess
import time

GPIO.setmode(GPIO.BOARD)


buttonPin = 36

GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    buttonState = GPIO.input(buttonPin)
    if buttonState == False:
        subprocess.call(["xdotool", "key", "ctrl+Tab"])
        time.sleep(0.5) 
   

This program works great when I execute it via:

python3 button_tab.py

Every time I push the button, it changes the tab in the selected chromium window as if I push the keyboard keys ctrl+tab, and it has a cooldown of 0.5 sec in order to not trigger it multiple time at once.
I have even created a listen_for_button.sh script in order to execute it via bash.

#! /bin/sh
echo "Starting listen-for-button.py"
python3 /usr/bin/button_tab.py

When I start it manually, it works too

./listen_for_button.sh

Now the problem:
I need to start this script on the startup
, so I Installed all the pip3 modules for the sudo
(like sudo -H pip3 install subprocess.run) and started to try different solutions:

  • to run it via crontab: @reboot /home/pi/listen_for_button.sh (nope)
  • to move listen_for_button.sh in /etc/init.d/ and to abilitate it via sudo rcconf (after installing it via sudo aptitute install rcconf sysv-rc-conf) (nope)
  • thanks to @Ra, I've edited with sudo nano /etc/profile the ~/bashrc configuration file adding /bin/bash /home/pi/listen_for_button.sh at the bottom of the file (something moves, but the login stuck to my process probably, because no desktop is shown, only a black screen)
  • to build /lib/systemd/system/button_tab.service (spoiler... nope)

#the button_tab.service file:

[Unit]
Description=Button Tab Service
After=multi-user.target
Conflicts=getty@tty1.service
DefaultDependencies=false

[Service]
Type=simple
Environment="PATH=/home/{{ user }}/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin"
ExecStart=/usr/bin/python3 /usr/bin/button_tab.py
Restart=on-abort
User=pi
    
[Install]
WantedBy=multi-user.target

and to enable it via:

sudo systemctl daemon-reload
sudo systemctl enable button_tab.service
sudo systemctl start button_tab.service

The process works (green light when asking for the status via sudo systemctl status button_tab.service) but the button doesn't work even if the process is running. I have tried different construction of the .service file: this is the last version. All the changes to the .service file have deal to the same result: nope

The only way that I have found to make the button works is to launch manually or the button_tab.py script or the listen_for_button.sh script.

None of the automated ways works.

Probably I am missing something basic, like the behaviour of the xdotool... I really don't know!

Please, I invoke the sudo help
Thanks

6
  • Perhaps I'm misunderstanding things, but I don't think you should be running such a script when the system starts up. Shouldn't you run it when you log in? If so, perhaps you can put it in your ~/.bashrc file? Also, I don't think you should put it in the root account; it should be the user account (I can't remember if Ubuntu on the Raspberry Pi makes a distinction). – Ray Feb 26 at 10:08
  • @Ray super thank you for the comment! I am trying to build something like a kiosk, so with the push of a physical button, the user can change the webpage shown. This is why I need to auto-start the script – Pnz Feb 26 at 10:10
  • I'll give a look to the ~/.bashrc documentation. Thanks – Pnz Feb 26 at 10:13
  • I see. I've never ran Ubuntu on a Raspberry Pi before (I've used Raspberry Pi OS). But for a plain computer, it (1) starts up and (2) the user login. Since you mentioned chromium, I presume this is something that is being done at the user level. So, any cronjob with @reboot seems way too early. Likewise, I think making it into a service isn't necessary if the kiosk has only one user logged in every. There's a lot of information to look at, but maybe this on the Bash startup files will help? – Ray Feb 26 at 10:18
  • LoL something is moving. I have added to the file /etc/profile at the bottom of the file the string: /bin/bash /home/pi/listen_for_button.sh (via nano) the result is that the system doesn't log in anymore LoL. The startup stops to a black screen. I solved booting in safe mode via root. (adding S to the cmdline.txt file in boot partition). Probaby it happens because there is no exit condition in my .py file (because I want it to continue working...) – Pnz Feb 26 at 10:48
0

SOLVED! Thanks to the @Ray comments, was enough to do this:

sudo nano /etc/profile

and add to the bottom of the file:

nohup /bin/bash /home/pi/listen_for_button.sh

Doing this, the ~/bashrc execute the .sh file in the background, with no output at all. So the login process can continue smoothly and the .sh file act like if it is executed by the user <3

I hope that this can help someone else. All the best

1
  • FYI, you can put #!/bin/bash at the top of the file and then you can remove /bin/bash from the command. Similarly, if you want output, you can send it to a file for debugging, if that's needed. Good to hear you solved your problem! – Ray Feb 27 at 3:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.