Why do I get a 'permission denied' error when I try to access a serial device when executing python script on Jenkins? -
if execute following script user 'build' in command line works:
#!/usr/bin/python import serial import pwd import os import grp user = pwd.getpwuid(os.getuid()).pw_name print user groups = [g.gr_name g in grp.getgrall() if user in g.gr_mem] gid = pwd.getpwnam(user).pw_gid groups.append(grp.getgrgid(gid).gr_name) print groups print oct(os.stat("/dev/ttyusb0").st_mode & 0777) ser = serial.serial('/dev/ttyusb0', 115200, timeout=1) print ser.portstr # check port used ser.write("hello") # write string ser.close()
the output is
$ python test-serial.py build ['dialout', 'build'] 0660 /dev/ttyusb0
if execute script in jenkins job get
[envinject] - loading node environment variables. building remotely on build-node in workspace /home/build/workspace/test-serial-python [test-serial-python] $ /bin/sh -xe /tmp/hudson7262474284926512955.sh + /usr/bin/python /home/build/test-serial.py build ['dialout', 'build'] 0660 traceback (most recent call last): file "/home/build/test-serial.py", line 17, in <module> ser = serial.serial('/dev/ttyusb0', 115200, timeout=1) file "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 261, in __init__ self.open() file "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 278, in open raise serialexception("could not open port %s: %s" % (self._port, msg)) serial.serialutil.serialexception: not open port /dev/ttyusb0: [errno 13] permission denied: '/dev/ttyusb0' build step 'execute shell' marked build failure finished: failure
if change file permissions sudo chmod 666 /dev/ttyusb0
jenkins job succeeds
[envinject] - loading node environment variables. building remotely on build-node in workspace /home/build/workspace/test-serial-python [test-serial-python] $ /bin/sh -xe /tmp/hudson9120277699281127818.sh + /usr/bin/python /home/build/test-serial.py build ['dialout', 'build'] 0666 /dev/ttyusb0 finished: success
what problem here?
the solutions plain 'did try turn off , on again?'. build succeeded after restarting jenkins slave.
the reason added user 'build' group 'dialout' , after logout/login worked on command line. forgot reconnecting jenkins slave.
the output of python script showed correct group membership (because read database) not activated before reconnecting jenkins slave.
Comments
Post a Comment