简体   繁体   English

在Jenkins上执行python脚本时,尝试访问串行设备时,为什么会出现“权限被拒绝”错误?

[英]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.

相关问题 当我尝试在python脚本中访问此JSON文件的第一项时,为什么会收到KeyError? - Why do I get a KeyError when I try to access the first item of this JSON file in a python script 尝试获取文件夹权限列表时,权限被拒绝 - permission denied when i try to get folder permission list 为什么在尝试启动Python Scapy回送接口时出现“权限被拒绝”的问题? - Why do I get “permission denied” when trying to start Python Scapy loopback interface? 尝试从一个s3对象重定向到另一个s3对象时,为什么拒绝访问? - Why do I get an access denied when I try to do a redirect from one s3 object to another? 当我尝试结束 Python 子进程时,为什么 terminate() 会收到“权限被拒绝”? - Why does terminate() recieve 'permission denied' when I try and end my Python subprocess? 尝试从另一个python脚本中运行python脚本时,出现“权限被拒绝”! - When attempting run a python script from within another python script, I get 'permission denied'! 尝试通过解释器运行Python脚本时,为什么会出现“ ImportError:未命名模块”的提示? - Why do I get “ImportError: No module named” when I try to run my Python script via the Interpreter? 为什么我在尝试使用 python 的 ldap 简单请求时会出现错误 - Why do I get an error when I try a ldap simple request with python Python:当我尝试在日期之间插入 xarray 时,为什么会出现错误? - Python: why do I get an error when I try to interpolate an xarray between dates? 为什么我的 Python 脚本从终端运行时会导致“权限被拒绝”? - Why does my Python script result in “permission denied” when I run it from the terminal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM