Using notify-send with cron

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











up vote
27
down vote

favorite
15












I am using Arch Linux with KDE/Awesome WM. I am trying to get
notify-send to work with cron.



I have tried setting DISPLAY/XAUTHORITY variables, and running notify-send with "sudo -u", all without result.



I am able to call notify-send interactively from the session and get notifications.



FWIW, the cron job is running fine which I verified by echoing stuff to a temporary file. It is just the "notify-send" that fails to work.



Code:



[matrix@morpheus ~]$ crontab -l
* * * * * /home/matrix/scripts/notify.sh

[matrix@morpheus ~]$ cat /home/matrix/scripts/notify.sh
#!/bin/bash
export DISPLAY=127.0.0.1:0.0
export XAUTHORITY=/home/matrix/.Xauthority
echo "testing cron" >/tmp/crontest
sudo -u matrix /usr/bin/notify-send "hello"
echo "now tested notify-send" >>/tmp/crontest

[matrix@morpheus ~]$ cat /tmp/crontest
testing cron
now tested notify-send

[matrix@morpheus ~]$


As you can see the echo before & after notify-send worked.

Also I have tried setting DISPLAY=:0.0



UPDATE:
I searched a bit more and found that DBUS_SESSION_BUS_ADDRESS needs to be set. And after hardcoding this using the value I got from my interactive session, the tiny little "hello" message started popping up on the screen every minute!



But the catch is this variable is not permanent per that post, so I'll have try the the named pipe solution suggested there.



[matrix@morpheus ~]$ cat scripts/notify.sh
#!/bin/bash
export DISPLAY=127.0.0.1:0.0
export XAUTHORITY=/home/matrix/.Xauthority
export DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-BouFPQKgqg,guid=64b483d7678f2196e780849752e67d3c
echo "testing cron" >/tmp/crontest
/usr/bin/notify-send "hello"
echo "now tested notify-send" >>/tmp/crontest


Since cron doesn't seem to support notify-send (at least not directly) is there some other notification system that is more cron friendly that I can use?










share|improve this question























  • This should work as far as I can see. Why don't you add a &>>/tmp/crontest to the notify send line and see if notify-send gives any error messages.
    – Graeme
    Jan 27 '14 at 19:28










  • Out of curiosity, did you try my solution? It seems much simpler and worked perfectly on my Debian. I'm asking just to know if it Debian specific or not
    – terdon♦
    Jan 27 '14 at 19:40










  • @terdon I tried your solution (just a quick test) and it seems to work on my Debian system. I'd like to know if it's generally applicable since it is indeed simpler.
    – Marco
    Jan 27 '14 at 19:48











  • @Marco I'm on LMDE (essentially Debian testing) and using Cinnamon as DE. Can't tell you if it works beyond those.
    – terdon♦
    Jan 27 '14 at 19:52










  • @Marco & terdon: Ubuntu guys are able to do so: ubuntuforums.org/showthread.php?t=1727148
    – justsomeone
    Jan 27 '14 at 19:53














up vote
27
down vote

favorite
15












I am using Arch Linux with KDE/Awesome WM. I am trying to get
notify-send to work with cron.



I have tried setting DISPLAY/XAUTHORITY variables, and running notify-send with "sudo -u", all without result.



I am able to call notify-send interactively from the session and get notifications.



FWIW, the cron job is running fine which I verified by echoing stuff to a temporary file. It is just the "notify-send" that fails to work.



Code:



[matrix@morpheus ~]$ crontab -l
* * * * * /home/matrix/scripts/notify.sh

[matrix@morpheus ~]$ cat /home/matrix/scripts/notify.sh
#!/bin/bash
export DISPLAY=127.0.0.1:0.0
export XAUTHORITY=/home/matrix/.Xauthority
echo "testing cron" >/tmp/crontest
sudo -u matrix /usr/bin/notify-send "hello"
echo "now tested notify-send" >>/tmp/crontest

[matrix@morpheus ~]$ cat /tmp/crontest
testing cron
now tested notify-send

[matrix@morpheus ~]$


As you can see the echo before & after notify-send worked.

Also I have tried setting DISPLAY=:0.0



UPDATE:
I searched a bit more and found that DBUS_SESSION_BUS_ADDRESS needs to be set. And after hardcoding this using the value I got from my interactive session, the tiny little "hello" message started popping up on the screen every minute!



But the catch is this variable is not permanent per that post, so I'll have try the the named pipe solution suggested there.



[matrix@morpheus ~]$ cat scripts/notify.sh
#!/bin/bash
export DISPLAY=127.0.0.1:0.0
export XAUTHORITY=/home/matrix/.Xauthority
export DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-BouFPQKgqg,guid=64b483d7678f2196e780849752e67d3c
echo "testing cron" >/tmp/crontest
/usr/bin/notify-send "hello"
echo "now tested notify-send" >>/tmp/crontest


Since cron doesn't seem to support notify-send (at least not directly) is there some other notification system that is more cron friendly that I can use?










share|improve this question























  • This should work as far as I can see. Why don't you add a &>>/tmp/crontest to the notify send line and see if notify-send gives any error messages.
    – Graeme
    Jan 27 '14 at 19:28










  • Out of curiosity, did you try my solution? It seems much simpler and worked perfectly on my Debian. I'm asking just to know if it Debian specific or not
    – terdon♦
    Jan 27 '14 at 19:40










  • @terdon I tried your solution (just a quick test) and it seems to work on my Debian system. I'd like to know if it's generally applicable since it is indeed simpler.
    – Marco
    Jan 27 '14 at 19:48











  • @Marco I'm on LMDE (essentially Debian testing) and using Cinnamon as DE. Can't tell you if it works beyond those.
    – terdon♦
    Jan 27 '14 at 19:52










  • @Marco & terdon: Ubuntu guys are able to do so: ubuntuforums.org/showthread.php?t=1727148
    – justsomeone
    Jan 27 '14 at 19:53












up vote
27
down vote

favorite
15









up vote
27
down vote

favorite
15






15





I am using Arch Linux with KDE/Awesome WM. I am trying to get
notify-send to work with cron.



I have tried setting DISPLAY/XAUTHORITY variables, and running notify-send with "sudo -u", all without result.



I am able to call notify-send interactively from the session and get notifications.



FWIW, the cron job is running fine which I verified by echoing stuff to a temporary file. It is just the "notify-send" that fails to work.



Code:



[matrix@morpheus ~]$ crontab -l
* * * * * /home/matrix/scripts/notify.sh

[matrix@morpheus ~]$ cat /home/matrix/scripts/notify.sh
#!/bin/bash
export DISPLAY=127.0.0.1:0.0
export XAUTHORITY=/home/matrix/.Xauthority
echo "testing cron" >/tmp/crontest
sudo -u matrix /usr/bin/notify-send "hello"
echo "now tested notify-send" >>/tmp/crontest

[matrix@morpheus ~]$ cat /tmp/crontest
testing cron
now tested notify-send

[matrix@morpheus ~]$


As you can see the echo before & after notify-send worked.

Also I have tried setting DISPLAY=:0.0



UPDATE:
I searched a bit more and found that DBUS_SESSION_BUS_ADDRESS needs to be set. And after hardcoding this using the value I got from my interactive session, the tiny little "hello" message started popping up on the screen every minute!



But the catch is this variable is not permanent per that post, so I'll have try the the named pipe solution suggested there.



[matrix@morpheus ~]$ cat scripts/notify.sh
#!/bin/bash
export DISPLAY=127.0.0.1:0.0
export XAUTHORITY=/home/matrix/.Xauthority
export DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-BouFPQKgqg,guid=64b483d7678f2196e780849752e67d3c
echo "testing cron" >/tmp/crontest
/usr/bin/notify-send "hello"
echo "now tested notify-send" >>/tmp/crontest


Since cron doesn't seem to support notify-send (at least not directly) is there some other notification system that is more cron friendly that I can use?










share|improve this question















I am using Arch Linux with KDE/Awesome WM. I am trying to get
notify-send to work with cron.



I have tried setting DISPLAY/XAUTHORITY variables, and running notify-send with "sudo -u", all without result.



I am able to call notify-send interactively from the session and get notifications.



FWIW, the cron job is running fine which I verified by echoing stuff to a temporary file. It is just the "notify-send" that fails to work.



Code:



[matrix@morpheus ~]$ crontab -l
* * * * * /home/matrix/scripts/notify.sh

[matrix@morpheus ~]$ cat /home/matrix/scripts/notify.sh
#!/bin/bash
export DISPLAY=127.0.0.1:0.0
export XAUTHORITY=/home/matrix/.Xauthority
echo "testing cron" >/tmp/crontest
sudo -u matrix /usr/bin/notify-send "hello"
echo "now tested notify-send" >>/tmp/crontest

[matrix@morpheus ~]$ cat /tmp/crontest
testing cron
now tested notify-send

[matrix@morpheus ~]$


As you can see the echo before & after notify-send worked.

Also I have tried setting DISPLAY=:0.0



UPDATE:
I searched a bit more and found that DBUS_SESSION_BUS_ADDRESS needs to be set. And after hardcoding this using the value I got from my interactive session, the tiny little "hello" message started popping up on the screen every minute!



But the catch is this variable is not permanent per that post, so I'll have try the the named pipe solution suggested there.



[matrix@morpheus ~]$ cat scripts/notify.sh
#!/bin/bash
export DISPLAY=127.0.0.1:0.0
export XAUTHORITY=/home/matrix/.Xauthority
export DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbus-BouFPQKgqg,guid=64b483d7678f2196e780849752e67d3c
echo "testing cron" >/tmp/crontest
/usr/bin/notify-send "hello"
echo "now tested notify-send" >>/tmp/crontest


