![](/img/trans.png)
[英]Chrome crashes when running selenium python3 script as sudo
[英]python3 script fails when run as sudo
當我使用 pi 用戶在樹莓派上運行下面的 python 腳本時,它運行良好。
#!/usr/bin/python3
from prometheus_client import start_http_server, Summary, Gauge
import random
import time
import subprocess
import json
# Create a metric to track time spent and requests made.
REQUEST_TIME = Summary('request_processing_seconds', 'Time spent processing request')
# use Gauge to record the metrics
labels = ['server_name', 'server_id']
latencyGauge = Gauge('ping_latency', 'Ping Latency in ms', labels)
downloadGauge = Gauge('download_speed', 'Download speed in bytes', labels)
uploadGauge = Gauge('upload_speed', 'Upload speed in bytes', labels)
# launch a subprocess running the speedtest command
# Decorate function with metric.
@REQUEST_TIME.time()
def process_request(id, t):
comp = subprocess.run(['speedtest', '-s', id, '-f', 'json'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
# print(comp)
output_str = comp.stdout
# print(output_str)
res = json.loads(output_str)
ping = res["ping"]["latency"]
upload = res["upload"]["bandwidth"]
download = res["download"]["bandwidth"]
server_name = res["server"]["name"]
print("Result for %s (%s): %d, %d, %d" % (id, server_name, ping, download, upload))
latencyGauge.labels(server_name, id).set(ping)
uploadGauge.labels(server_name, id).set(upload)
downloadGauge.labels(server_name, id).set(download)
time.sleep(t)
if __name__ == '__main__':
# Start up the server to expose the metrics.
start_http_server(8000)
# Generate some requests.
while True:
process_request("1686", 120)
但是,如果我使用 sudo 運行它,它會失敗並出現以下錯誤。
pi@raspberrypi:~/speedtest $ sudo ./speedtest.py
Traceback (most recent call last):
File "./speedtest.py", line 46, in <module>
process_request("1686", 120)
File "<decorator-gen-1>", line 2, in process_request
File "/usr/local/lib/python3.7/dist-packages/prometheus_client/context_managers.py", line 66, in wrapped
return func(*args, **kwargs)
File "./speedtest.py", line 27, in process_request
res = json.loads(output_str)
File "/usr/lib/python3.7/json/__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "/usr/lib/python3.7/json/decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python3.7/json/decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
在 sudo 中運行腳本的原因是我希望它通過編輯 /etc/rc.local 自動啟動
可能是什么原因?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.