我试图从我的python应用程序调用bash命令,以更改触摸屏上的背景光。 python应用程序将在我的Raspberry Pi(Rasbian / stretch)上运行。
在终端中运行bash命令并不复杂:sudo sh -c "echo 80 > /sys/class/backlight/rpi_backlight/brightness"
肯定会使屏幕变暗(这是我想要的)。但是如何在我的python应用程序中sudo
脚本? (我知道有很多线程正在讨论这个问题,例如这个Using sudo with Python script,但我不知道如何在实践中做到这一点?)
这是我的代码:
#!/usr/bin/env python3
import subprocess
import time
import sys
# read arguments from the run command:
# idle time before dim (in seconds)
idleTimeBeforeDimMS = int( sys.argv[1] )*1000
# brightness when dimmed (between 0 and 255)
brightnessDimmed = int( sys.argv[2] )
brightnessFull = 255
def get(cmd):
# just a helper function
return subprocess.check_output(cmd).decode("utf-8").strip()
isIdle0 = False
stateChanged = False
timeIntervalToWatchChangesS = 100 / 1000
while True:
time.sleep( timeIntervalToWatchChangesS )
currentIdleTimeMS = int( get("xprintidle") )
isIdle = currentIdleTimeMS > idleTimeBeforeDimMS
stateChanged = isIdle0 != isIdle
if isIdle and stateChanged:
# idling
bashCommand = "echo 50 > /sys/class/backlight/rpi_backlight/brightness"
subprocess.run(['bash', '-c', bashCommand])
elif not isIdle and stateChanged:
# active
bashCommand = "echo 255 > /sys/class/backlight/rpi_backlight/brightness"
subprocess.run(['bash', '-c', bashCommand])
# set current state as initial one for the next loop cycle
isIdle0 = isIdle
如果立即运行脚本,则bash命令bash: /sys/class/backlight/rpi_backlight/brightness: Permission denied
会出现错误。没关系,我知道我缺少sudo
部分,但是应该放在哪里?
答案 0 :(得分:1)
将它放在外壳的前面,就像您进行交互式操作一样:
subprocess.run(['sudo', 'bash', '-c', bashCommand])
答案 1 :(得分:1)
我建议只使用sudo运行python脚本...即:sudo myscript.py
。这样,它可能运行的所有命令都将具有特权。
您可以使用that_other_guy的答案,但是您的脚本仍会提示您输入密码(至少在我看来是这样)。因此,答案不是那么好。
如果您真的想自动化它,但又不想以root用户身份运行它,则需要使用that_other_guys的答案,而且还应显示here所示的密码。
虽然有点hacky。我只会以root权限运行python脚本本身。
但是,如果您真的不想以root身份运行它,则可以执行以下操作:
>>> from subprocess import run, PIPE
>>> cmd = "echo mypassword | sudo -S ls"
>>> out = run(cmd, shell=True, stdout=PIPE)
>>> output = [i for i in out.stdout.decode().split('\n') if i]
>>> output
['build', 'dist', '__init__.py', 'palindrome', 'palindrome.egg-info', 'LICENSE', 'README.md', 'setup.py']