How to use notify-send to notify users as root?












0














#!/bin/bash
export DISPLAY=:0
state=$(upower -i $(upower -e | grep BAT) | grep --color=never -E "state" )
stat=${state:25:32}
batperct=$(upower -i $(upower -e | grep '/battery') | grep -E "percentage:")
bat=${batperct:24:26}
intbat=$(echo $bat | cut -d'%' -f1)
if [ "$intbat" -lt "15" -a "$stat" == "discharging" ]; then
notify-send 'battery low..! please plugin charger..... charge is only' "$intbat"
fi


The script runs fine when run as a normal user (bash script.sh). The desktop notification is displayed.



If I run it as root with sudo bash script.sh, the desktop notification is not displayed.



How can I use notify-send to notify users as root?



I am using Arch Linux.










share|improve this question









New contributor




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
















  • 2




    Please show how you are running the script.
    – l0b0
    13 hours ago










  • like when run script like bash script.sh it runs but when i use sudo and run it doesnt
    – Prinz Piuz
    10 hours ago






  • 2




    by "it doesn't" do you mean it "doesn't run and I get a sudo error", or "it doesn't run and emits THIS error" or what?
    – Jeff Schaller
    8 hours ago






  • 1




    did you keep $XAUTHORITY when becoming root?
    – A.B
    7 hours ago










  • Why would you want to run this as root? It's supposed to notify your user, right? Root can't connect to the X server you'r running, which is why it doesn't work, but really why would you want this to be run s root?
    – terdon
    3 hours ago
















0














#!/bin/bash
export DISPLAY=:0
state=$(upower -i $(upower -e | grep BAT) | grep --color=never -E "state" )
stat=${state:25:32}
batperct=$(upower -i $(upower -e | grep '/battery') | grep -E "percentage:")
bat=${batperct:24:26}
intbat=$(echo $bat | cut -d'%' -f1)
if [ "$intbat" -lt "15" -a "$stat" == "discharging" ]; then
notify-send 'battery low..! please plugin charger..... charge is only' "$intbat"
fi


The script runs fine when run as a normal user (bash script.sh). The desktop notification is displayed.



If I run it as root with sudo bash script.sh, the desktop notification is not displayed.



How can I use notify-send to notify users as root?



I am using Arch Linux.










share|improve this question









New contributor




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
















  • 2




    Please show how you are running the script.
    – l0b0
    13 hours ago










  • like when run script like bash script.sh it runs but when i use sudo and run it doesnt
    – Prinz Piuz
    10 hours ago






  • 2




    by "it doesn't" do you mean it "doesn't run and I get a sudo error", or "it doesn't run and emits THIS error" or what?
    – Jeff Schaller
    8 hours ago






  • 1




    did you keep $XAUTHORITY when becoming root?
    – A.B
    7 hours ago










  • Why would you want to run this as root? It's supposed to notify your user, right? Root can't connect to the X server you'r running, which is why it doesn't work, but really why would you want this to be run s root?
    – terdon
    3 hours ago














0












0








0







#!/bin/bash
export DISPLAY=:0
state=$(upower -i $(upower -e | grep BAT) | grep --color=never -E "state" )
stat=${state:25:32}
batperct=$(upower -i $(upower -e | grep '/battery') | grep -E "percentage:")
bat=${batperct:24:26}
intbat=$(echo $bat | cut -d'%' -f1)
if [ "$intbat" -lt "15" -a "$stat" == "discharging" ]; then
notify-send 'battery low..! please plugin charger..... charge is only' "$intbat"
fi


The script runs fine when run as a normal user (bash script.sh). The desktop notification is displayed.



If I run it as root with sudo bash script.sh, the desktop notification is not displayed.



How can I use notify-send to notify users as root?



I am using Arch Linux.










share|improve this question









New contributor




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











#!/bin/bash
export DISPLAY=:0
state=$(upower -i $(upower -e | grep BAT) | grep --color=never -E "state" )
stat=${state:25:32}
batperct=$(upower -i $(upower -e | grep '/battery') | grep -E "percentage:")
bat=${batperct:24:26}
intbat=$(echo $bat | cut -d'%' -f1)
if [ "$intbat" -lt "15" -a "$stat" == "discharging" ]; then
notify-send 'battery low..! please plugin charger..... charge is only' "$intbat"
fi


The script runs fine when run as a normal user (bash script.sh). The desktop notification is displayed.



If I run it as root with sudo bash script.sh, the desktop notification is not displayed.



How can I use notify-send to notify users as root?



