How to use notify-send to notify users as root?
#!/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
New contributor
|
show 2 more comments
#!/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
New contributor
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
|
show 2 more comments
#!/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
New contributor
#!/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
bash shell-script d-bus
New contributor
New contributor
edited 13 mins ago
Ralf
1133
1133
New contributor
asked 15 hours ago
Prinz Piuz
1
1
New contributor
New contributor
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
|
show 2 more comments
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
|
show 2 more comments
1 Answer
1
active
oldest
votes
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
New contributor
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
New contributor
add a comment |
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
New contributor
add a comment |
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
New contributor
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
New contributor
edited 2 hours ago
New contributor
answered 3 hours ago
Ralf
1133
1133
New contributor
New contributor
add a comment |
add a comment |
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.
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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