Run a dbus program in crontab , how to know about the SESSION id?












17














I need to run some program within crontab , but how can the program know about dbus session id ? it's only available for programs launched by session managers.










share|improve this question



























    17














    I need to run some program within crontab , but how can the program know about dbus session id ? it's only available for programs launched by session managers.










    share|improve this question

























      17












      17








      17


      7





      I need to run some program within crontab , but how can the program know about dbus session id ? it's only available for programs launched by session managers.










      share|improve this question













      I need to run some program within crontab , but how can the program know about dbus session id ? it's only available for programs launched by session managers.







      kde d-bus






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 6 '12 at 15:32









      daisydaisy

      28.4k49168301




      28.4k49168301






















          5 Answers
          5






          active

          oldest

          votes


















          21














          The problem is somewhat similar to accessing the X display and finding the location of the X cookie file. (Also, refer to these questions if you want to launch a GUI program on the user's display.)



          Dbus stores the session address in a file in ~/.dbus/session-bus. The name of the file is $machine_id-$display_number, where $machine_id is a randomly generated number stored in /var/lib/dbus/machine-id and $display_number is the X display number ($DISPLAY is :$display_number or :$display_number.$screen_number). The file in ~/.dbus/session-bus is parseable by a shell and contains definitions for DBUS_SESSION_BUS_ADDRESS and DBUS_SESSION_BUS_PID.



          dbus_session_file=~/.dbus/session-bus/$(cat /var/lib/dbus/machine-id)-0
          if [ -e "$dbus_session_file" ]; then
          . "$dbus_session_file"
          export DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_PID
          dbus-send …
          fi


          Beware that there's no guarantee that the dbus daemon is still available. The user may have logged out.



          An alternative method is to find the PID of a process in the desktop session, and obtain the dbus address from its environment.



          export $(</proc/$pid/environ tr \0 \n | grep -E '^DBUS_SESSION_BUS_ADDRESS=')


          If the crontab is running as root and you want to communicate with the session of whatever user is logged in on the console, see Can I launch a graphical program on another user's desktop as root?






          share|improve this answer































            2














            I think if you know the pid of the gnome session manager, then you read the environment from /proc filesystem.



            GNOME_SESSION_PID=<PID_OF_GNOME_SESSION> 
            READ_SESSION_COOKIE="$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$GNOME_SESSION_PID/environ|cut -d= -f2-)"


            Use the SESSION id then, with other programs like notify-send or dbus* tools.



            Cheers.






            share|improve this answer























            • very impressive , actually i'm running KDE4 , so i looked for plasma-desktop instead, thanks !
              – daisy
              Jan 7 '12 at 0:13



















            1














            ps -u yourlogin e | grep -Eo 'dbus-daemon.*address=unix:abstract=/tmp/dbus-[A-Za-z0-9]{10}' | tail -c35





            share|improve this answer





























              1














              I can't comment on Vincenzo's answer, but I find his answer works best for me on KDE4.



              I've had to slightly modify the command though. For me it's:



              ps -u yourlogin e | grep -Eo 'dbus-daemon.*ADDRESS=unix:abstract=/tmp/dbus-[A-Za-z0-9]{10}' | tail -c35


              Notice ADDRESS in capital letters.






              share|improve this answer































                0














                Yes DBus must have a x session.
                Like earlier answers, 'DBUS_SESSION_BUS_ADDRESS' must have a value before starting . However, you could use 'dbus-launch' to create this value. Following snippet could be put in a script called from crontab.



                if test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
                eval `dbus-launch --sh-syntax`
                fi
                echo "D-Bus daemon address is:"
                echo "$DBUS_SESSION_BUS_ADDRESS"

                # -E to export DBUS_SESSION_BUS_ADDRESS. Crucial to make DBUS work.
                sudo -u <user> -E <program>


                One setback with this solution is that the session might hang around after you've stopped your software.





                share








                New contributor




                karnbo 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',
                  autoActivateHeartbeat: false,
                  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%2f28463%2frun-a-dbus-program-in-crontab-how-to-know-about-the-session-id%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  21














                  The problem is somewhat similar to accessing the X display and finding the location of the X cookie file. (Also, refer to these questions if you want to launch a GUI program on the user's display.)



                  Dbus stores the session address in a file in ~/.dbus/session-bus. The name of the file is $machine_id-$display_number, where $machine_id is a randomly generated number stored in /var/lib/dbus/machine-id and $display_number is the X display number ($DISPLAY is :$display_number or :$display_number.$screen_number). The file in ~/.dbus/session-bus is parseable by a shell and contains definitions for DBUS_SESSION_BUS_ADDRESS and DBUS_SESSION_BUS_PID.



                  dbus_session_file=~/.dbus/session-bus/$(cat /var/lib/dbus/machine-id)-0
                  if [ -e "$dbus_session_file" ]; then
                  . "$dbus_session_file"
                  export DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_PID
                  dbus-send …
                  fi


                  Beware that there's no guarantee that the dbus daemon is still available. The user may have logged out.



                  An alternative method is to find the PID of a process in the desktop session, and obtain the dbus address from its environment.



                  export $(</proc/$pid/environ tr \0 \n | grep -E '^DBUS_SESSION_BUS_ADDRESS=')


                  If the crontab is running as root and you want to communicate with the session of whatever user is logged in on the console, see Can I launch a graphical program on another user's desktop as root?






                  share|improve this answer




























                    21














                    The problem is somewhat similar to accessing the X display and finding the location of the X cookie file. (Also, refer to these questions if you want to launch a GUI program on the user's display.)



                    Dbus stores the session address in a file in ~/.dbus/session-bus. The name of the file is $machine_id-$display_number, where $machine_id is a randomly generated number stored in /var/lib/dbus/machine-id and $display_number is the X display number ($DISPLAY is :$display_number or :$display_number.$screen_number). The file in ~/.dbus/session-bus is parseable by a shell and contains definitions for DBUS_SESSION_BUS_ADDRESS and DBUS_SESSION_BUS_PID.



                    dbus_session_file=~/.dbus/session-bus/$(cat /var/lib/dbus/machine-id)-0
                    if [ -e "$dbus_session_file" ]; then
                    . "$dbus_session_file"
                    export DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_PID
                    dbus-send …
                    fi


                    Beware that there's no guarantee that the dbus daemon is still available. The user may have logged out.



                    An alternative method is to find the PID of a process in the desktop session, and obtain the dbus address from its environment.



                    export $(</proc/$pid/environ tr \0 \n | grep -E '^DBUS_SESSION_BUS_ADDRESS=')


                    If the crontab is running as root and you want to communicate with the session of whatever user is logged in on the console, see Can I launch a graphical program on another user's desktop as root?






                    share|improve this answer


























                      21












                      21








                      21






                      The problem is somewhat similar to accessing the X display and finding the location of the X cookie file. (Also, refer to these questions if you want to launch a GUI program on the user's display.)



                      Dbus stores the session address in a file in ~/.dbus/session-bus. The name of the file is $machine_id-$display_number, where $machine_id is a randomly generated number stored in /var/lib/dbus/machine-id and $display_number is the X display number ($DISPLAY is :$display_number or :$display_number.$screen_number). The file in ~/.dbus/session-bus is parseable by a shell and contains definitions for DBUS_SESSION_BUS_ADDRESS and DBUS_SESSION_BUS_PID.



                      dbus_session_file=~/.dbus/session-bus/$(cat /var/lib/dbus/machine-id)-0
                      if [ -e "$dbus_session_file" ]; then
                      . "$dbus_session_file"
                      export DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_PID
                      dbus-send …
                      fi


                      Beware that there's no guarantee that the dbus daemon is still available. The user may have logged out.



                      An alternative method is to find the PID of a process in the desktop session, and obtain the dbus address from its environment.



                      export $(</proc/$pid/environ tr \0 \n | grep -E '^DBUS_SESSION_BUS_ADDRESS=')


                      If the crontab is running as root and you want to communicate with the session of whatever user is logged in on the console, see Can I launch a graphical program on another user's desktop as root?






                      share|improve this answer














                      The problem is somewhat similar to accessing the X display and finding the location of the X cookie file. (Also, refer to these questions if you want to launch a GUI program on the user's display.)



                      Dbus stores the session address in a file in ~/.dbus/session-bus. The name of the file is $machine_id-$display_number, where $machine_id is a randomly generated number stored in /var/lib/dbus/machine-id and $display_number is the X display number ($DISPLAY is :$display_number or :$display_number.$screen_number). The file in ~/.dbus/session-bus is parseable by a shell and contains definitions for DBUS_SESSION_BUS_ADDRESS and DBUS_SESSION_BUS_PID.



                      dbus_session_file=~/.dbus/session-bus/$(cat /var/lib/dbus/machine-id)-0
                      if [ -e "$dbus_session_file" ]; then
                      . "$dbus_session_file"
                      export DBUS_SESSION_BUS_ADDRESS DBUS_SESSION_BUS_PID
                      dbus-send …
                      fi


                      Beware that there's no guarantee that the dbus daemon is still available. The user may have logged out.



                      An alternative method is to find the PID of a process in the desktop session, and obtain the dbus address from its environment.



                      export $(</proc/$pid/environ tr \0 \n | grep -E '^DBUS_SESSION_BUS_ADDRESS=')


                      If the crontab is running as root and you want to communicate with the session of whatever user is logged in on the console, see Can I launch a graphical program on another user's desktop as root?







                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Apr 13 '17 at 12:36









                      Community

                      1




                      1










                      answered Jan 7 '12 at 1:25









                      GillesGilles

                      530k12810611588




                      530k12810611588

























                          2














                          I think if you know the pid of the gnome session manager, then you read the environment from /proc filesystem.



                          GNOME_SESSION_PID=<PID_OF_GNOME_SESSION> 
                          READ_SESSION_COOKIE="$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$GNOME_SESSION_PID/environ|cut -d= -f2-)"


                          Use the SESSION id then, with other programs like notify-send or dbus* tools.



                          Cheers.






                          share|improve this answer























                          • very impressive , actually i'm running KDE4 , so i looked for plasma-desktop instead, thanks !
                            – daisy
                            Jan 7 '12 at 0:13
















                          2














                          I think if you know the pid of the gnome session manager, then you read the environment from /proc filesystem.



                          GNOME_SESSION_PID=<PID_OF_GNOME_SESSION> 
                          READ_SESSION_COOKIE="$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$GNOME_SESSION_PID/environ|cut -d= -f2-)"


                          Use the SESSION id then, with other programs like notify-send or dbus* tools.



                          Cheers.






                          share|improve this answer























                          • very impressive , actually i'm running KDE4 , so i looked for plasma-desktop instead, thanks !
                            – daisy
                            Jan 7 '12 at 0:13














                          2












                          2








                          2






                          I think if you know the pid of the gnome session manager, then you read the environment from /proc filesystem.



                          GNOME_SESSION_PID=<PID_OF_GNOME_SESSION> 
                          READ_SESSION_COOKIE="$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$GNOME_SESSION_PID/environ|cut -d= -f2-)"


                          Use the SESSION id then, with other programs like notify-send or dbus* tools.



                          Cheers.






                          share|improve this answer














                          I think if you know the pid of the gnome session manager, then you read the environment from /proc filesystem.



                          GNOME_SESSION_PID=<PID_OF_GNOME_SESSION> 
                          READ_SESSION_COOKIE="$(grep -z DBUS_SESSION_BUS_ADDRESS /proc/$GNOME_SESSION_PID/environ|cut -d= -f2-)"


                          Use the SESSION id then, with other programs like notify-send or dbus* tools.



                          Cheers.







                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Jan 6 '12 at 16:43

























                          answered Jan 6 '12 at 16:27









                          Nikhil MulleyNikhil Mulley

                          6,3472245




                          6,3472245












                          • very impressive , actually i'm running KDE4 , so i looked for plasma-desktop instead, thanks !
                            – daisy
                            Jan 7 '12 at 0:13


















                          • very impressive , actually i'm running KDE4 , so i looked for plasma-desktop instead, thanks !
                            – daisy
                            Jan 7 '12 at 0:13
















                          very impressive , actually i'm running KDE4 , so i looked for plasma-desktop instead, thanks !
                          – daisy
                          Jan 7 '12 at 0:13




                          very impressive , actually i'm running KDE4 , so i looked for plasma-desktop instead, thanks !
                          – daisy
                          Jan 7 '12 at 0:13











                          1














                          ps -u yourlogin e | grep -Eo 'dbus-daemon.*address=unix:abstract=/tmp/dbus-[A-Za-z0-9]{10}' | tail -c35





                          share|improve this answer


























                            1














                            ps -u yourlogin e | grep -Eo 'dbus-daemon.*address=unix:abstract=/tmp/dbus-[A-Za-z0-9]{10}' | tail -c35





                            share|improve this answer
























                              1












                              1








                              1






                              ps -u yourlogin e | grep -Eo 'dbus-daemon.*address=unix:abstract=/tmp/dbus-[A-Za-z0-9]{10}' | tail -c35





                              share|improve this answer












                              ps -u yourlogin e | grep -Eo 'dbus-daemon.*address=unix:abstract=/tmp/dbus-[A-Za-z0-9]{10}' | tail -c35






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Jun 17 '14 at 13:32









                              VincenzoVincenzo

                              111




                              111























                                  1














                                  I can't comment on Vincenzo's answer, but I find his answer works best for me on KDE4.



                                  I've had to slightly modify the command though. For me it's:



                                  ps -u yourlogin e | grep -Eo 'dbus-daemon.*ADDRESS=unix:abstract=/tmp/dbus-[A-Za-z0-9]{10}' | tail -c35


                                  Notice ADDRESS in capital letters.






                                  share|improve this answer




























                                    1














                                    I can't comment on Vincenzo's answer, but I find his answer works best for me on KDE4.



                                    I've had to slightly modify the command though. For me it's:



                                    ps -u yourlogin e | grep -Eo 'dbus-daemon.*ADDRESS=unix:abstract=/tmp/dbus-[A-Za-z0-9]{10}' | tail -c35


                                    Notice ADDRESS in capital letters.






                                    share|improve this answer


























                                      1












                                      1








                                      1






                                      I can't comment on Vincenzo's answer, but I find his answer works best for me on KDE4.



                                      I've had to slightly modify the command though. For me it's:



                                      ps -u yourlogin e | grep -Eo 'dbus-daemon.*ADDRESS=unix:abstract=/tmp/dbus-[A-Za-z0-9]{10}' | tail -c35


                                      Notice ADDRESS in capital letters.






                                      share|improve this answer














                                      I can't comment on Vincenzo's answer, but I find his answer works best for me on KDE4.



                                      I've had to slightly modify the command though. For me it's:



                                      ps -u yourlogin e | grep -Eo 'dbus-daemon.*ADDRESS=unix:abstract=/tmp/dbus-[A-Za-z0-9]{10}' | tail -c35


                                      Notice ADDRESS in capital letters.







                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Apr 13 '17 at 12:36









                                      Community

                                      1




                                      1










                                      answered Apr 22 '16 at 9:53









                                      noricnoric

                                      111




                                      111























                                          0














                                          Yes DBus must have a x session.
                                          Like earlier answers, 'DBUS_SESSION_BUS_ADDRESS' must have a value before starting . However, you could use 'dbus-launch' to create this value. Following snippet could be put in a script called from crontab.



                                          if test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
                                          eval `dbus-launch --sh-syntax`
                                          fi
                                          echo "D-Bus daemon address is:"
                                          echo "$DBUS_SESSION_BUS_ADDRESS"

                                          # -E to export DBUS_SESSION_BUS_ADDRESS. Crucial to make DBUS work.
                                          sudo -u <user> -E <program>


                                          One setback with this solution is that the session might hang around after you've stopped your software.





                                          share








                                          New contributor




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























                                            0














                                            Yes DBus must have a x session.
                                            Like earlier answers, 'DBUS_SESSION_BUS_ADDRESS' must have a value before starting . However, you could use 'dbus-launch' to create this value. Following snippet could be put in a script called from crontab.



                                            if test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
                                            eval `dbus-launch --sh-syntax`
                                            fi
                                            echo "D-Bus daemon address is:"
                                            echo "$DBUS_SESSION_BUS_ADDRESS"

                                            # -E to export DBUS_SESSION_BUS_ADDRESS. Crucial to make DBUS work.
                                            sudo -u <user> -E <program>


                                            One setback with this solution is that the session might hang around after you've stopped your software.





                                            share








                                            New contributor




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





















                                              0












                                              0








                                              0






                                              Yes DBus must have a x session.
                                              Like earlier answers, 'DBUS_SESSION_BUS_ADDRESS' must have a value before starting . However, you could use 'dbus-launch' to create this value. Following snippet could be put in a script called from crontab.



                                              if test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
                                              eval `dbus-launch --sh-syntax`
                                              fi
                                              echo "D-Bus daemon address is:"
                                              echo "$DBUS_SESSION_BUS_ADDRESS"

                                              # -E to export DBUS_SESSION_BUS_ADDRESS. Crucial to make DBUS work.
                                              sudo -u <user> -E <program>


                                              One setback with this solution is that the session might hang around after you've stopped your software.





                                              share








                                              New contributor




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









                                              Yes DBus must have a x session.
                                              Like earlier answers, 'DBUS_SESSION_BUS_ADDRESS' must have a value before starting . However, you could use 'dbus-launch' to create this value. Following snippet could be put in a script called from crontab.



                                              if test -z "$DBUS_SESSION_BUS_ADDRESS" ; then
                                              eval `dbus-launch --sh-syntax`
                                              fi
                                              echo "D-Bus daemon address is:"
                                              echo "$DBUS_SESSION_BUS_ADDRESS"

                                              # -E to export DBUS_SESSION_BUS_ADDRESS. Crucial to make DBUS work.
                                              sudo -u <user> -E <program>


                                              One setback with this solution is that the session might hang around after you've stopped your software.






                                              share








                                              New contributor




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








                                              share


                                              share






                                              New contributor




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









                                              answered 4 mins ago









                                              karnbokarnbo

                                              1




                                              1




                                              New contributor




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





                                              New contributor





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






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






























                                                  draft saved

                                                  draft discarded




















































                                                  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%2f28463%2frun-a-dbus-program-in-crontab-how-to-know-about-the-session-id%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 rewrite equation of hyperbola in standard form

                                                  No ethernet ip address in my vocore2