I am using Arch Linux.







bash shell-script d-bus






share|improve this question









New contributor




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











share|improve this question









New contributor




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









share|improve this question




share|improve this question








edited 13 mins ago









Ralf

1133




1133






New contributor




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









asked 15 hours ago









Prinz Piuz

1




1




New contributor




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





New contributor





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






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








  • 2




    Please show how you are running the script.
    – l0b0
    13 hours ago










  • like when run script like bash script.sh it runs but when i use sudo and run it doesnt
    – Prinz Piuz
    10 hours ago






  • 2




    by "it doesn't" do you mean it "doesn't run and I get a sudo error", or "it doesn't run and emits THIS error" or what?
    – Jeff Schaller
    8 hours ago






  • 1




    did you keep $XAUTHORITY when becoming root?
    – A.B
    7 hours ago










  • Why would you want to run this as root? It's supposed to notify your user, right? Root can't connect to the X server you'r running, which is why it doesn't work, but really why would you want this to be run s root?
    – terdon
    3 hours ago














  • 2




    Please show how you are running the script.
    – l0b0
    13 hours ago










  • like when run script like bash script.sh it runs but when i use sudo and run it doesnt
    – Prinz Piuz
    10 hours ago






  • 2




    by "it doesn't" do you mean it "doesn't run and I get a sudo error", or "it doesn't run and emits THIS error" or what?
    – Jeff Schaller
    8 hours ago






  • 1




    did you keep $XAUTHORITY when becoming root?
    – A.B
    7 hours ago










  • Why would you want to run this as root? It's supposed to notify your user, right? Root can't connect to the X server you'r running, which is why it doesn't work, but really why would you want this to be run s root?
    – terdon
    3 hours ago








2




2




Please show how you are running the script.
– l0b0
13 hours ago




Please show how you are running the script.
– l0b0
13 hours ago












like when run script like bash script.sh it runs but when i use sudo and run it doesnt
– Prinz Piuz
10 hours ago




like when run script like bash script.sh it runs but when i use sudo and run it doesnt
– Prinz Piuz
10 hours ago




2




2




by "it doesn't" do you mean it "doesn't run and I get a sudo error", or "it doesn't run and emits THIS error" or what?
– Jeff Schaller
8 hours ago




by "it doesn't" do you mean it "doesn't run and I get a sudo error", or "it doesn't run and emits THIS error" or what?
– Jeff Schaller
8 hours ago




1




1




did you keep $XAUTHORITY when becoming root?
– A.B
7 hours ago




did you keep $XAUTHORITY when becoming root?
– A.B
7 hours ago












Why would you want to run this as root? It's supposed to notify your user, right? Root can't connect to the X server you'r running, which is why it doesn't work, but really why would you want this to be run s root?
– terdon
3 hours ago




Why would you want to run this as root? It's supposed to notify your user, right? Root can't connect to the X server you'r running, which is why it doesn't work, but really why would you want this to be run s root?
– terdon
3 hours ago










1 Answer
1






active

oldest

votes


















1














The command notify-send needs the environment variable DBUS_SESSION_BUS_ADDRESS set and must be called with the owner of this session bus as user.



In the following script the function notify_users searches for all dbus-daemons that control a session bus. The command line of such a daemon looks like this:



dbus-daemon --fork --session --address=unix:abstract=/tmp/dbus-ceVHx19Kiy


For this processes the owner and the dbus address is determined. Then we are able to use notify-send. This script should inform all user logged in with a GDM session. But I never tested that.



Note: If you call this as non-root, you are queried for a password due to the use of su.



#!/bin/bash
# To be run as root!

# Inform all logged on users
notify_users()
{
typeset title="$1"
typeset message="$2"

typeset myself="$(whoami)"

typeset ls user bus_addr

typeset IFS=$'n'

# for all dbus-daemon processes that create a session bus
for ln in "$(ps -eo user,args | grep "dbus-daemon.*--session.*--address=" | grep -v grep)"; do
# get the user name
user="$(echo "$ln" | cut '-d ' -f 1)"
# get dbus address and export it as DBUS_SESSION_BUS_ADDRESS
bus_addr="$(echo "$ln" | sed 's/^.*--address=//;s/ .*$//')"
export DBUS_SESSION_BUS_ADDRESS="$bus_addr"
# run notify-send with the correct user
su $user -c "/usr/bin/notify-send '$title' '$message'"
done
}

