[英]Why do I get a 'permission denied' error when I try to access a serial device when executing python script on Jenkins?
If I execute the following script as user 'build' in the command line it works: 如果我在命令行中以“ build”用户身份执行以下脚本,它将起作用:
#!/usr/bin/python
import serial
import pwd
import os
import grp
user = pwd.getpwuid(os.getuid()).pw_name
print user
groups = [g.gr_name for 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 which port was really used
ser.write("hello") # write a string
ser.close()
The output is 输出是
$ python test-serial.py
build
['dialout', 'build']
0660
/dev/ttyUSB0
If I execute this script in a Jenkins job I get 如果我在詹金斯(Jenkins)工作中执行此脚本,我会得到
[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: could not open port /dev/ttyUSB0: [Errno 13] Permission denied: '/dev/ttyUSB0'
Build step 'Execute shell' marked build as failure
Finished: FAILURE
If I change the file permissions with sudo chmod 666 /dev/ttyUSB0
the Jenkins job succeeds 如果我使用
sudo chmod 666 /dev/ttyUSB0
更改文件权限,则Jenkins作业成功
[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 could be the problem here? 这可能是什么问题?
The solutions was a plain 'Did you try to turn it off and on again?'. 解决方案是一个简单的“您是否尝试将其关闭然后再次打开?”。 The build succeeded after restarting the Jenkins slave.
重新启动Jenkins从站后,构建成功。
The reason for this is that I added the user 'build' to the group 'dialout' and after a logout/login it worked on the command line. 原因是我将用户“ build”添加到组“ dialout”中,并且在注销/登录后可以在命令行中使用。 But I forgot about reconnecting the Jenkins slave.
但是我忘记了重新连接詹金斯奴隶。
The output of the Python script showed the correct group membership (because it is read from a database) but this was not activated before reconnecting the Jenkins slave. Python脚本的输出显示正确的组成员身份(因为它是从数据库中读取的),但是在重新连接Jenkins从属之前未激活该成员身份。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.