0

I'm trying to run a python script from a headless raspberrypi that uses the pynput library. In order to use pynput on a headleass machine it requires you to do the following steps on every bootup:

export DISPLAY=:0 
DISPLAY=:0 python -c 'import pynput'

If I don't do this I get the following error:

ImportError: this platform is not supported: ('failed to acquire X connection: >

Try one of the following resolutions:

  • Please make sure that you have an X server running, and that the DISPLAY env>

If I now try to automaticate this process through a bash script that contains the following lines:

#!/bin/bash
    sleep 60
    export DISPLAY=:0
    sudo chmod 666 /dev/hidg0
    DISPLAY=:0 python -c 'import pynput'
    python3 /home/pi/Hid.py |& tee -a /home/pi/test.log
exit 0

And execute the file normally like this from terminal:

bash /home/pi/bash.sh

it works perfectly fine. But when I mention the script in the rc.local file the same way I executed it before, the script is getting executed on boot, but the python script throws the error that was shown at the beginning. How can there be two different outcomes, when it's getting executed by hand and when it's getting executed by a bash script at boot via. rc.local? Any help is appreciated!

3
  • Why not replace the boot script with the one that works? Anyway by the error message you should be able to infer that the script cannot connect to the X server, most likely because it wasn't already started. Commented Jun 15, 2022 at 2:34
  • I use the same script everytime. I have two Scenarios: 1. Execute the script by hand from terminal => Works 2. Executing the script on boot by rc.local => Error That's my problem. Commented Jun 15, 2022 at 2:40
  • 1
    You need to ensure this script starts after the X server is started. Does your init system have dependencies? You may need to make use of that to have it depend on X server started first. Commented Jun 15, 2022 at 2:56

1 Answer 1

1

All commands from rc.local are getting executed by user root. To execute them from a different user use:

sudo su USERNAME -c 'SCRIPT/PATH'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.