I wrote a python script, which pings a server using ping3
.
Since this requires root permissions in order to function, I ran the script as sudo.
Now the thing is, when I run the program without sudo, it stays open, but if ran as sudo, it immediately closes, without any exceptions.
My script:
import atexit
import RPi.GPIO as gpio
import sys
import time
from threading import Thread
import ping3
from ping3 import ping
from phue import Bridge
from datetime import datetime
ping3.EXCEPTIONS = False
functionOne = 13
functionTwo = 15
functionThree = 16
relaisStörung = 18
functionOneState = False
functionTwoState = False
errorState = False
def handleExit():
gpio.cleanup()
print('EXITED! ', hueLogic.getCurrentDateTime())
sys.exit(0)
class NetworkWatchdog(Thread):
def __init__(self):
# Call the Thread class's init function
Thread.__init__(self)
def run(self):
global errorState
routerAddress = '192.168.178.1'
errorCounter = 0
while 1:
result = ping(routerAddress)
if not result or result is None:
errorCounter = 1
else:
errorCounter = 0
if errorCounter > 1:
print('Netzwerk Fehler! Stör Relay an! ', hueLogic.getCurrentDateTime())
errorState = True
gpio.output(relaisStörung, gpio.HIGH)
elif errorCounter < 1:
if errorState:
print("Netzwerk Fehler behoben! Stör Relay normal! ", hueLogic.getCurrentDateTime())
errorState = False
gpio.output(relaisStörung, gpio.LOW)
time.sleep(2)
# sys.stdout = open('/home/admin/Desktop/log.log', 'a ')
# sys.stderr = open('/home/admin/Desktop/log.log', 'a ')
atexit.register(handleExit)
hueLogic = HueLogic()
watchDog = NetworkWatchdog()
watchDog.daemon = True
watchDog.start()
print("--------------------------------")
print("Programm started! ", hueLogic.getCurrentDateTime())
gpio.setmode(gpio.BOARD)
gpio.setup(functionOne, gpio.IN, pull_up_down=gpio.PUD_DOWN)
gpio.setup(functionTwo, gpio.IN, pull_up_down=gpio.PUD_DOWN)
gpio.setup(functionThree, gpio.IN, pull_up_down=gpio.PUD_DOWN)
gpio.setup(relaisStörung, gpio.OUT)
# relais default on
gpio.output(relaisStörung, gpio.LOW)
gpio.add_event_detect(functionOne, gpio.BOTH, callback=hueLogic.functionOne, bouncetime=500)
gpio.add_event_detect(functionTwo, gpio.BOTH, callback=hueLogic.functionTwo, bouncetime=500)
gpio.add_event_detect(functionThree, gpio.BOTH, callback=hueLogic.functionThree, bouncetime=500)
Now my question is, why does this happen and if the solution is as simple as adding a while True loop at the end?
CodePudding user response:
make sure u excute
$sudo python main.py
instead of
$sudo main.py
also try to add
$chmod x myscript.py
CodePudding user response:
When you run $ sudo cmd
instead of $ cmd
, several
things are different.
Definitely the uid reported by $ id
will be different.
But your $PATH
is likely different, as well,
and that's not the only env var that may change.
Insert a call to breakpoint()
to let you
isolate which of those many python statements
is the one that causes trouble.
Use the l
list and n
next commands
to identify where you are in the execution so far.
Use commands like $ sudo which python
and $ sudo python -m site
to verify
that you're running the expected
version of interpreter and imported libraries.
Here is a generic way of assessing how your user & root env vars differ.
$ env | sort > /tmp/user.txt
$
$ sudo env | sort > /tmp/root.txt
$
$ diff -u /tmp/{user,root}.txt
It is possible that when running
as a root user you will need to
conda activate myproject
,
or similarly activate venv.
There might be a .bashrc file
you want to source
.
Pay careful attention to PYTHONPATH
-- it's
a pretty important env var!
It controls the value of sys.path
,
which matters for each of your import
statements.