我正在尝试制作一个systemd服务文件来运行python脚本来使用热打印机打印一些东西。escpos是我用来访问热打印机控制的库。然而,escpos需要sudo来访问usb设备。
如何使systemd服务文件使exec命令获得根访问权?基于互联网上的一些答案,如果我没有为用户设置任何内容,默认的exec应该会自动获得根访问。但我还是有麻烦让我的程序运行。希望有人能帮我找出我的错误。
文件: /usr/local/etc/test/testPrint.py
from escpos.printer import Usb
#require escpos use "pip install python-escpos" to install the library
""" Seiko Epson Corp. Receipt Printer (EPSON TM-T88III) """
p = Usb(0x4b43, 0x3538, 0, 0x82, 0x02)
p.text("Hello World\n")
p.text("\n\n\n")
p.cut()我可以运行sudo python3 /usr/local/etc/test/testPrint.py和打印文本从热打印机没有任何问题。
文件: /etc/systemd/system/testPrint.service
[Unit]
Description=Test Python escpos print
After=network.target
[Service]
Type=oneshot
ExecStart=/usr/bin/python3 /usr/local/etc/test/testPrint.py
[Install]
WantedBy=multi-user.target当我尝试运行命令sudo systemctl start testPrint.service时。它会产生错误
命令:sudo journalctl -u testPrint.service:
python3.5[27149]: Traceback (most recent call last):
python3.5[27149]: File "/usr/local/etc/test/testPrint.py",
python3.5[27149]: from escpos.printer import Usb
python3.5[27149]: ImportError: No module named 'escpos'
systemd[1]: testPrint.service: Main process exited, code=exit
systemd[1]: Failed to start Test python escpos print.
systemd[1]: testPrint.service: Unit entered failed state.
systemd[1]: testPrint.service: Failed with result 'exit-code'这是相同的错误,如果我运行python而没有sudo "python3 printTest.py“
只是为了提供一些额外的信息。我使用sudo pip3安装来安装模块。我的操作系统是Ubuntu 16.04
发布于 2018-11-22 09:58:28
我已经解决了这个问题。
此错误不是由根权限引起的,而是根用户下的escpos模块没有正确安装。在我通过sudo pip3 install python-escpos重新安装托管文件后,所有代码都会相应地工作,并能够打印文本。
https://askubuntu.com/questions/1094225
复制相似问题