Setting DISPLAY in systemd service file

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite
3












I'm trying to learn systemd services by trying to start xclock as a service; the service file is below



[Unit]
Description=clock

[Service]
Environment=DISPLAY=:0
ExecStart=/usr/bin/xclock

[Install]
WantedBy=graphical.target


Any ideas what's wrong here? I'm getting an error saying "cannot connect to display."










share|improve this question



























    up vote
    7
    down vote

    favorite
    3












    I'm trying to learn systemd services by trying to start xclock as a service; the service file is below



    [Unit]
    Description=clock

    [Service]
    Environment=DISPLAY=:0
    ExecStart=/usr/bin/xclock

    [Install]
    WantedBy=graphical.target


    Any ideas what's wrong here? I'm getting an error saying "cannot connect to display."










    share|improve this question

























      up vote
      7
      down vote

      favorite
      3









      up vote
      7
      down vote

      favorite
      3






      3





      I'm trying to learn systemd services by trying to start xclock as a service; the service file is below



      [Unit]
      Description=clock

      [Service]
      Environment=DISPLAY=:0
      ExecStart=/usr/bin/xclock

      [Install]
      WantedBy=graphical.target


      Any ideas what's wrong here? I'm getting an error saying "cannot connect to display."










      share|improve this question















      I'm trying to learn systemd services by trying to start xclock as a service; the service file is below



      [Unit]
      Description=clock

      [Service]
      Environment=DISPLAY=:0
      ExecStart=/usr/bin/xclock

      [Install]
      WantedBy=graphical.target


      Any ideas what's wrong here? I'm getting an error saying "cannot connect to display."







      opensuse systemd






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Oct 16 '16 at 13:10









      Jeff Schaller

      36k952119




      36k952119










      asked Aug 1 '13 at 21:57









      dogs

      3813




      3813




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          14
          down vote



          accepted










          An application needs two things to open a window on an X display. It needs to know the location of the X display; that's conveyed by the DISPLAY environment variable. It also needs to authenticate with the X server. This is conveyed through a cookie, which is a secret value generated by the X server when it starts and stored in a file that only the user who started the X server can access. The default cookie file is ~/.Xauthority.



          If your X server is using the default cookie file location, then adding Environment=XAUTHORITY=/home/dogs/.Xauthority will work (assuming /home/dogs is the home directory of the user who is logged in under X). If you need to find the location, see Can I launch a graphical program on another user's desktop as root? and Open a window on a remote X display (why “Cannot open display”)?



          Alternatively, running the program as the user who is running the X server will work, provided that the cookie file is in the default location (if not, you'll have to locate the cookie file, like in the root case). Add the User directive (e.g. User=dogs).



          Of course the service won't run if there isn't an X display by that number owned by the user you specify.



          It's rather bizarre to start a GUI program from Systemd. It wasn't designed for this. GUI programs live in an X session, started by a user. Systemd is for system processes. You should experiment with daemons instead.






          share|improve this answer





























            up vote
            -2
            down vote













            You can add in .xinitrc :



            xhost si:localuser:$USER


            It works for me!






            share|improve this answer


















            • 1




              this is for systemd service files, not .xinitrc
              – slang
              Sep 28 '15 at 20:59










            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
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f85244%2fsetting-display-in-systemd-service-file%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
            14
            down vote



            accepted










            An application needs two things to open a window on an X display. It needs to know the location of the X display; that's conveyed by the DISPLAY environment variable. It also needs to authenticate with the X server. This is conveyed through a cookie, which is a secret value generated by the X server when it starts and stored in a file that only the user who started the X server can access. The default cookie file is ~/.Xauthority.



            If your X server is using the default cookie file location, then adding Environment=XAUTHORITY=/home/dogs/.Xauthority will work (assuming /home/dogs is the home directory of the user who is logged in under X). If you need to find the location, see Can I launch a graphical program on another user's desktop as root? and Open a window on a remote X display (why “Cannot open display”)?



            Alternatively, running the program as the user who is running the X server will work, provided that the cookie file is in the default location (if not, you'll have to locate the cookie file, like in the root case). Add the User directive (e.g. User=dogs).



            Of course the service won't run if there isn't an X display by that number owned by the user you specify.



            It's rather bizarre to start a GUI program from Systemd. It wasn't designed for this. GUI programs live in an X session, started by a user. Systemd is for system processes. You should experiment with daemons instead.






            share|improve this answer


























              up vote
              14
              down vote



              accepted










              An application needs two things to open a window on an X display. It needs to know the location of the X display; that's conveyed by the DISPLAY environment variable. It also needs to authenticate with the X server. This is conveyed through a cookie, which is a secret value generated by the X server when it starts and stored in a file that only the user who started the X server can access. The default cookie file is ~/.Xauthority.



              If your X server is using the default cookie file location, then adding Environment=XAUTHORITY=/home/dogs/.Xauthority will work (assuming /home/dogs is the home directory of the user who is logged in under X). If you need to find the location, see Can I launch a graphical program on another user's desktop as root? and Open a window on a remote X display (why “Cannot open display”)?



              Alternatively, running the program as the user who is running the X server will work, provided that the cookie file is in the default location (if not, you'll have to locate the cookie file, like in the root case). Add the User directive (e.g. User=dogs).



              Of course the service won't run if there isn't an X display by that number owned by the user you specify.



              It's rather bizarre to start a GUI program from Systemd. It wasn't designed for this. GUI programs live in an X session, started by a user. Systemd is for system processes. You should experiment with daemons instead.






              share|improve this answer
























                up vote
                14
                down vote



                accepted







                up vote
                14
                down vote



                accepted






                An application needs two things to open a window on an X display. It needs to know the location of the X display; that's conveyed by the DISPLAY environment variable. It also needs to authenticate with the X server. This is conveyed through a cookie, which is a secret value generated by the X server when it starts and stored in a file that only the user who started the X server can access. The default cookie file is ~/.Xauthority.



                If your X server is using the default cookie file location, then adding Environment=XAUTHORITY=/home/dogs/.Xauthority will work (assuming /home/dogs is the home directory of the user who is logged in under X). If you need to find the location, see Can I launch a graphical program on another user's desktop as root? and Open a window on a remote X display (why “Cannot open display”)?



                Alternatively, running the program as the user who is running the X server will work, provided that the cookie file is in the default location (if not, you'll have to locate the cookie file, like in the root case). Add the User directive (e.g. User=dogs).



                Of course the service won't run if there isn't an X display by that number owned by the user you specify.



                It's rather bizarre to start a GUI program from Systemd. It wasn't designed for this. GUI programs live in an X session, started by a user. Systemd is for system processes. You should experiment with daemons instead.






                share|improve this answer














                An application needs two things to open a window on an X display. It needs to know the location of the X display; that's conveyed by the DISPLAY environment variable. It also needs to authenticate with the X server. This is conveyed through a cookie, which is a secret value generated by the X server when it starts and stored in a file that only the user who started the X server can access. The default cookie file is ~/.Xauthority.



                If your X server is using the default cookie file location, then adding Environment=XAUTHORITY=/home/dogs/.Xauthority will work (assuming /home/dogs is the home directory of the user who is logged in under X). If you need to find the location, see Can I launch a graphical program on another user's desktop as root? and Open a window on a remote X display (why “Cannot open display”)?



                Alternatively, running the program as the user who is running the X server will work, provided that the cookie file is in the default location (if not, you'll have to locate the cookie file, like in the root case). Add the User directive (e.g. User=dogs).



                Of course the service won't run if there isn't an X display by that number owned by the user you specify.



                It's rather bizarre to start a GUI program from Systemd. It wasn't designed for this. GUI programs live in an X session, started by a user. Systemd is for system processes. You should experiment with daemons instead.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Apr 13 '17 at 12:36









                Community

                1




                1










                answered Aug 2 '13 at 0:24









                Gilles

                520k12610391569




                520k12610391569






















                    up vote
                    -2
                    down vote













                    You can add in .xinitrc :



                    xhost si:localuser:$USER


                    It works for me!






                    share|improve this answer


















                    • 1




                      this is for systemd service files, not .xinitrc
                      – slang
                      Sep 28 '15 at 20:59














                    up vote
                    -2
                    down vote













                    You can add in .xinitrc :



                    xhost si:localuser:$USER


                    It works for me!






                    share|improve this answer


















                    • 1




                      this is for systemd service files, not .xinitrc
                      – slang
                      Sep 28 '15 at 20:59












                    up vote
                    -2
                    down vote










                    up vote
                    -2
                    down vote









                    You can add in .xinitrc :



                    xhost si:localuser:$USER


                    It works for me!






                    share|improve this answer














                    You can add in .xinitrc :



                    xhost si:localuser:$USER


                    It works for me!







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 2 days ago









                    GAD3R

                    24.3k1749103




                    24.3k1749103










                    answered Jul 17 '15 at 10:40









                    Monica

                    1




                    1







                    • 1




                      this is for systemd service files, not .xinitrc
                      – slang
                      Sep 28 '15 at 20:59












                    • 1




                      this is for systemd service files, not .xinitrc
                      – slang
                      Sep 28 '15 at 20:59







                    1




                    1




                    this is for systemd service files, not .xinitrc
                    – slang
                    Sep 28 '15 at 20:59




                    this is for systemd service files, not .xinitrc
                    – slang
                    Sep 28 '15 at 20:59

















                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f85244%2fsetting-display-in-systemd-service-file%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

                    How to check contact read email or not when send email to Individual?

                    Displaying single band from multi-band raster using QGIS

                    How many registers does an x86_64 CPU actually have?