Since cron doesn't seem to support notify-send (at least not directly) is there some other notification system that is more cron friendly that I can use?







shell scripting cron libnotify






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 13 '17 at 12:36









Community♦

1




1










asked Jan 27 '14 at 19:12









justsomeone

145125




145125











  • This should work as far as I can see. Why don't you add a &>>/tmp/crontest to the notify send line and see if notify-send gives any error messages.
    – Graeme
    Jan 27 '14 at 19:28










  • Out of curiosity, did you try my solution? It seems much simpler and worked perfectly on my Debian. I'm asking just to know if it Debian specific or not
    – terdon♦
    Jan 27 '14 at 19:40










  • @terdon I tried your solution (just a quick test) and it seems to work on my Debian system. I'd like to know if it's generally applicable since it is indeed simpler.
    – Marco
    Jan 27 '14 at 19:48











  • @Marco I'm on LMDE (essentially Debian testing) and using Cinnamon as DE. Can't tell you if it works beyond those.
    – terdon♦
    Jan 27 '14 at 19:52










  • @Marco & terdon: Ubuntu guys are able to do so: ubuntuforums.org/showthread.php?t=1727148
    – justsomeone
    Jan 27 '14 at 19:53
















  • This should work as far as I can see. Why don't you add a &>>/tmp/crontest to the notify send line and see if notify-send gives any error messages.
    – Graeme
    Jan 27 '14 at 19:28










  • Out of curiosity, did you try my solution? It seems much simpler and worked perfectly on my Debian. I'm asking just to know if it Debian specific or not
    – terdon♦
    Jan 27 '14 at 19:40










  • @terdon I tried your solution (just a quick test) and it seems to work on my Debian system. I'd like to know if it's generally applicable since it is indeed simpler.
    – Marco
    Jan 27 '14 at 19:48











  • @Marco I'm on LMDE (essentially Debian testing) and using Cinnamon as DE. Can't tell you if it works beyond those.
    – terdon♦
    Jan 27 '14 at 19:52










  • @Marco & terdon: Ubuntu guys are able to do so: ubuntuforums.org/showthread.php?t=1727148
    – justsomeone
    Jan 27 '14 at 19:53















This should work as far as I can see. Why don't you add a &>>/tmp/crontest to the notify send line and see if notify-send gives any error messages.
– Graeme
Jan 27 '14 at 19:28




This should work as far as I can see. Why don't you add a &>>/tmp/crontest to the notify send line and see if notify-send gives any error messages.
– Graeme
Jan 27 '14 at 19:28












Out of curiosity, did you try my solution? It seems much simpler and worked perfectly on my Debian. I'm asking just to know if it Debian specific or not
– terdon♦
Jan 27 '14 at 19:40




Out of curiosity, did you try my solution? It seems much simpler and worked perfectly on my Debian. I'm asking just to know if it Debian specific or not
– terdon♦
Jan 27 '14 at 19:40












@terdon I tried your solution (just a quick test) and it seems to work on my Debian system. I'd like to know if it's generally applicable since it is indeed simpler.
– Marco
Jan 27 '14 at 19:48





@terdon I tried your solution (just a quick test) and it seems to work on my Debian system. I'd like to know if it's generally applicable since it is indeed simpler.
– Marco
Jan 27 '14 at 19:48













@Marco I'm on LMDE (essentially Debian testing) and using Cinnamon as DE. Can't tell you if it works beyond those.
– terdon♦
Jan 27 '14 at 19:52




@Marco I'm on LMDE (essentially Debian testing) and using Cinnamon as DE. Can't tell you if it works beyond those.
– terdon♦
Jan 27 '14 at 19:52












@Marco & terdon: Ubuntu guys are able to do so: ubuntuforums.org/showthread.php?t=1727148
– justsomeone
Jan 27 '14 at 19:53




@Marco & terdon: Ubuntu guys are able to do so: ubuntuforums.org/showthread.php?t=1727148
– justsomeone
Jan 27 '14 at 19:53










11 Answers
11






active

oldest

votes

















up vote
26
down vote



accepted










You need to set the DBUS_SESSION_BUS_ADDRESS variable. By default cron does
not have access to the variable. To remedy this put the following script
somewhere and call it when the user logs in, for example using awesome and
the run_once function mentioned on the wiki. Any method will do, since it
does not harm if the function is called more often than required.



#!/bin/sh

touch $HOME/.dbus/Xdbus
chmod 600 $HOME/.dbus/Xdbus
env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.dbus/Xdbus
echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.dbus/Xdbus

exit 0


This creates a file containing the required Dbus evironment variable. Then in
the script called by cron you import the variable by sourcing the script:



if [ -r "$HOME/.dbus/Xdbus" ]; then
. "$HOME/.dbus/Xdbus"
fi


Here is an answer that uses the same
mechanism.






share|improve this answer


















  • 1




    Glad to see that I was almost near to the solution. Thanks Marco, that is neat!
    – justsomeone
    Jan 27 '14 at 19:38










  • Great, I reused your answer and added some more detailed instructions here: askubuntu.com/a/537721/34298
    – rubo77
    Oct 18 '14 at 7:09










  • Wouldn't this be a security risk? security.stackexchange.com/questions/71019/…
    – rubo77
    Oct 18 '14 at 7:18










  • @Gilles How could you do this in one line like you mentioned in chat?
    – rubo77
    Oct 18 '14 at 7:34










  • I have tried so many other answers not including DBUS on ubuntu 15.10 and nothing worked. That one is simple and works flawlessly.
    – bastian
    Nov 4 '15 at 11:19

















up vote
15
down vote













You need to set the variables in the crontab itself:



DISPLAY=:0.0
XAUTHORITY=/home/matrix/.Xauthority

# m h dom mon dow command
* * * * * /usr/bin/notify-send "hello"


No sudo needed, at least not on my system.






share|improve this answer




















  • Thanks terdon for your time. This seems to be a simple solution. Unfortunately, this didn't work for me,
    – justsomeone
    Jan 27 '14 at 19:46










  • @justsomeone huh, OK, might depend on the desktop environment then.
    – terdon♦
    Jan 27 '14 at 19:47










  • I think this has got something to do with distro or Desktop Environment. For Ubuntu users, the straight forward solutions seems to work fine from what I have seen in online forums.
    – justsomeone
    Jan 27 '14 at 19:50










  • @justsomeone I'm on Debian (LMDE) using Cinnamon as DE. Might have something to do with how X is started or with the notifications system used by the DE, dunno.
    – terdon♦
    Jan 27 '14 at 19:52










  • Confirmed it works on Ubuntu 14.04/14.10. With GNOME and Unity.
    – Jordon Bedwell
    Sep 29 '14 at 19:32

















up vote
7
down vote













