Description
I am trying to make a systemd service file to run a python script to print something using thermal printer. escpos is the library I am using to access the control of thermal printer. However escpos require sudo to access the usb device.
Question
How to make a systemd service file such that the exec command get the root access? Base on some answer on internet, if I don't set anything to User, the default exec should get root access automatically. But I still getting trouble to make my program run. Hope anyone can help me to spot my mistake.
Code
python
File: /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()
I can run sudo python3 /usr/local/etc/test/testPrint.py
and print the text from the thermal printer without any problem.
Service File
File: /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
And when I try to run the command sudo systemctl start testPrint.service
. It gives error
Error log
Command: 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'
Which is the same error, if I run python without sudo "python3 printTest.py"
Edit:
Just to give out some extra info. I use sudo pip3 install python-escpos to install the module. My OS is Ubuntu 16.04
escpos
?sudo pip3 install escpos
orpip3 install escpos
? Or was this in a virtualenv you used for escpos? Because you need the module installed systemwide withsudo pip3 install
for it to be able to useescpos
.