I have the following Python script that I use to toggle my official touchscreen (1st one) display on and off using the Pimoroni fan-shim button. It also mute's my USB speaker when the screen is off:
This works a treat when I run the script from the command line. However, when I run it as a service or from crontab, the display switches on and off but the audio does not mute. I have tried various methods of muting the speaker with no success:Can anyone help with this please? I presume that it is something to do with user permissions as I know that the commands don't work with sudo.
Code:
#!/usr/bin/env python3import signal, osfrom fanshim import FanShimfanshim = FanShim()def getScreenState(): return os.popen("cat /sys/class/backlight/10-0045/bl_power").read().strip() == "0"def toggle_screen(): if getScreenState(): # Turn off os.system("""sudo sh -c 'echo "1" > /sys/class/backlight/10-0045/bl_power'""") os.system("sudo rmmod edt-ft5x06") os.system("pactl set-sink-mute 0 toggle") else: # Turn on os.system("""sudo sh -c 'echo "0" > /sys/class/backlight/10-0045/bl_power'""") os.system("sudo modprobe edt-ft5x06") os.system("pactl set-sink-mute 0 toggle")@fanshim.on_release()def release_handler(was_held): toggle_screen()try: signal.pause()except KeyboardInterrupt: passCode:
os.system("/usr/bin/amixer -D pulse sset Master 0%")Popen(['amixer', 'set', 'Master', 'mute']) # requires "from subprocess import Popen"Statistics: Posted by trlaing — Wed Nov 19, 2025 4:52 pm