1

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

  • 2
    How did you install escpos? sudo pip3 install escpos or pip3 install escpos? Or was this in a virtualenv you used for escpos? Because you need the module installed systemwide with sudo pip3 install for it to be able to use escpos. – Thomas Ward Nov 19 '18 at 14:16
  • I use sudo pip3 install python-escpos to install the library – Wan Chap Nov 19 '18 at 17:26
  • 2
    Welcome to Ask Ubuntu! Please don’t put the solution into the question. Use the answer field below instead. askubuntu.com/help/self-answer Also don’t add “SOLVED” to the title. In order to mark your question solved in the right way, you may accept the answer (by clicking on the tick mark (✓) next to it) that helped you. In case the helpful answer was posted by you, you'll have to wait 2 days before being able to accept it. – Melebius Nov 22 '18 at 9:12
  • Thanks for the tips, I am still learning to be a part of AskUbuntu community. – Wan Chap Nov 22 '18 at 10:01
0

I have solved the problem.

This bug is not caused by root permission, but rather the escpos module are not install correctly under root user. After I reinstall the escpos by sudo pip3 install python-escpos all codes work accordingly and is able to print the text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.