sudo -E sh -c 'echo 1 > /sys/class/backlight/rpi_backlight/bl_power'
sudo -E sh -c 'echo 0 > /sys/class/backlight/rpi_backlight/bl_power'
TIP: I define the above as
~/.bash_aliases. Be sure to save that file elsewhere, since you'll lose it if you re-image.
- First save the above as
backlight_on.sh (with proper
#!/bin/sh shebang header), maybe saved to
/usr/local/bin/ if you want it available in your $PATH
- ... then you can use Python
subprocess() to invoke these new scripts. Use the "command syntax" to run the script files, and the script's own shell should take care of things.
- If you are doing this in a web application (and therefore rightly concerned about security), then avoid use of variables around these calls (hardcode the Python subprocess calls), and get the code peer-reviewed.
If you read Subprocess documentation, it may contain suggestions to not use it to run shell scripts. That's fine in most cases, but not in this one.
FYI - "-c" is a life-saver
The "-c" option is often used to "wrap" commands into a user context, like sudo. You'll see this in some
cron files, or used heavily by the
More reading (and doing this in "pure" Python):
There are downsides to this, and you may need to run your Python script as sudo (not a good idea, generally) or make configuration changes to your Pi. You'll learn a bit going down this rabbit hole..
FYI this also worked:
pi@pi3:~ $ echo 1 | sudo tee /sys/class/backlight/rpi_backlight/bl_power
That works because the redirect is executed as sudo, but there is the unintended side-effect of
1 output text.