state=$(upower -i $(upower -e | grep BAT) | grep --color=never -E "state" )
stat=${state:25:32}
batperct=$(upower -i $(upower -e | grep '/battery') | grep -E "percentage:")
bat=${batperct:24:26}
intbat=$(echo $bat | cut -d'%' -f1)
if [ "$intbat" -lt "15" -a "$stat" == "discharging" ]; then
notify_users 'battery low!' "Please plugin charger..... charge is only $intbat%"
fi





share|improve this answer










New contributor




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


















    Your Answer








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

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

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


    }
    });






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










    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f492622%2fhow-to-use-notify-send-to-notify-users-as-root%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The command notify-send needs the environment variable DBUS_SESSION_BUS_ADDRESS set and must be called with the owner of this session bus as user.



    In the following script the function notify_users searches for all dbus-daemons that control a session bus. The command line of such a daemon looks like this:



    dbus-daemon --fork --session --address=unix:abstract=/tmp/dbus-ceVHx19Kiy


    For this processes the owner and the dbus address is determined. Then we are able to use notify-send. This script should inform all user logged in with a GDM session. But I never tested that.



    Note: If you call this as non-root, you are queried for a password due to the use of su.



    #!/bin/bash
    # To be run as root!

    # Inform all logged on users
    notify_users()
    {
    typeset title="$1"
    typeset message="$2"

    typeset myself="$(whoami)"

    typeset ls user bus_addr

    typeset IFS=$'n'

    # for all dbus-daemon processes that create a session bus
    for ln in "$(ps -eo user,args | grep "dbus-daemon.*--session.*--address=" | grep -v grep)"; do
    # get the user name
    user="$(echo "$ln" | cut '-d ' -f 1)"
    # get dbus address and export it as DBUS_SESSION_BUS_ADDRESS
    bus_addr="$(echo "$ln" | sed 's/^.*--address=//;s/ .*$//')"
    export DBUS_SESSION_BUS_ADDRESS="$bus_addr"
    # run notify-send with the correct user
    su $user -c "/usr/bin/notify-send '$title' '$message'"
    done
    }

    state=$(upower -i $(upower -e | grep BAT) | grep --color=never -E "state" )
    stat=${state:25:32}
    batperct=$(upower -i $(upower -e | grep '/battery') | grep -E "percentage:")
    bat=${batperct:24:26}
    intbat=$(echo $bat | cut -d'%' -f1)
    if [ "$intbat" -lt "15" -a "$stat" == "discharging" ]; then
    notify_users 'battery low!' "Please plugin charger..... charge is only $intbat%"
    fi





    share|improve this answer










    New contributor




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























      1














      The command notify-send needs the environment variable DBUS_SESSION_BUS_ADDRESS set and must be called with the owner of this session bus as user.



      In the following script the function notify_users searches for all dbus-daemons that control a session bus. The command line of such a daemon looks like this:



      dbus-daemon --fork --session --address=unix:abstract=/tmp/dbus-ceVHx19Kiy


      For this processes the owner and the dbus address is determined. Then we are able to use notify-send. This script should inform all user logged in with a GDM session. But I never tested that.



      Note: If you call this as non-root, you are queried for a password due to the use of su.



      #!/bin/bash
      # To be run as root!

      # Inform all logged on users
      notify_users()
      {
      typeset title="$1"
      typeset message="$2"

      typeset myself="$(whoami)"

      typeset ls user bus_addr

      typeset IFS=$'n'

      # for all dbus-daemon processes that create a session bus
      for ln in "$(ps -eo user,args | grep "dbus-daemon.*--session.*--address=" | grep -v grep)"; do
      # get the user name
      user="$(echo "$ln" | cut '-d ' -f 1)"
      # get dbus address and export it as DBUS_SESSION_BUS_ADDRESS
      bus_addr="$(echo "$ln" | sed 's/^.*--address=//;s/ .*$//')"
      export DBUS_SESSION_BUS_ADDRESS="$bus_addr"
      # run notify-send with the correct user
      su $user -c "/usr/bin/notify-send '$title' '$message'"
      done
      }

      state=$(upower -i $(upower -e | grep BAT) | grep --color=never -E "state" )
      stat=${state:25:32}
      batperct=$(upower -i $(upower -e | grep '/battery') | grep -E "percentage:")
      bat=${batperct:24:26}
      intbat=$(echo $bat | cut -d'%' -f1)
      if [ "$intbat" -lt "15" -a "$stat" == "discharging" ]; then
      notify_users 'battery low!' "Please plugin charger..... charge is only $intbat%"
      fi





      share|improve this answer










      New contributor




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





















        1












        1








        1






        The command notify-send needs the environment variable DBUS_SESSION_BUS_ADDRESS set and must be called with the owner of this session bus as user.



        In the following script the function notify_users searches for all dbus-daemons that control a session bus. The command line of such a daemon looks like this:



        dbus-daemon --fork --session --address=unix:abstract=/tmp/dbus-ceVHx19Kiy


        For this processes the owner and the dbus address is determined. Then we are able to use notify-send. This script should inform all user logged in with a GDM session. But I never tested that.



        Note: If you call this as non-root, you are queried for a password due to the use of su.



        #!/bin/bash
        # To be run as root!

        # Inform all logged on users
        notify_users()
        {
        typeset title="$1"
        typeset message="$2"

        typeset myself="$(whoami)"

        typeset ls user bus_addr

        typeset IFS=$'n'

        # for all dbus-daemon processes that create a session bus
        for ln in "$(ps -eo user,args | grep "dbus-daemon.*--session.*--address=" | grep -v grep)"; do
        # get the user name
        user="$(echo "$ln" | cut '-d ' -f 1)"
        # get dbus address and export it as DBUS_SESSION_BUS_ADDRESS
        bus_addr="$(echo "$ln" | sed 's/^.*--address=//;s/ .*$//')"
        export DBUS_SESSION_BUS_ADDRESS="$bus_addr"
        # run notify-send with the correct user
        su $user -c "/usr/bin/notify-send '$title' '$message'"
        done
        }

        state=$(upower -i $(upower -e | grep BAT) | grep --color=never -E "state" )
        stat=${state:25:32}
        batperct=$(upower -i $(upower -e | grep '/battery') | grep -E "percentage:")
        bat=${batperct:24:26}
        intbat=$(echo $bat | cut -d'%' -f1)
        if [ "$intbat" -lt "15" -a "$stat" == "discharging" ]; then
        notify_users 'battery low!' "Please plugin charger..... charge is only $intbat%"
        fi





        share|improve this answer










        New contributor




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









        The command notify-send needs the environment variable DBUS_SESSION_BUS_ADDRESS set and must be called with the owner of this session bus as user.



        In the following script the function notify_users searches for all dbus-daemons that control a session bus. The command line of such a daemon looks like this:



        dbus-daemon --fork --session --address=unix:abstract=/tmp/dbus-ceVHx19Kiy


        For this processes the owner and the dbus address is determined. Then we are able to use notify-send. This script should inform all user logged in with a GDM session. But I never tested that.



        Note: If you call this as non-root, you are queried for a password due to the use of su.



        #!/bin/bash
        # To be run as root!

        # Inform all logged on users
        notify_users()
        {
        typeset title="$1"
        typeset message="$2"

        typeset myself="$(whoami)"

        typeset ls user bus_addr

        typeset IFS=$'n'

        # for all dbus-daemon processes that create a session bus
        for ln in "$(ps -eo user,args | grep "dbus-daemon.*--session.*--address=" | grep -v grep)"; do
        # get the user name
        user="$(echo "$ln" | cut '-d ' -f 1)"
        # get dbus address and export it as DBUS_SESSION_BUS_ADDRESS
        bus_addr="$(echo "$ln" | sed 's/^.*--address=//;s/ .*$//')"
        export DBUS_SESSION_BUS_ADDRESS="$bus_addr"
        # run notify-send with the correct user
        su $user -c "/usr/bin/notify-send '$title' '$message'"
        done
        }

        state=$(upower -i $(upower -e | grep BAT) | grep --color=never -E "state" )
        stat=${state:25:32}
        batperct=$(upower -i $(upower -e | grep '/battery') | grep -E "percentage:")
        bat=${batperct:24:26}
        intbat=$(echo $bat | cut -d'%' -f1)
        if [ "$intbat" -lt "15" -a "$stat" == "discharging" ]; then
        notify_users 'battery low!' "Please plugin charger..... charge is only $intbat%"
        fi






        share|improve this answer










        New contributor




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









        share|improve this answer



        share|improve this answer








        edited 2 hours ago





















        New contributor




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









        answered 3 hours ago









        Ralf

        1133




        1133




        New contributor




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





        New contributor





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






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






















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










            draft saved

            draft discarded


















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













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












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
















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


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

            But avoid



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

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


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





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


            Please pay close attention to the following guidance:


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

            But avoid



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

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


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




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f492622%2fhow-to-use-notify-send-to-notify-users-as-root%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            CARDNET

            Boot-repair Failure: Unable to locate package grub-common:i386

            濃尾地震