The safest way to get X session related environmental variables is to get them from the environment of a process of the user who is logged on to X. Here is an adaptation of the script that I use for exactly the same purpose (although DBUS_SESSION_BUS_ADDRESS doesn't seem to be a problem for me on Debian):



X=Xorg # works for the given X command
copy_envs="DISPLAY XAUTHORITY DBUS_SESSION_BUS_ADDRESS"

tty=$(ps h -o tty -C $X | head -1)
[ -z "$tty" ] && exit 1

# calling who with LANG empty ensures a consistent date format
who_line=$(LANG= who -u | grep "^[^ ]+[ ]+$tty")

x_user=$(echo $who_line | cut -d ' ' -f 1) # the user associated with the tty
pid=$(echo $who_line | cut -d ' ' -f 7) # the user's logon process

for env_name in $copy_envs
do
# if the variable is not set in the process environment, ensure it does not remain exported here
unset "$env_name"

# use the same line as is in the environ file to export the variable
export "$(grep -az "^$env_name=" /proc/$pid/environ)" >/dev/null
done

sudo -u "$x_user" notify-send "hello"


This sends to message to the first X user it finds, although you could add a loop to send it to all users. Hope this helps.



Update



It seems that updates to the utmp format cause who to print a display instead of a tty in its second column. This actually makes things easier, previously it only printed the display in the comment at the end and I decided this wasn't safe to rely on for the original answer. If this is the case, try this:



X=Xorg # works for the given X command
copy_envs="DISPLAY XAUTHORITY DBUS_SESSION_BUS_ADDRESS"

# calling who with LANG empty ensures a consistent date format
who_line=$(LANG= who -u | awk '$2 ~ ":[0-9]"')

x_user=$(echo $who_line | cut -d ' ' -f 1) # the user associated with the tty
pid=$(echo $who_line | cut -d ' ' -f 7) # the user's logon process

for env_name in $copy_envs
do
# if the variable is not set in the process environment, ensure it does not remain exported here
unset "$env_name"

# use the same line as is in the environ file to export the variable
export "$(grep -az "^$env_name=" /proc/$pid/environ)" >/dev/null
done

sudo -u "$x_user" notify-send "hello"





share|improve this answer






















  • This doesn't work for me on Trusty because there is no tty printed in the who_line command. The output looks like me :0 2015-09-23 10:40 ? 17234.
    – blujay
    Oct 3 '15 at 23:15






  • 1




    @blujay, updated.
    – Graeme
    Oct 4 '15 at 8:06










  • Thanks, that works. However, as I posted in a separate answer, there is an even simpler solution.
    – blujay
    Oct 7 '15 at 11:11











  • @blujay yeah, this was a attempt at a portable answer. Not sure this is really possible though, but it should still work in most cases.
    – Graeme
    Oct 7 '15 at 15:31

















up vote
1
down vote













This is sufficient to make notify-send work for me in a cronjob on Ubuntu Trusty:



#!/bin/bash
export DISPLAY=$(who -u | awk '/s:[0-9]s/ print $2')


It simply exports the DISPLAY for the user the cronjob is running as. It works for me without setting XAUTHORITY or DBUS_SESSION_BUS_ADDRESS.






share|improve this answer
















  • 1




    Works on Ubuntu 16.04 as well. I actually have cron launching a Perl script, which system()s a bash script, which launches a different Perl script, which performs a system("notify-send ..."). Adding the export command to the bash script modified the environment for that script, which the last Perl script then inherited and made available to the system("notify-send ..."). Good find blujay!
    – Tim
    Jan 9 at 18:16


















up vote
1
down vote













For those on Linux who are comfortable installing Python packages, I just released a notify-send-headless program which is working well for me. It searches /proc for the required username and environment variables and then runs notify-send with these variables (it will use sudo to switch to the required user if necessary).






share|improve this answer





























    up vote
    1
    down vote













    You could also make a script:



    #!/usr/bin/env bash
    runuser -l [yourusername] -c 'DISPLAY=:0 notify-send "hey there user"'


    Then run it with sudo. However, since crontab -e runs all commands with the user that created it, the following should suffice when called without sudo:



    #!/usr/bin/env bash
    DISPLAY=:0 notify-send "hey there user"


    At least it does for me. It all seems to be dependent on the environment configuration.






    share|improve this answer



























      up vote
      1
      down vote













      This one-liner worked for me in Manjaro with Cronie:



      # Note: "1000" would be your user id, the output of... "id -u <username>" 
      10 * * * * pj DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send 'Hello world!' 'This is an example notification.'


      Without the very ugly DBUS_blah_blah it doesn't work at all. I also found journalctl -xb -u cronie helpful. I'm not familiar with Cronie yet, but created my "crontab" as /etc/cron.d/mycronjobs and I'm not sure if that filename is required, or if it just reads everything in the cron.d directory.



      I found the solution here https://wiki.archlinux.org/index.php/Desktop_notifications






      share|improve this answer



























        up vote
        0
        down vote













        I use this script in cron to post MPD now playing to twitter every hour



        #!/bin/bash
        export DISPLAY=":0.0"
        msg=$(mpc current -h 192.168.1.33)
        twitter set "#MPD Server nowplaying $msg : http://cirrus.turtil.net:9001"
        #ttytter -status="#MPD Server nowplaying $msg. http://cirrus.turtil.net:9001"

        exit


        similar script using notify-send



        #!/bin/bash
        export DISPLAY=":0.0"
        notify-send -i ~/.icons/48arch.png 'OS- Archlinux x86_64 : DWM Window Manager' 'Installed on Sun Apr 21 2013 at 18:17:22'
        exit


        you may be experiencing problems as KDE uses its own notify-deamon IIRC.






        share|improve this answer





























          up vote
          0
          down vote













          For what its worth....



          I had to use ALL of the following on Debian Jessie to get this to work...



          export DISPLAY=:0.0
          export HOME=/home/$user
          source "$HOME/.dbus/session-bus/*-0"


          Leaving out any one of these caused it to stop working.






          share|improve this answer




















          • That last line won't do anything as written here, because there will be no file literally called *-0 in your session-bus directory. You might have meant source "$HOME"/.dbus/session-bus/*-0.
            – roaima
            Apr 5 '17 at 18:16

















          up vote
          0
          down vote













          Using sudo :



          sudo -u $currentxuser notify-send $message


          Tip :



          We can get the current x user by this command



          ps auxw | grep -i screen | grep -v grep | cut -f 1 -d ' '


          In addition...



          currentxuser=$(ps auxw | grep -i screen | grep -v grep | cut -f 1 -d ' ')
          echo $currentxuser


          Good to know :



          Cron running under root does not have access to x thus all gui commands will not be displayed, one simple solution is to add root to authorized x user for the current x user with this command



          from the x user shell



          xhost local:root


          or



          sudo -u $currentxuser xhost local:root





          share|improve this answer



























            up vote
            -1
            down vote













            Here is a less complex script than what Graeme provided. His script didn't work for me, $who_line was always empty. My script doesn't waste so much time with finding a process. Instead, it just tries all and pick the last useful value found. I'm running xubuntu 14.04 and have some lxc containers running which probably confuse this kind of scripts.



            env="$(
            ps -C init -o uid,pid | while read u p; do
            [ "$u" = "`id -u`" ] || continue
            grep -az '^DBUS_SESSION_BUS_ADDRESS=' /proc/$p/environ | xargs -0
            done | tail -1
            )"

            export "$env"

            notify-send "test"





            share|improve this answer






















            • This doesn't work for me on Trusty because the Xorg process's environment doesn't have DBUS_SESSION_BUS_ADDRESS. I can get it from my shells, but not from the Xorg process.
              – blujay
              Oct 3 '15 at 23:18










            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: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            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%2f111188%2fusing-notify-send-with-cron%23new-answer', 'question_page');

            );

            Post as a guest






























            11 Answers
            11






            active

            oldest

            votes








            11 Answers
            11






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            26
            down vote



            accepted










            You need to set the DBUS_SESSION_BUS_ADDRESS variable. By default cron does
            not have access to the variable. To remedy this put the following script
            somewhere and call it when the user logs in, for example using awesome and
            the run_once function mentioned on the wiki. Any method will do, since it
            does not harm if the function is called more often than required.



            #!/bin/sh

            touch $HOME/.dbus/Xdbus
            chmod 600 $HOME/.dbus/Xdbus
            env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.dbus/Xdbus
            echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.dbus/Xdbus

            exit 0


            This creates a file containing the required Dbus evironment variable. Then in
            the script called by cron you import the variable by sourcing the script:



            if [ -r "$HOME/.dbus/Xdbus" ]; then
            . "$HOME/.dbus/Xdbus"
            fi


            Here is an answer that uses the same
            mechanism.






            share|improve this answer


















            • 1




              Glad to see that I was almost near to the solution. Thanks Marco, that is neat!
              – justsomeone
              Jan 27 '14 at 19:38










            • Great, I reused your answer and added some more detailed instructions here: askubuntu.com/a/537721/34298
              – rubo77
              Oct 18 '14 at 7:09










            • Wouldn't this be a security risk? security.stackexchange.com/questions/71019/…
              – rubo77
              Oct 18 '14 at 7:18










            • @Gilles How could you do this in one line like you mentioned in chat?
              – rubo77
              Oct 18 '14 at 7:34










            • I have tried so many other answers not including DBUS on ubuntu 15.10 and nothing worked. That one is simple and works flawlessly.
              – bastian
              Nov 4 '15 at 11:19














            up vote
            26
            down vote



            accepted










            You need to set the DBUS_SESSION_BUS_ADDRESS variable. By default cron does
            not have access to the variable. To remedy this put the following script
            somewhere and call it when the user logs in, for example using awesome and
            the run_once function mentioned on the wiki. Any method will do, since it
            does not harm if the function is called more often than required.



            #!/bin/sh

            touch $HOME/.dbus/Xdbus
            chmod 600 $HOME/.dbus/Xdbus
            env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.dbus/Xdbus
            echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.dbus/Xdbus

            exit 0


            This creates a file containing the required Dbus evironment variable. Then in
            the script called by cron you import the variable by sourcing the script:



            if [ -r "$HOME/.dbus/Xdbus" ]; then
            . "$HOME/.dbus/Xdbus"
            fi


            Here is an answer that uses the same
            mechanism.






            share|improve this answer


















            • 1




              Glad to see that I was almost near to the solution. Thanks Marco, that is neat!
              – justsomeone
              Jan 27 '14 at 19:38










            • Great, I reused your answer and added some more detailed instructions here: askubuntu.com/a/537721/34298
              – rubo77
              Oct 18 '14 at 7:09










            • Wouldn't this be a security risk? security.stackexchange.com/questions/71019/…
              – rubo77
              Oct 18 '14 at 7:18










            • @Gilles How could you do this in one line like you mentioned in chat?
              – rubo77
              Oct 18 '14 at 7:34










            • I have tried so many other answers not including DBUS on ubuntu 15.10 and nothing worked. That one is simple and works flawlessly.
              – bastian
              Nov 4 '15 at 11:19












            up vote
            26
            down vote



            accepted







            up vote
            26
            down vote



            accepted






            You need to set the DBUS_SESSION_BUS_ADDRESS variable. By default cron does
            not have access to the variable. To remedy this put the following script
            somewhere and call it when the user logs in, for example using awesome and
            the run_once function mentioned on the wiki. Any method will do, since it
            does not harm if the function is called more often than required.



            #!/bin/sh

            touch $HOME/.dbus/Xdbus
            chmod 600 $HOME/.dbus/Xdbus
            env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.dbus/Xdbus
            echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.dbus/Xdbus

            exit 0


            This creates a file containing the required Dbus evironment variable. Then in
            the script called by cron you import the variable by sourcing the script:



            if [ -r "$HOME/.dbus/Xdbus" ]; then
            . "$HOME/.dbus/Xdbus"
            fi


            Here is an answer that uses the same
            mechanism.






            share|improve this answer














            You need to set the DBUS_SESSION_BUS_ADDRESS variable. By default cron does
            not have access to the variable. To remedy this put the following script
            somewhere and call it when the user logs in, for example using awesome and
            the run_once function mentioned on the wiki. Any method will do, since it
            does not harm if the function is called more often than required.



            #!/bin/sh

            touch $HOME/.dbus/Xdbus
            chmod 600 $HOME/.dbus/Xdbus
            env | grep DBUS_SESSION_BUS_ADDRESS > $HOME/.dbus/Xdbus
            echo 'export DBUS_SESSION_BUS_ADDRESS' >> $HOME/.dbus/Xdbus

            exit 0


            This creates a file containing the required Dbus evironment variable. Then in
            the script called by cron you import the variable by sourcing the script:



            if [ -r "$HOME/.dbus/Xdbus" ]; then
            . "$HOME/.dbus/Xdbus"
            fi


            Here is an answer that uses the same
            mechanism.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Apr 13 '17 at 12:36









            Community♦

            1




            1










            answered Jan 27 '14 at 19:30









            Marco

            24.4k580113




            24.4k580113







            • 1




              Glad to see that I was almost near to the solution. Thanks Marco, that is neat!
              – justsomeone
              Jan 27 '14 at 19:38










            • Great, I reused your answer and added some more detailed instructions here: askubuntu.com/a/537721/34298
              – rubo77
              Oct 18 '14 at 7:09










            • Wouldn't this be a security risk? security.stackexchange.com/questions/71019/…
              – rubo77
              Oct 18 '14 at 7:18










            • @Gilles How could you do this in one line like you mentioned in chat?
              – rubo77
              Oct 18 '14 at 7:34










            • I have tried so many other answers not including DBUS on ubuntu 15.10 and nothing worked. That one is simple and works flawlessly.
              – bastian
              Nov 4 '15 at 11:19












            • 1




              Glad to see that I was almost near to the solution. Thanks Marco, that is neat!
              – justsomeone
              Jan 27 '14 at 19:38










            • Great, I reused your answer and added some more detailed instructions here: askubuntu.com/a/537721/34298
              – rubo77
              Oct 18 '14 at 7:09










            • Wouldn't this be a security risk? security.stackexchange.com/questions/71019/…
              – rubo77
              Oct 18 '14 at 7:18










            • @Gilles How could you do this in one line like you mentioned in chat?
              – rubo77
              Oct 18 '14 at 7:34










            • I have tried so many other answers not including DBUS on ubuntu 15.10 and nothing worked. That one is simple and works flawlessly.
              – bastian
              Nov 4 '15 at 11:19







            1




            1




            Glad to see that I was almost near to the solution. Thanks Marco, that is neat!
            – justsomeone
            Jan 27 '14 at 19:38




            Glad to see that I was almost near to the solution. Thanks Marco, that is neat!
            – justsomeone
            Jan 27 '14 at 19:38












            Great, I reused your answer and added some more detailed instructions here: askubuntu.com/a/537721/34298
            – rubo77
            Oct 18 '14 at 7:09




            Great, I reused your answer and added some more detailed instructions here: askubuntu.com/a/537721/34298
            – rubo77
            Oct 18 '14 at 7:09












            Wouldn't this be a security risk? security.stackexchange.com/questions/71019/…
            – rubo77
            Oct 18 '14 at 7:18




            Wouldn't this be a security risk? security.stackexchange.com/questions/71019/…
            – rubo77
            Oct 18 '14 at 7:18












            @Gilles How could you do this in one line like you mentioned in chat?
            – rubo77
            Oct 18 '14 at 7:34




            @Gilles How could you do this in one line like you mentioned in chat?
            – rubo77
            Oct 18 '14 at 7:34












            I have tried so many other answers not including DBUS on ubuntu 15.10 and nothing worked. That one is simple and works flawlessly.
            – bastian
            Nov 4 '15 at 11:19




            I have tried so many other answers not including DBUS on ubuntu 15.10 and nothing worked. That one is simple and works flawlessly.
            – bastian
            Nov 4 '15 at 11:19












            up vote
            15
            down vote













            You need to set the variables in the crontab itself:



            DISPLAY=:0.0
            XAUTHORITY=/home/matrix/.Xauthority

            # m h dom mon dow command
            * * * * * /usr/bin/notify-send "hello"


            No sudo needed, at least not on my system.






            share|improve this answer




















            • Thanks terdon for your time. This seems to be a simple solution. Unfortunately, this didn't work for me,
              – justsomeone
              Jan 27 '14 at 19:46










            • @justsomeone huh, OK, might depend on the desktop environment then.
              – terdon♦
              Jan 27 '14 at 19:47










            • I think this has got something to do with distro or Desktop Environment. For Ubuntu users, the straight forward solutions seems to work fine from what I have seen in online forums.
              – justsomeone
              Jan 27 '14 at 19:50










            • @justsomeone I'm on Debian (LMDE) using Cinnamon as DE. Might have something to do with how X is started or with the notifications system used by the DE, dunno.
              – terdon♦
              Jan 27 '14 at 19:52










            • Confirmed it works on Ubuntu 14.04/14.10. With GNOME and Unity.
              – Jordon Bedwell
              Sep 29 '14 at 19:32














            up vote
            15
            down vote













            You need to set the variables in the crontab itself:



            DISPLAY=:0.0
            XAUTHORITY=/home/matrix/.Xauthority

            # m h dom mon dow command
            * * * * * /usr/bin/notify-send "hello"


            No sudo needed, at least not on my system.






            share|improve this answer




















            • Thanks terdon for your time. This seems to be a simple solution. Unfortunately, this didn't work for me,
              – justsomeone
              Jan 27 '14 at 19:46










            • @justsomeone huh, OK, might depend on the desktop environment then.
              – terdon♦
              Jan 27 '14 at 19:47










            • I think this has got something to do with distro or Desktop Environment. For Ubuntu users, the straight forward solutions seems to work fine from what I have seen in online forums.
              – justsomeone
              Jan 27 '14 at 19:50










            • @justsomeone I'm on Debian (LMDE) using Cinnamon as DE. Might have something to do with how X is started or with the notifications system used by the DE, dunno.
              – terdon♦
              Jan 27 '14 at 19:52










            • Confirmed it works on Ubuntu 14.04/14.10. With GNOME and Unity.
              – Jordon Bedwell
              Sep 29 '14 at 19:32












            up vote
            15
            down vote










            up vote
            15
            down vote









            You need to set the variables in the crontab itself:



            DISPLAY=:0.0
            XAUTHORITY=/home/matrix/.Xauthority

            # m h dom mon dow command
            * * * * * /usr/bin/notify-send "hello"


            No sudo needed, at least not on my system.






            share|improve this answer












            You need to set the variables in the crontab itself:



            DISPLAY=:0.0
            XAUTHORITY=/home/matrix/.Xauthority

            # m h dom mon dow command
            * * * * * /usr/bin/notify-send "hello"


            No sudo needed, at least not on my system.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 27 '14 at 19:24









            terdon♦

            123k28232405




            123k28232405











            • Thanks terdon for your time. This seems to be a simple solution. Unfortunately, this didn't work for me,
              – justsomeone
              Jan 27 '14 at 19:46










            • @justsomeone huh, OK, might depend on the desktop environment then.
              – terdon♦
              Jan 27 '14 at 19:47










            • I think this has got something to do with distro or Desktop Environment. For Ubuntu users, the straight forward solutions seems to work fine from what I have seen in online forums.
              – justsomeone
              Jan 27 '14 at 19:50










            • @justsomeone I'm on Debian (LMDE) using Cinnamon as DE. Might have something to do with how X is started or with the notifications system used by the DE, dunno.
              – terdon♦
              Jan 27 '14 at 19:52










            • Confirmed it works on Ubuntu 14.04/14.10. With GNOME and Unity.
              – Jordon Bedwell
              Sep 29 '14 at 19:32
















            • Thanks terdon for your time. This seems to be a simple solution. Unfortunately, this didn't work for me,
              – justsomeone
              Jan 27 '14 at 19:46










            • @justsomeone huh, OK, might depend on the desktop environment then.
              – terdon♦
              Jan 27 '14 at 19:47










            • I think this has got something to do with distro or Desktop Environment. For Ubuntu users, the straight forward solutions seems to work fine from what I have seen in online forums.
              – justsomeone
              Jan 27 '14 at 19:50










            • @justsomeone I'm on Debian (LMDE) using Cinnamon as DE. Might have something to do with how X is started or with the notifications system used by the DE, dunno.
              – terdon♦
              Jan 27 '14 at 19:52










            • Confirmed it works on Ubuntu 14.04/14.10. With GNOME and Unity.
              – Jordon Bedwell
              Sep 29 '14 at 19:32















            Thanks terdon for your time. This seems to be a simple solution. Unfortunately, this didn't work for me,
            – justsomeone
            Jan 27 '14 at 19:46




            Thanks terdon for your time. This seems to be a simple solution. Unfortunately, this didn't work for me,
            – justsomeone
            Jan 27 '14 at 19:46












            @justsomeone huh, OK, might depend on the desktop environment then.
            – terdon♦
            Jan 27 '14 at 19:47




            @justsomeone huh, OK, might depend on the desktop environment then.
            – terdon♦
            Jan 27 '14 at 19:47












            I think this has got something to do with distro or Desktop Environment. For Ubuntu users, the straight forward solutions seems to work fine from what I have seen in online forums.
            – justsomeone
            Jan 27 '14 at 19:50




            I think this has got something to do with distro or Desktop Environment. For Ubuntu users, the straight forward solutions seems to work fine from what I have seen in online forums.
            – justsomeone
            Jan 27 '14 at 19:50












            @justsomeone I'm on Debian (LMDE) using Cinnamon as DE. Might have something to do with how X is started or with the notifications system used by the DE, dunno.
            – terdon♦
            Jan 27 '14 at 19:52




            @justsomeone I'm on Debian (LMDE) using Cinnamon as DE. Might have something to do with how X is started or with the notifications system used by the DE, dunno.
            – terdon♦
            Jan 27 '14 at 19:52












            Confirmed it works on Ubuntu 14.04/14.10. With GNOME and Unity.
            – Jordon Bedwell
            Sep 29 '14 at 19:32




            Confirmed it works on Ubuntu 14.04/14.10. With GNOME and Unity.
            – Jordon Bedwell
            Sep 29 '14 at 19:32










            up vote
            7
            down vote













            The safest way to get X session related environmental variables is to get them from the environment of a process of the user who is logged on to X. Here is an adaptation of the script that I use for exactly the same purpose (although DBUS_SESSION_BUS_ADDRESS doesn't seem to be a problem for me on Debian):



            X=Xorg # works for the given X command
            copy_envs="DISPLAY XAUTHORITY DBUS_SESSION_BUS_ADDRESS"

            tty=$(ps h -o tty -C $X | head -1)
            [ -z "$tty" ] && exit 1

            # calling who with LANG empty ensures a consistent date format
            who_line=$(LANG= who -u | grep "^[^ ]+[ ]+$tty")

            x_user=$(echo $who_line | cut -d ' ' -f 1) # the user associated with the tty
            pid=$(echo $who_line | cut -d ' ' -f 7) # the user's logon process

            for env_name in $copy_envs
            do
            # if the variable is not set in the process environment, ensure it does not remain exported here
            unset "$env_name"

            # use the same line as is in the environ file to export the variable
            export "$(grep -az "^$env_name=" /proc/$pid/environ)" >/dev/null
            done

            sudo -u "$x_user" notify-send "hello"


            This sends to message to the first X user it finds, although you could add a loop to send it to all users. Hope this helps.



            Update



            It seems that updates to the utmp format cause who to print a display instead of a tty in its second column. This actually makes things easier, previously it only printed the display in the comment at the end and I decided this wasn't safe to rely on for the original answer. If this is the case, try this:



            X=Xorg # works for the given X command
            copy_envs="DISPLAY XAUTHORITY DBUS_SESSION_BUS_ADDRESS"

            # calling who with LANG empty ensures a consistent date format
            who_line=$(LANG= who -u | awk '$2 ~ ":[0-9]"')

            x_user=$(echo $who_line | cut -d ' ' -f 1) # the user associated with the tty
            pid=$(echo $who_line | cut -d ' ' -f 7) # the user's logon process

            for env_name in $copy_envs
            do
            # if the variable is not set in the process environment, ensure it does not remain exported here
            unset "$env_name"

            # use the same line as is in the environ file to export the variable
            export "$(grep -az "^$env_name=" /proc/$pid/environ)" >/dev/null
            done

            sudo -u "$x_user" notify-send "hello"





            share|improve this answer






















            • This doesn't work for me on Trusty because there is no tty printed in the who_line command. The output looks like me :0 2015-09-23 10:40 ? 17234.
              – blujay
              Oct 3 '15 at 23:15






            • 1




              @blujay, updated.
              – Graeme
              Oct 4 '15 at 8:06










            • Thanks, that works. However, as I posted in a separate answer, there is an even simpler solution.
              – blujay
              Oct 7 '15 at 11:11











            • @blujay yeah, this was a attempt at a portable answer. Not sure this is really possible though, but it should still work in most cases.
              – Graeme
              Oct 7 '15 at 15:31














            up vote
            7
            down vote













            The safest way to get X session related environmental variables is to get them from the environment of a process of the user who is logged on to X. Here is an adaptation of the script that I use for exactly the same purpose (although DBUS_SESSION_BUS_ADDRESS doesn't seem to be a problem for me on Debian):



            X=Xorg # works for the given X command
            copy_envs="DISPLAY XAUTHORITY DBUS_SESSION_BUS_ADDRESS"

            tty=$(ps h -o tty -C $X | head -1)
            [ -z "$tty" ] && exit 1

            # calling who with LANG empty ensures a consistent date format
            who_line=$(LANG= who -u | grep "^[^ ]+[ ]+$tty")

            x_user=$(echo $who_line | cut -d ' ' -f 1) # the user associated with the tty
            pid=$(echo $who_line | cut -d ' ' -f 7) # the user's logon process

            for env_name in $copy_envs
            do
            # if the variable is not set in the process environment, ensure it does not remain exported here
            unset "$env_name"

            # use the same line as is in the environ file to export the variable
            export "$(grep -az "^$env_name=" /proc/$pid/environ)" >/dev/null
            done

            sudo -u "$x_user" notify-send "hello"


            This sends to message to the first X user it finds, although you could add a loop to send it to all users. Hope this helps.



            Update



            It seems that updates to the utmp format cause who to print a display instead of a tty in its second column. This actually makes things easier, previously it only printed the display in the comment at the end and I decided this wasn't safe to rely on for the original answer. If this is the case, try this:



            X=Xorg # works for the given X command
            copy_envs="DISPLAY XAUTHORITY DBUS_SESSION_BUS_ADDRESS"

            # calling who with LANG empty ensures a consistent date format
            who_line=$(LANG= who -u | awk '$2 ~ ":[0-9]"')

            x_user=$(echo $who_line | cut -d ' ' -f 1) # the user associated with the tty
            pid=$(echo $who_line | cut -d ' ' -f 7) # the user's logon process

            for env_name in $copy_envs
            do
            # if the variable is not set in the process environment, ensure it does not remain exported here
            unset "$env_name"

            # use the same line as is in the environ file to export the variable
            export "$(grep -az "^$env_name=" /proc/$pid/environ)" >/dev/null
            done

            sudo -u "$x_user" notify-send "hello"





            share|improve this answer






















            • This doesn't work for me on Trusty because there is no tty printed in the who_line command. The output looks like me :0 2015-09-23 10:40 ? 17234.
              – blujay
              Oct 3 '15 at 23:15






            • 1




              @blujay, updated.
              – Graeme
              Oct 4 '15 at 8:06










            • Thanks, that works. However, as I posted in a separate answer, there is an even simpler solution.
              – blujay
              Oct 7 '15 at 11:11











            • @blujay yeah, this was a attempt at a portable answer. Not sure this is really possible though, but it should still work in most cases.
              – Graeme
              Oct 7 '15 at 15:31












            up vote
            7
            down vote










            up vote
            7
            down vote









            The safest way to get X session related environmental variables is to get them from the environment of a process of the user who is logged on to X. Here is an adaptation of the script that I use for exactly the same purpose (although DBUS_SESSION_BUS_ADDRESS doesn't seem to be a problem for me on Debian):



            X=Xorg # works for the given X command
            copy_envs="DISPLAY XAUTHORITY DBUS_SESSION_BUS_ADDRESS"

            tty=$(ps h -o tty -C $X | head -1)
            [ -z "$tty" ] && exit 1

            # calling who with LANG empty ensures a consistent date format
            who_line=$(LANG= who -u | grep "^[^ ]+[ ]+$tty")

            x_user=$(echo $who_line | cut -d ' ' -f 1) # the user associated with the tty
            pid=$(echo $who_line | cut -d ' ' -f 7) # the user's logon process

            for env_name in $copy_envs
            do
            # if the variable is not set in the process environment, ensure it does not remain exported here
            unset "$env_name"

            # use the same line as is in the environ file to export the variable
            export "$(grep -az "^$env_name=" /proc/$pid/environ)" >/dev/null
            done

            sudo -u "$x_user" notify-send "hello"


            This sends to message to the first X user it finds, although you could add a loop to send it to all users. Hope this helps.



            Update



            It seems that updates to the utmp format cause who to print a display instead of a tty in its second column. This actually makes things easier, previously it only printed the display in the comment at the end and I decided this wasn't safe to rely on for the original answer. If this is the case, try this:



            X=Xorg # works for the given X command
            copy_envs="DISPLAY XAUTHORITY DBUS_SESSION_BUS_ADDRESS"

            # calling who with LANG empty ensures a consistent date format
            who_line=$(LANG= who -u | awk '$2 ~ ":[0-9]"')

            x_user=$(echo $who_line | cut -d ' ' -f 1) # the user associated with the tty
            pid=$(echo $who_line | cut -d ' ' -f 7) # the user's logon process

            for env_name in $copy_envs
            do
            # if the variable is not set in the process environment, ensure it does not remain exported here
            unset "$env_name"

            # use the same line as is in the environ file to export the variable
            export "$(grep -az "^$env_name=" /proc/$pid/environ)" >/dev/null
            done

            sudo -u "$x_user" notify-send "hello"





            share|improve this answer














            The safest way to get X session related environmental variables is to get them from the environment of a process of the user who is logged on to X. Here is an adaptation of the script that I use for exactly the same purpose (although DBUS_SESSION_BUS_ADDRESS doesn't seem to be a problem for me on Debian):



            X=Xorg # works for the given X command
            copy_envs="DISPLAY XAUTHORITY DBUS_SESSION_BUS_ADDRESS"

            tty=$(ps h -o tty -C $X | head -1)
            [ -z "$tty" ] && exit 1

            # calling who with LANG empty ensures a consistent date format
            who_line=$(LANG= who -u | grep "^[^ ]+[ ]+$tty")

            x_user=$(echo $who_line | cut -d ' ' -f 1) # the user associated with the tty
            pid=$(echo $who_line | cut -d ' ' -f 7) # the user's logon process

            for env_name in $copy_envs
            do
            # if the variable is not set in the process environment, ensure it does not remain exported here
            unset "$env_name"

            # use the same line as is in the environ file to export the variable
            export "$(grep -az "^$env_name=" /proc/$pid/environ)" >/dev/null
            done

            sudo -u "$x_user" notify-send "hello"


            This sends to message to the first X user it finds, although you could add a loop to send it to all users. Hope this helps.



            Update



            It seems that updates to the utmp format cause who to print a display instead of a tty in its second column. This actually makes things easier, previously it only printed the display in the comment at the end and I decided this wasn't safe to rely on for the original answer. If this is the case, try this:



            X=Xorg # works for the given X command
            copy_envs="DISPLAY XAUTHORITY DBUS_SESSION_BUS_ADDRESS"

            # calling who with LANG empty ensures a consistent date format
            who_line=$(LANG= who -u | awk '$2 ~ ":[0-9]"')

            x_user=$(echo $who_line | cut -d ' ' -f 1) # the user associated with the tty
            pid=$(echo $who_line | cut -d ' ' -f 7) # the user's logon process

            for env_name in $copy_envs
            do
            # if the variable is not set in the process environment, ensure it does not remain exported here
            unset "$env_name"

            # use the same line as is in the environ file to export the variable
            export "$(grep -az "^$env_name=" /proc/$pid/environ)" >/dev/null
            done

            sudo -u "$x_user" notify-send "hello"






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Oct 4 '15 at 8:04

























            answered Jan 27 '14 at 19:51









            Graeme

            24.3k46193




            24.3k46193











            • This doesn't work for me on Trusty because there is no tty printed in the who_line command. The output looks like me :0 2015-09-23 10:40 ? 17234.
              – blujay
              Oct 3 '15 at 23:15






            • 1




              @blujay, updated.
              – Graeme
              Oct 4 '15 at 8:06










            • Thanks, that works. However, as I posted in a separate answer, there is an even simpler solution.
              – blujay
              Oct 7 '15 at 11:11











            • @blujay yeah, this was a attempt at a portable answer. Not sure this is really possible though, but it should still work in most cases.
              – Graeme
              Oct 7 '15 at 15:31
















            • This doesn't work for me on Trusty because there is no tty printed in the who_line command. The output looks like me :0 2015-09-23 10:40 ? 17234.
              – blujay
              Oct 3 '15 at 23:15






            • 1




              @blujay, updated.
              – Graeme
              Oct 4 '15 at 8:06










            • Thanks, that works. However, as I posted in a separate answer, there is an even simpler solution.
              – blujay
              Oct 7 '15 at 11:11











            • @blujay yeah, this was a attempt at a portable answer. Not sure this is really possible though, but it should still work in most cases.
              – Graeme
              Oct 7 '15 at 15:31















            This doesn't work for me on Trusty because there is no tty printed in the who_line command. The output looks like me :0 2015-09-23 10:40 ? 17234.
            – blujay
            Oct 3 '15 at 23:15




            This doesn't work for me on Trusty because there is no tty printed in the who_line command. The output looks like me :0 2015-09-23 10:40 ? 17234.
            – blujay
            Oct 3 '15 at 23:15




            1




            1




            @blujay, updated.
            – Graeme
            Oct 4 '15 at 8:06




            @blujay, updated.
            – Graeme
            Oct 4 '15 at 8:06












            Thanks, that works. However, as I posted in a separate answer, there is an even simpler solution.
            – blujay
            Oct 7 '15 at 11:11





            Thanks, that works. However, as I posted in a separate answer, there is an even simpler solution.
            – blujay
            Oct 7 '15 at 11:11













            @blujay yeah, this was a attempt at a portable answer. Not sure this is really possible though, but it should still work in most cases.
            – Graeme
            Oct 7 '15 at 15:31




            @blujay yeah, this was a attempt at a portable answer. Not sure this is really possible though, but it should still work in most cases.
            – Graeme
            Oct 7 '15 at 15:31










            up vote
            1
            down vote













            This is sufficient to make notify-send work for me in a cronjob on Ubuntu Trusty:



            #!/bin/bash
            export DISPLAY=$(who -u | awk '/s:[0-9]s/ print $2')


            It simply exports the DISPLAY for the user the cronjob is running as. It works for me without setting XAUTHORITY or DBUS_SESSION_BUS_ADDRESS.






            share|improve this answer
















            • 1




              Works on Ubuntu 16.04 as well. I actually have cron launching a Perl script, which system()s a bash script, which launches a different Perl script, which performs a system("notify-send ..."). Adding the export command to the bash script modified the environment for that script, which the last Perl script then inherited and made available to the system("notify-send ..."). Good find blujay!
              – Tim
              Jan 9 at 18:16















            up vote
            1
            down vote













            This is sufficient to make notify-send work for me in a cronjob on Ubuntu Trusty:



            #!/bin/bash
            export DISPLAY=$(who -u | awk '/s:[0-9]s/ print $2')


            It simply exports the DISPLAY for the user the cronjob is running as. It works for me without setting XAUTHORITY or DBUS_SESSION_BUS_ADDRESS.






            share|improve this answer
















            • 1




              Works on Ubuntu 16.04 as well. I actually have cron launching a Perl script, which system()s a bash script, which launches a different Perl script, which performs a system("notify-send ..."). Adding the export command to the bash script modified the environment for that script, which the last Perl script then inherited and made available to the system("notify-send ..."). Good find blujay!
              – Tim
              Jan 9 at 18:16













            up vote
            1
            down vote










            up vote
            1
            down vote









            This is sufficient to make notify-send work for me in a cronjob on Ubuntu Trusty:



            #!/bin/bash
            export DISPLAY=$(who -u | awk '/s:[0-9]s/ print $2')


            It simply exports the DISPLAY for the user the cronjob is running as. It works for me without setting XAUTHORITY or DBUS_SESSION_BUS_ADDRESS.






            share|improve this answer












            This is sufficient to make notify-send work for me in a cronjob on Ubuntu Trusty:



            #!/bin/bash
            export DISPLAY=$(who -u | awk '/s:[0-9]s/ print $2')


            It simply exports the DISPLAY for the user the cronjob is running as. It works for me without setting XAUTHORITY or DBUS_SESSION_BUS_ADDRESS.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Oct 3 '15 at 23:33









            blujay

            33816




            33816







            • 1




              Works on Ubuntu 16.04 as well. I actually have cron launching a Perl script, which system()s a bash script, which launches a different Perl script, which performs a system("notify-send ..."). Adding the export command to the bash script modified the environment for that script, which the last Perl script then inherited and made available to the system("notify-send ..."). Good find blujay!
              – Tim
              Jan 9 at 18:16













            • 1




              Works on Ubuntu 16.04 as well. I actually have cron launching a Perl script, which system()s a bash script, which launches a different Perl script, which performs a system("notify-send ..."). Adding the export command to the bash script modified the environment for that script, which the last Perl script then inherited and made available to the system("notify-send ..."). Good find blujay!
              – Tim
              Jan 9 at 18:16








            1




            1




            Works on Ubuntu 16.04 as well. I actually have cron launching a Perl script, which system()s a bash script, which launches a different Perl script, which performs a system("notify-send ..."). Adding the export command to the bash script modified the environment for that script, which the last Perl script then inherited and made available to the system("notify-send ..."). Good find blujay!
            – Tim
            Jan 9 at 18:16





            Works on Ubuntu 16.04 as well. I actually have cron launching a Perl script, which system()s a bash script, which launches a different Perl script, which performs a system("notify-send ..."). Adding the export command to the bash script modified the environment for that script, which the last Perl script then inherited and made available to the system("notify-send ..."). Good find blujay!
            – Tim
            Jan 9 at 18:16











            up vote
            1
            down vote













            For those on Linux who are comfortable installing Python packages, I just released a notify-send-headless program which is working well for me. It searches /proc for the required username and environment variables and then runs notify-send with these variables (it will use sudo to switch to the required user if necessary).






            share|improve this answer


























              up vote
              1
              down vote













              For those on Linux who are comfortable installing Python packages, I just released a notify-send-headless program which is working well for me. It searches /proc for the required username and environment variables and then runs notify-send with these variables (it will use sudo to switch to the required user if necessary).






              share|improve this answer
























                up vote
                1
                down vote










                up vote
                1
                down vote









                For those on Linux who are comfortable installing Python packages, I just released a notify-send-headless program which is working well for me. It searches /proc for the required username and environment variables and then runs notify-send with these variables (it will use sudo to switch to the required user if necessary).






                share|improve this answer














                For those on Linux who are comfortable installing Python packages, I just released a notify-send-headless program which is working well for me. It searches /proc for the required username and environment variables and then runs notify-send with these variables (it will use sudo to switch to the required user if necessary).







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 12 '16 at 9:30

























                answered Apr 21 '16 at 22:28









                xolox

                68047




                68047




















                    up vote
                    1
                    down vote













                    You could also make a script:



                    #!/usr/bin/env bash
                    runuser -l [yourusername] -c 'DISPLAY=:0 notify-send "hey there user"'


                    Then run it with sudo. However, since crontab -e runs all commands with the user that created it, the following should suffice when called without sudo:



                    #!/usr/bin/env bash
                    DISPLAY=:0 notify-send "hey there user"


                    At least it does for me. It all seems to be dependent on the environment configuration.






                    share|improve this answer
























                      up vote
                      1
                      down vote













                      You could also make a script:



                      #!/usr/bin/env bash
                      runuser -l [yourusername] -c 'DISPLAY=:0 notify-send "hey there user"'


                      Then run it with sudo. However, since crontab -e runs all commands with the user that created it, the following should suffice when called without sudo:



                      #!/usr/bin/env bash
                      DISPLAY=:0 notify-send "hey there user"


                      At least it does for me. It all seems to be dependent on the environment configuration.






                      share|improve this answer






















                        up vote
                        1
                        down vote










                        up vote
                        1
                        down vote









                        You could also make a script:



                        #!/usr/bin/env bash
                        runuser -l [yourusername] -c 'DISPLAY=:0 notify-send "hey there user"'


                        Then run it with sudo. However, since crontab -e runs all commands with the user that created it, the following should suffice when called without sudo:



                        #!/usr/bin/env bash
                        DISPLAY=:0 notify-send "hey there user"


                        At least it does for me. It all seems to be dependent on the environment configuration.






                        share|improve this answer












                        You could also make a script:



                        #!/usr/bin/env bash
                        runuser -l [yourusername] -c 'DISPLAY=:0 notify-send "hey there user"'


                        Then run it with sudo. However, since crontab -e runs all commands with the user that created it, the following should suffice when called without sudo:



                        #!/usr/bin/env bash
                        DISPLAY=:0 notify-send "hey there user"


                        At least it does for me. It all seems to be dependent on the environment configuration.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Feb 11 '17 at 15:26









                        user1112789

                        111




                        111




















                            up vote
                            1
                            down vote













                            This one-liner worked for me in Manjaro with Cronie:



                            # Note: "1000" would be your user id, the output of... "id -u <username>" 
                            10 * * * * pj DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send 'Hello world!' 'This is an example notification.'


                            Without the very ugly DBUS_blah_blah it doesn't work at all. I also found journalctl -xb -u cronie helpful. I'm not familiar with Cronie yet, but created my "crontab" as /etc/cron.d/mycronjobs and I'm not sure if that filename is required, or if it just reads everything in the cron.d directory.



                            I found the solution here https://wiki.archlinux.org/index.php/Desktop_notifications






                            share|improve this answer
























                              up vote
                              1
                              down vote













                              This one-liner worked for me in Manjaro with Cronie:



                              # Note: "1000" would be your user id, the output of... "id -u <username>" 
                              10 * * * * pj DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send 'Hello world!' 'This is an example notification.'


                              Without the very ugly DBUS_blah_blah it doesn't work at all. I also found journalctl -xb -u cronie helpful. I'm not familiar with Cronie yet, but created my "crontab" as /etc/cron.d/mycronjobs and I'm not sure if that filename is required, or if it just reads everything in the cron.d directory.



                              I found the solution here https://wiki.archlinux.org/index.php/Desktop_notifications






                              share|improve this answer






















                                up vote
                                1
                                down vote










                                up vote
                                1
                                down vote









                                This one-liner worked for me in Manjaro with Cronie:



                                # Note: "1000" would be your user id, the output of... "id -u <username>" 
                                10 * * * * pj DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send 'Hello world!' 'This is an example notification.'


                                Without the very ugly DBUS_blah_blah it doesn't work at all. I also found journalctl -xb -u cronie helpful. I'm not familiar with Cronie yet, but created my "crontab" as /etc/cron.d/mycronjobs and I'm not sure if that filename is required, or if it just reads everything in the cron.d directory.



                                I found the solution here https://wiki.archlinux.org/index.php/Desktop_notifications






                                share|improve this answer












                                This one-liner worked for me in Manjaro with Cronie:



                                # Note: "1000" would be your user id, the output of... "id -u <username>" 
                                10 * * * * pj DISPLAY=:0 DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus notify-send 'Hello world!' 'This is an example notification.'


                                Without the very ugly DBUS_blah_blah it doesn't work at all. I also found journalctl -xb -u cronie helpful. I'm not familiar with Cronie yet, but created my "crontab" as /etc/cron.d/mycronjobs and I'm not sure if that filename is required, or if it just reads everything in the cron.d directory.



                                I found the solution here https://wiki.archlinux.org/index.php/Desktop_notifications







                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Feb 16 '17 at 19:19









                                PJ Brunet

                                25729




                                25729




















                                    up vote
                                    0
                                    down vote













                                    I use this script in cron to post MPD now playing to twitter every hour



                                    #!/bin/bash
                                    export DISPLAY=":0.0"
                                    msg=$(mpc current -h 192.168.1.33)
                                    twitter set "#MPD Server nowplaying $msg : http://cirrus.turtil.net:9001"
                                    #ttytter -status="#MPD Server nowplaying $msg. http://cirrus.turtil.net:9001"

                                    exit


                                    similar script using notify-send



                                    #!/bin/bash
                                    export DISPLAY=":0.0"
                                    notify-send -i ~/.icons/48arch.png 'OS- Archlinux x86_64 : DWM Window Manager' 'Installed on Sun Apr 21 2013 at 18:17:22'
                                    exit


                                    you may be experiencing problems as KDE uses its own notify-deamon IIRC.






                                    share|improve this answer


























                                      up vote
                                      0
                                      down vote













                                      I use this script in cron to post MPD now playing to twitter every hour



                                      #!/bin/bash
                                      export DISPLAY=":0.0"
                                      msg=$(mpc current -h 192.168.1.33)
                                      twitter set "#MPD Server nowplaying $msg : http://cirrus.turtil.net:9001"
                                      #ttytter -status="#MPD Server nowplaying $msg. http://cirrus.turtil.net:9001"

                                      exit


                                      similar script using notify-send



                                      #!/bin/bash
                                      export DISPLAY=":0.0"
                                      notify-send -i ~/.icons/48arch.png 'OS- Archlinux x86_64 : DWM Window Manager' 'Installed on Sun Apr 21 2013 at 18:17:22'
                                      exit


                                      you may be experiencing problems as KDE uses its own notify-deamon IIRC.






                                      share|improve this answer
























                                        up vote
                                        0
                                        down vote










                                        up vote
                                        0
                                        down vote









                                        I use this script in cron to post MPD now playing to twitter every hour



                                        #!/bin/bash
                                        export DISPLAY=":0.0"
                                        msg=$(mpc current -h 192.168.1.33)
                                        twitter set "#MPD Server nowplaying $msg : http://cirrus.turtil.net:9001"
                                        #ttytter -status="#MPD Server nowplaying $msg. http://cirrus.turtil.net:9001"

                                        exit


                                        similar script using notify-send



                                        #!/bin/bash
                                        export DISPLAY=":0.0"
                                        notify-send -i ~/.icons/48arch.png 'OS- Archlinux x86_64 : DWM Window Manager' 'Installed on Sun Apr 21 2013 at 18:17:22'
                                        exit


                                        you may be experiencing problems as KDE uses its own notify-deamon IIRC.






                                        share|improve this answer














                                        I use this script in cron to post MPD now playing to twitter every hour



                                        #!/bin/bash
                                        export DISPLAY=":0.0"
                                        msg=$(mpc current -h 192.168.1.33)
                                        twitter set "#MPD Server nowplaying $msg : http://cirrus.turtil.net:9001"
                                        #ttytter -status="#MPD Server nowplaying $msg. http://cirrus.turtil.net:9001"

                                        exit


                                        similar script using notify-send



                                        #!/bin/bash
                                        export DISPLAY=":0.0"
                                        notify-send -i ~/.icons/48arch.png 'OS- Archlinux x86_64 : DWM Window Manager' 'Installed on Sun Apr 21 2013 at 18:17:22'
                                        exit


                                        you may be experiencing problems as KDE uses its own notify-deamon IIRC.







                                        share|improve this answer














                                        share|improve this answer



                                        share|improve this answer








                                        edited Sep 3 '15 at 9:46









                                        Archemar

                                        19.1k93467




                                        19.1k93467










                                        answered Sep 3 '15 at 9:26









                                        cirrus

                                        1




                                        1




















                                            up vote
                                            0
                                            down vote













                                            For what its worth....



                                            I had to use ALL of the following on Debian Jessie to get this to work...



                                            export DISPLAY=:0.0
                                            export HOME=/home/$user
                                            source "$HOME/.dbus/session-bus/*-0"


                                            Leaving out any one of these caused it to stop working.






                                            share|improve this answer




















                                            • That last line won't do anything as written here, because there will be no file literally called *-0 in your session-bus directory. You might have meant source "$HOME"/.dbus/session-bus/*-0.
                                              – roaima
                                              Apr 5 '17 at 18:16














                                            up vote
                                            0
                                            down vote













                                            For what its worth....



                                            I had to use ALL of the following on Debian Jessie to get this to work...



                                            export DISPLAY=:0.0
                                            export HOME=/home/$user
                                            source "$HOME/.dbus/session-bus/*-0"


                                            Leaving out any one of these caused it to stop working.






                                            share|improve this answer




















                                            • That last line won't do anything as written here, because there will be no file literally called *-0 in your session-bus directory. You might have meant source "$HOME"/.dbus/session-bus/*-0.
                                              – roaima
                                              Apr 5 '17 at 18:16












                                            up vote
                                            0
                                            down vote










                                            up vote
                                            0
                                            down vote









                                            For what its worth....



                                            I had to use ALL of the following on Debian Jessie to get this to work...



                                            export DISPLAY=:0.0
                                            export HOME=/home/$user
                                            source "$HOME/.dbus/session-bus/*-0"


                                            Leaving out any one of these caused it to stop working.






                                            share|improve this answer












                                            For what its worth....



                                            I had to use ALL of the following on Debian Jessie to get this to work...



                                            export DISPLAY=:0.0
                                            export HOME=/home/$user
                                            source "$HOME/.dbus/session-bus/*-0"


                                            Leaving out any one of these caused it to stop working.







                                            share|improve this answer












                                            share|improve this answer



                                            share|improve this answer










                                            answered Apr 5 '17 at 16:57









                                            BenJ

                                            1




                                            1











                                            • That last line won't do anything as written here, because there will be no file literally called *-0 in your session-bus directory. You might have meant source "$HOME"/.dbus/session-bus/*-0.
                                              – roaima
                                              Apr 5 '17 at 18:16
















                                            • That last line won't do anything as written here, because there will be no file literally called *-0 in your session-bus directory. You might have meant source "$HOME"/.dbus/session-bus/*-0.
                                              – roaima
                                              Apr 5 '17 at 18:16















                                            That last line won't do anything as written here, because there will be no file literally called *-0 in your session-bus directory. You might have meant source "$HOME"/.dbus/session-bus/*-0.
                                            – roaima
                                            Apr 5 '17 at 18:16




                                            That last line won't do anything as written here, because there will be no file literally called *-0 in your session-bus directory. You might have meant source "$HOME"/.dbus/session-bus/*-0.
                                            – roaima
                                            Apr 5 '17 at 18:16










                                            up vote
                                            0
                                            down vote













                                            Using sudo :



                                            sudo -u $currentxuser notify-send $message


                                            Tip :



                                            We can get the current x user by this command



                                            ps auxw | grep -i screen | grep -v grep | cut -f 1 -d ' '


                                            In addition...



                                            currentxuser=$(ps auxw | grep -i screen | grep -v grep | cut -f 1 -d ' ')
                                            echo $currentxuser


                                            Good to know :



                                            Cron running under root does not have access to x thus all gui commands will not be displayed, one simple solution is to add root to authorized x user for the current x user with this command



                                            from the x user shell



                                            xhost local:root


                                            or



                                            sudo -u $currentxuser xhost local:root





                                            share|improve this answer
























                                              up vote
                                              0
                                              down vote













                                              Using sudo :



                                              sudo -u $currentxuser notify-send $message


                                              Tip :



                                              We can get the current x user by this command



                                              ps auxw | grep -i screen | grep -v grep | cut -f 1 -d ' '


                                              In addition...



                                              currentxuser=$(ps auxw | grep -i screen | grep -v grep | cut -f 1 -d ' ')
                                              echo $currentxuser


                                              Good to know :



                                              Cron running under root does not have access to x thus all gui commands will not be displayed, one simple solution is to add root to authorized x user for the current x user with this command



                                              from the x user shell



                                              xhost local:root


                                              or



                                              sudo -u $currentxuser xhost local:root





                                              share|improve this answer






















                                                up vote
                                                0
                                                down vote










                                                up vote
                                                0
                                                down vote









                                                Using sudo :



                                                sudo -u $currentxuser notify-send $message


                                                Tip :



                                                We can get the current x user by this command



                                                ps auxw | grep -i screen | grep -v grep | cut -f 1 -d ' '


                                                In addition...



                                                currentxuser=$(ps auxw | grep -i screen | grep -v grep | cut -f 1 -d ' ')
                                                echo $currentxuser


                                                Good to know :



                                                Cron running under root does not have access to x thus all gui commands will not be displayed, one simple solution is to add root to authorized x user for the current x user with this command



                                                from the x user shell



                                                xhost local:root


                                                or



                                                sudo -u $currentxuser xhost local:root





                                                share|improve this answer












                                                Using sudo :



                                                sudo -u $currentxuser notify-send $message


                                                Tip :



                                                We can get the current x user by this command



                                                ps auxw | grep -i screen | grep -v grep | cut -f 1 -d ' '


                                                In addition...



                                                currentxuser=$(ps auxw | grep -i screen | grep -v grep | cut -f 1 -d ' ')
                                                echo $currentxuser


                                                Good to know :



                                                Cron running under root does not have access to x thus all gui commands will not be displayed, one simple solution is to add root to authorized x user for the current x user with this command



                                                from the x user shell



                                                xhost local:root


                                                or



                                                sudo -u $currentxuser xhost local:root






                                                share|improve this answer












                                                share|improve this answer



                                                share|improve this answer










                                                answered Aug 19 at 13:19









                                                intika

                                                1605




                                                1605




















                                                    up vote
                                                    -1
                                                    down vote













                                                    Here is a less complex script than what Graeme provided. His script didn't work for me, $who_line was always empty. My script doesn't waste so much time with finding a process. Instead, it just tries all and pick the last useful value found. I'm running xubuntu 14.04 and have some lxc containers running which probably confuse this kind of scripts.



                                                    env="$(
                                                    ps -C init -o uid,pid | while read u p; do
                                                    [ "$u" = "`id -u`" ] || continue
                                                    grep -az '^DBUS_SESSION_BUS_ADDRESS=' /proc/$p/environ | xargs -0
                                                    done | tail -1
                                                    )"

                                                    export "$env"

                                                    notify-send "test"





                                                    share|improve this answer






















                                                    • This doesn't work for me on Trusty because the Xorg process's environment doesn't have DBUS_SESSION_BUS_ADDRESS. I can get it from my shells, but not from the Xorg process.
                                                      – blujay
                                                      Oct 3 '15 at 23:18














                                                    up vote
                                                    -1
                                                    down vote













                                                    Here is a less complex script than what Graeme provided. His script didn't work for me, $who_line was always empty. My script doesn't waste so much time with finding a process. Instead, it just tries all and pick the last useful value found. I'm running xubuntu 14.04 and have some lxc containers running which probably confuse this kind of scripts.



                                                    env="$(
                                                    ps -C init -o uid,pid | while read u p; do
                                                    [ "$u" = "`id -u`" ] || continue
                                                    grep -az '^DBUS_SESSION_BUS_ADDRESS=' /proc/$p/environ | xargs -0
                                                    done | tail -1
                                                    )"

                                                    export "$env"

                                                    notify-send "test"





                                                    share|improve this answer






















                                                    • This doesn't work for me on Trusty because the Xorg process's environment doesn't have DBUS_SESSION_BUS_ADDRESS. I can get it from my shells, but not from the Xorg process.
                                                      – blujay
                                                      Oct 3 '15 at 23:18












                                                    up vote
                                                    -1
                                                    down vote










                                                    up vote
                                                    -1
                                                    down vote









                                                    Here is a less complex script than what Graeme provided. His script didn't work for me, $who_line was always empty. My script doesn't waste so much time with finding a process. Instead, it just tries all and pick the last useful value found. I'm running xubuntu 14.04 and have some lxc containers running which probably confuse this kind of scripts.



                                                    env="$(
                                                    ps -C init -o uid,pid | while read u p; do
                                                    [ "$u" = "`id -u`" ] || continue
                                                    grep -az '^DBUS_SESSION_BUS_ADDRESS=' /proc/$p/environ | xargs -0
                                                    done | tail -1
                                                    )"

                                                    export "$env"

                                                    notify-send "test"





                                                    share|improve this answer














                                                    Here is a less complex script than what Graeme provided. His script didn't work for me, $who_line was always empty. My script doesn't waste so much time with finding a process. Instead, it just tries all and pick the last useful value found. I'm running xubuntu 14.04 and have some lxc containers running which probably confuse this kind of scripts.



                                                    env="$(
                                                    ps -C init -o uid,pid | while read u p; do
                                                    [ "$u" = "`id -u`" ] || continue
                                                    grep -az '^DBUS_SESSION_BUS_ADDRESS=' /proc/$p/environ | xargs -0
                                                    done | tail -1
                                                    )"

                                                    export "$env"

                                                    notify-send "test"






                                                    share|improve this answer














                                                    share|improve this answer



                                                    share|improve this answer








                                                    edited Jul 13 '15 at 10:57

























                                                    answered Jul 13 '15 at 10:51









                                                    Daniel Alder

                                                    448516




                                                    448516











                                                    • This doesn't work for me on Trusty because the Xorg process's environment doesn't have DBUS_SESSION_BUS_ADDRESS. I can get it from my shells, but not from the Xorg process.
                                                      – blujay
                                                      Oct 3 '15 at 23:18
















                                                    • This doesn't work for me on Trusty because the Xorg process's environment doesn't have DBUS_SESSION_BUS_ADDRESS. I can get it from my shells, but not from the Xorg process.
                                                      – blujay
                                                      Oct 3 '15 at 23:18















                                                    This doesn't work for me on Trusty because the Xorg process's environment doesn't have DBUS_SESSION_BUS_ADDRESS. I can get it from my shells, but not from the Xorg process.
                                                    – blujay
                                                    Oct 3 '15 at 23:18




                                                    This doesn't work for me on Trusty because the Xorg process's environment doesn't have DBUS_SESSION_BUS_ADDRESS. I can get it from my shells, but not from the Xorg process.
                                                    – blujay
                                                    Oct 3 '15 at 23:18

















                                                     

                                                    draft saved


                                                    draft discarded















































                                                     


                                                    draft saved


                                                    draft discarded














                                                    StackExchange.ready(
                                                    function ()
                                                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f111188%2fusing-notify-send-with-cron%23new-answer', 'question_page');

                                                    );

                                                    Post as a guest













































































                                                    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?