How to setup a systemd service with root access?











up vote
1
down vote

favorite
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 Worldn")
p.text("nnn")
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










share|improve this question




















  • 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 at 14:16










  • I use sudo pip3 install python-escpos to install the library
    – Wan Chap
    Nov 19 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 at 9:12










  • Thanks for the tips, I am still learning to be a part of AskUbuntu community.
    – Wan Chap
    Nov 22 at 10:01















up vote
1
down vote

favorite
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 Worldn")
p.text("nnn")
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










share|improve this question




















  • 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 at 14:16










  • I use sudo pip3 install python-escpos to install the library
    – Wan Chap
    Nov 19 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 at 9:12










  • Thanks for the tips, I am still learning to be a part of AskUbuntu community.
    – Wan Chap
    Nov 22 at 10:01













up vote
1
down vote

favorite
1









up vote
1
down vote

favorite
1






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 Worldn")
p.text("nnn")
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










share|improve this question















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 Worldn")
p.text("nnn")
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







python sudo systemd services






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 at 9:59

























asked Nov 19 at 14:10









Wan Chap

112




112








  • 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 at 14:16










  • I use sudo pip3 install python-escpos to install the library
    – Wan Chap
    Nov 19 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 at 9:12










  • Thanks for the tips, I am still learning to be a part of AskUbuntu community.
    – Wan Chap
    Nov 22 at 10:01














  • 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 at 14:16










  • I use sudo pip3 install python-escpos to install the library
    – Wan Chap
    Nov 19 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 at 9:12










  • Thanks for the tips, I am still learning to be a part of AskUbuntu community.
    – Wan Chap
    Nov 22 at 10:01








2




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 at 14:16




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 at 14:16












I use sudo pip3 install python-escpos to install the library
– Wan Chap
Nov 19 at 17:26




I use sudo pip3 install python-escpos to install the library
– Wan Chap
Nov 19 at 17:26




2




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 at 9:12




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 at 9:12












Thanks for the tips, I am still learning to be a part of AskUbuntu community.
– Wan Chap
Nov 22 at 10:01




Thanks for the tips, I am still learning to be a part of AskUbuntu community.
– Wan Chap
Nov 22 at 10:01










1 Answer
1






active

oldest

votes

















up vote
0
down vote













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.






share|improve this answer





















    Your Answer








    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "89"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1094225%2fhow-to-setup-a-systemd-service-with-root-access%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    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.






    share|improve this answer

























      up vote
      0
      down vote













      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.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        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.






        share|improve this answer












        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.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 9:58









        Wan Chap

        112




        112






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Ask Ubuntu!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1094225%2fhow-to-setup-a-systemd-service-with-root-access%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

            ComboBox Display Member on multiple fields

            Is it possible to collect Nectar points via Trainline?