Linux Service Cannot Open Display











up vote
1
down vote

favorite












On my Odroid running Ubuntu 16.04.3 LTS I have a python2 program that interacts with a display device (projector). When I run this program from the command line:



python ~/imgProc/torcam.py


everything works fine. I want this program to run at startup so I created a service file:



/lib/systemd/system/torcam.service


It contains the following:



[Unit]
Description=Torcam Service
After=rc-local.service network-online.target
[Service]
User=root
ExecStart=/home/odroid/imgProc/starttor.sh
[Install]
WantedBy=multi-user.target


The startup script, starttor.sh contains:



#!/bin/bash
export DISPLAY=:0
cd /home/odroid/imgProc
python ./torcam.py


If I run this script from the command line everything works fine, but when I run this at boot or using systemctl I get an error saying "cannont open display: :0"



Here's how I installed the service:



odroid@odroid:~/imgProc$ sudo systemctl enable torcam
odroid@odroid:~/imgProc$ sudo systemctl start torcam
odroid@odroid:~/imgProc$ sudo systemctl status torcam


Here's the output:



odroid@odroid:~/imgProc$ sudo systemctl status torcam
● torcam.service - Torcam Service
Loaded: loaded (/lib/systemd/system/torcam.service; enabled; vendor preset: enabled)
Active: failed (Result: exit-code) since Fri 2016-02-12 08:39:21 EST; 6min ago
Process: 1807 ExecStart=/home/odroid/imgProc/starttor.sh (code=exited, status=1/FAILURE)
Main PID: 1807 (code=exited, status=1/FAILURE)

Feb 12 08:39:19 odroid systemd[1]: Started Torcam Service.
Feb 12 08:39:19 odroid systemd[1807]: torcam.service: Executing: /home/odroid/imgProc/starttor.sh
Feb 12 08:39:21 odroid starttor.sh[1807]: No protocol specified
Feb 12 08:39:21 odroid starttor.sh[1807]: (test:1808): Gtk-WARNING **: cannot open display: :0
Feb 12 08:39:21 odroid systemd[1]: torcam.service: Main process exited, code=exited, status=1/FAILURE
Feb 12 08:39:21 odroid systemd[1]: torcam.service: Unit entered failed state.
Feb 12 08:39:21 odroid systemd[1]: torcam.service: Failed with result 'exit-code'.


I understand that the problem is related to starting a service that interacts with the display, but I'm stuck on how to enable this so it works on boot. Any suggestions?










share|improve this question







New contributor




cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
























    up vote
    1
    down vote

    favorite












    On my Odroid running Ubuntu 16.04.3 LTS I have a python2 program that interacts with a display device (projector). When I run this program from the command line:



    python ~/imgProc/torcam.py


    everything works fine. I want this program to run at startup so I created a service file:



    /lib/systemd/system/torcam.service


    It contains the following:



    [Unit]
    Description=Torcam Service
    After=rc-local.service network-online.target
    [Service]
    User=root
    ExecStart=/home/odroid/imgProc/starttor.sh
    [Install]
    WantedBy=multi-user.target


    The startup script, starttor.sh contains:



    #!/bin/bash
    export DISPLAY=:0
    cd /home/odroid/imgProc
    python ./torcam.py


    If I run this script from the command line everything works fine, but when I run this at boot or using systemctl I get an error saying "cannont open display: :0"



    Here's how I installed the service:



    odroid@odroid:~/imgProc$ sudo systemctl enable torcam
    odroid@odroid:~/imgProc$ sudo systemctl start torcam
    odroid@odroid:~/imgProc$ sudo systemctl status torcam


    Here's the output:



    odroid@odroid:~/imgProc$ sudo systemctl status torcam
    ● torcam.service - Torcam Service
    Loaded: loaded (/lib/systemd/system/torcam.service; enabled; vendor preset: enabled)
    Active: failed (Result: exit-code) since Fri 2016-02-12 08:39:21 EST; 6min ago
    Process: 1807 ExecStart=/home/odroid/imgProc/starttor.sh (code=exited, status=1/FAILURE)
    Main PID: 1807 (code=exited, status=1/FAILURE)

    Feb 12 08:39:19 odroid systemd[1]: Started Torcam Service.
    Feb 12 08:39:19 odroid systemd[1807]: torcam.service: Executing: /home/odroid/imgProc/starttor.sh
    Feb 12 08:39:21 odroid starttor.sh[1807]: No protocol specified
    Feb 12 08:39:21 odroid starttor.sh[1807]: (test:1808): Gtk-WARNING **: cannot open display: :0
    Feb 12 08:39:21 odroid systemd[1]: torcam.service: Main process exited, code=exited, status=1/FAILURE
    Feb 12 08:39:21 odroid systemd[1]: torcam.service: Unit entered failed state.
    Feb 12 08:39:21 odroid systemd[1]: torcam.service: Failed with result 'exit-code'.


    I understand that the problem is related to starting a service that interacts with the display, but I'm stuck on how to enable this so it works on boot. Any suggestions?










    share|improve this question







    New contributor




    cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      On my Odroid running Ubuntu 16.04.3 LTS I have a python2 program that interacts with a display device (projector). When I run this program from the command line:



      python ~/imgProc/torcam.py


      everything works fine. I want this program to run at startup so I created a service file:



      /lib/systemd/system/torcam.service


      It contains the following:



      [Unit]
      Description=Torcam Service
      After=rc-local.service network-online.target
      [Service]
      User=root
      ExecStart=/home/odroid/imgProc/starttor.sh
      [Install]
      WantedBy=multi-user.target


      The startup script, starttor.sh contains:



      #!/bin/bash
      export DISPLAY=:0
      cd /home/odroid/imgProc
      python ./torcam.py


      If I run this script from the command line everything works fine, but when I run this at boot or using systemctl I get an error saying "cannont open display: :0"



      Here's how I installed the service:



      odroid@odroid:~/imgProc$ sudo systemctl enable torcam
      odroid@odroid:~/imgProc$ sudo systemctl start torcam
      odroid@odroid:~/imgProc$ sudo systemctl status torcam


      Here's the output:



      odroid@odroid:~/imgProc$ sudo systemctl status torcam
      ● torcam.service - Torcam Service
      Loaded: loaded (/lib/systemd/system/torcam.service; enabled; vendor preset: enabled)
      Active: failed (Result: exit-code) since Fri 2016-02-12 08:39:21 EST; 6min ago
      Process: 1807 ExecStart=/home/odroid/imgProc/starttor.sh (code=exited, status=1/FAILURE)
      Main PID: 1807 (code=exited, status=1/FAILURE)

      Feb 12 08:39:19 odroid systemd[1]: Started Torcam Service.
      Feb 12 08:39:19 odroid systemd[1807]: torcam.service: Executing: /home/odroid/imgProc/starttor.sh
      Feb 12 08:39:21 odroid starttor.sh[1807]: No protocol specified
      Feb 12 08:39:21 odroid starttor.sh[1807]: (test:1808): Gtk-WARNING **: cannot open display: :0
      Feb 12 08:39:21 odroid systemd[1]: torcam.service: Main process exited, code=exited, status=1/FAILURE
      Feb 12 08:39:21 odroid systemd[1]: torcam.service: Unit entered failed state.
      Feb 12 08:39:21 odroid systemd[1]: torcam.service: Failed with result 'exit-code'.


      I understand that the problem is related to starting a service that interacts with the display, but I'm stuck on how to enable this so it works on boot. Any suggestions?










      share|improve this question







      New contributor




      cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      On my Odroid running Ubuntu 16.04.3 LTS I have a python2 program that interacts with a display device (projector). When I run this program from the command line:



      python ~/imgProc/torcam.py


      everything works fine. I want this program to run at startup so I created a service file:



      /lib/systemd/system/torcam.service


      It contains the following:



      [Unit]
      Description=Torcam Service
      After=rc-local.service network-online.target
      [Service]
      User=root
      ExecStart=/home/odroid/imgProc/starttor.sh
      [Install]
      WantedBy=multi-user.target


      The startup script, starttor.sh contains:



      #!/bin/bash
      export DISPLAY=:0
      cd /home/odroid/imgProc
      python ./torcam.py


      If I run this script from the command line everything works fine, but when I run this at boot or using systemctl I get an error saying "cannont open display: :0"



      Here's how I installed the service:



      odroid@odroid:~/imgProc$ sudo systemctl enable torcam
      odroid@odroid:~/imgProc$ sudo systemctl start torcam
      odroid@odroid:~/imgProc$ sudo systemctl status torcam


      Here's the output:



      odroid@odroid:~/imgProc$ sudo systemctl status torcam
      ● torcam.service - Torcam Service
      Loaded: loaded (/lib/systemd/system/torcam.service; enabled; vendor preset: enabled)
      Active: failed (Result: exit-code) since Fri 2016-02-12 08:39:21 EST; 6min ago
      Process: 1807 ExecStart=/home/odroid/imgProc/starttor.sh (code=exited, status=1/FAILURE)
      Main PID: 1807 (code=exited, status=1/FAILURE)

      Feb 12 08:39:19 odroid systemd[1]: Started Torcam Service.
      Feb 12 08:39:19 odroid systemd[1807]: torcam.service: Executing: /home/odroid/imgProc/starttor.sh
      Feb 12 08:39:21 odroid starttor.sh[1807]: No protocol specified
      Feb 12 08:39:21 odroid starttor.sh[1807]: (test:1808): Gtk-WARNING **: cannot open display: :0
      Feb 12 08:39:21 odroid systemd[1]: torcam.service: Main process exited, code=exited, status=1/FAILURE
      Feb 12 08:39:21 odroid systemd[1]: torcam.service: Unit entered failed state.
      Feb 12 08:39:21 odroid systemd[1]: torcam.service: Failed with result 'exit-code'.


      I understand that the problem is related to starting a service that interacts with the display, but I'm stuck on how to enable this so it works on boot. Any suggestions?







      linux x11 display systemctl






      share|improve this question







      New contributor




      cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question







      New contributor




      cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question






      New contributor




      cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Dec 4 at 17:13









      cce1911

      82




      82




      New contributor




      cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          Your torcam needs an X server, and systemd scripts are not meant to use the X server (it may not even have started when the scripts are run).



          If you need this program to access the display device/projector independent of the user logging in via keyboard/monitor, consider using two X servers (one for the monitor, one for the projector), and starting torcam with correct authentication (man xauth etc.) when the X server for the projector has started.



          You'll need to read up on xorg.conf, and how to restrict the X server to just some outputs (assuming it's a single graphics card with multiple outputs). You'll also need to read up on how an X server is started (display manager like xdm etc., what you need for your monitor, vs. directly).






          share|improve this answer




























            up vote
            0
            down vote













            As kirkt mentioned, my problem was related to trying to use the X server running as root. After modifying my startup script, I was able to get the service to start correctly.



            starrtor.sh:



            #!/bin/bash
            sleep 10
            export DISPLAY=:0
            cd /home/odroid/imgProc
            exec sudo -u odroid /bin/sh - << eof
            python ./torcam.py





            share|improve this answer








            New contributor




            cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.


















              Your Answer








              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "106"
              };
              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: false,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: null,
              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
              });


              }
              });






              cce1911 is a new contributor. Be nice, and check out our Code of Conduct.










              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f485955%2flinux-service-cannot-open-display%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              1
              down vote



              accepted










              Your torcam needs an X server, and systemd scripts are not meant to use the X server (it may not even have started when the scripts are run).



              If you need this program to access the display device/projector independent of the user logging in via keyboard/monitor, consider using two X servers (one for the monitor, one for the projector), and starting torcam with correct authentication (man xauth etc.) when the X server for the projector has started.



              You'll need to read up on xorg.conf, and how to restrict the X server to just some outputs (assuming it's a single graphics card with multiple outputs). You'll also need to read up on how an X server is started (display manager like xdm etc., what you need for your monitor, vs. directly).






              share|improve this answer

























                up vote
                1
                down vote



                accepted










                Your torcam needs an X server, and systemd scripts are not meant to use the X server (it may not even have started when the scripts are run).



                If you need this program to access the display device/projector independent of the user logging in via keyboard/monitor, consider using two X servers (one for the monitor, one for the projector), and starting torcam with correct authentication (man xauth etc.) when the X server for the projector has started.



                You'll need to read up on xorg.conf, and how to restrict the X server to just some outputs (assuming it's a single graphics card with multiple outputs). You'll also need to read up on how an X server is started (display manager like xdm etc., what you need for your monitor, vs. directly).






                share|improve this answer























                  up vote
                  1
                  down vote



                  accepted







                  up vote
                  1
                  down vote



                  accepted






                  Your torcam needs an X server, and systemd scripts are not meant to use the X server (it may not even have started when the scripts are run).



                  If you need this program to access the display device/projector independent of the user logging in via keyboard/monitor, consider using two X servers (one for the monitor, one for the projector), and starting torcam with correct authentication (man xauth etc.) when the X server for the projector has started.



                  You'll need to read up on xorg.conf, and how to restrict the X server to just some outputs (assuming it's a single graphics card with multiple outputs). You'll also need to read up on how an X server is started (display manager like xdm etc., what you need for your monitor, vs. directly).






                  share|improve this answer












                  Your torcam needs an X server, and systemd scripts are not meant to use the X server (it may not even have started when the scripts are run).



                  If you need this program to access the display device/projector independent of the user logging in via keyboard/monitor, consider using two X servers (one for the monitor, one for the projector), and starting torcam with correct authentication (man xauth etc.) when the X server for the projector has started.



                  You'll need to read up on xorg.conf, and how to restrict the X server to just some outputs (assuming it's a single graphics card with multiple outputs). You'll also need to read up on how an X server is started (display manager like xdm etc., what you need for your monitor, vs. directly).







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 days ago









                  dirkt

                  16.4k21335




                  16.4k21335
























                      up vote
                      0
                      down vote













                      As kirkt mentioned, my problem was related to trying to use the X server running as root. After modifying my startup script, I was able to get the service to start correctly.



                      starrtor.sh:



                      #!/bin/bash
                      sleep 10
                      export DISPLAY=:0
                      cd /home/odroid/imgProc
                      exec sudo -u odroid /bin/sh - << eof
                      python ./torcam.py





                      share|improve this answer








                      New contributor




                      cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                      Check out our Code of Conduct.






















                        up vote
                        0
                        down vote













                        As kirkt mentioned, my problem was related to trying to use the X server running as root. After modifying my startup script, I was able to get the service to start correctly.



                        starrtor.sh:



                        #!/bin/bash
                        sleep 10
                        export DISPLAY=:0
                        cd /home/odroid/imgProc
                        exec sudo -u odroid /bin/sh - << eof
                        python ./torcam.py





                        share|improve this answer








                        New contributor




                        cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.




















                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          As kirkt mentioned, my problem was related to trying to use the X server running as root. After modifying my startup script, I was able to get the service to start correctly.



                          starrtor.sh:



                          #!/bin/bash
                          sleep 10
                          export DISPLAY=:0
                          cd /home/odroid/imgProc
                          exec sudo -u odroid /bin/sh - << eof
                          python ./torcam.py





                          share|improve this answer








                          New contributor




                          cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          As kirkt mentioned, my problem was related to trying to use the X server running as root. After modifying my startup script, I was able to get the service to start correctly.



                          starrtor.sh:



                          #!/bin/bash
                          sleep 10
                          export DISPLAY=:0
                          cd /home/odroid/imgProc
                          exec sudo -u odroid /bin/sh - << eof
                          python ./torcam.py






                          share|improve this answer








                          New contributor




                          cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          share|improve this answer



                          share|improve this answer






                          New contributor




                          cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.









                          answered 2 days ago









                          cce1911

                          82




                          82




                          New contributor




                          cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.





                          New contributor





                          cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          cce1911 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






















                              cce1911 is a new contributor. Be nice, and check out our Code of Conduct.










                              draft saved

                              draft discarded


















                              cce1911 is a new contributor. Be nice, and check out our Code of Conduct.













                              cce1911 is a new contributor. Be nice, and check out our Code of Conduct.












                              cce1911 is a new contributor. Be nice, and check out our Code of Conduct.
















                              Thanks for contributing an answer to Unix & Linux Stack Exchange!


                              • 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%2funix.stackexchange.com%2fquestions%2f485955%2flinux-service-cannot-open-display%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

                              Accessing regular linux commands in Huawei's Dopra Linux

                              Can't connect RFCOMM socket: Host is down

                              Kernel panic - not syncing: Fatal Exception in Interrupt