I wrote some scripts that have commands such as:

gsettings ...
nohup nm-applet &

Both commands showed feedback when running the script without sudo (as normal user).

When I started the script using sudo, the script successfully ran, but those commands didn't show any feedback.

Running the script with sudo using commands like these doesn't work even when whoami already shows normaluser. Example:

exitfn() {
nohup nm-applet &
sudo -i -u normaluser bash <<EOF
nohup nm-applet &
su - normaluser -c "whoami && nohup nm-applet &"

trap "exitfn" INT
sudo killall nm-applet
sleep 3600

In the end I opted to separate the script parts into normaluser part and a part where sudo is needed, but I am still wondering why this is the case. Is there a way to get these two commands running in a script called with sudo?


gsettings :

It turns out that I need to add gsettings --schemadir since the extension I installed is only in the home dir; not in the root.

Add sudo -E as suggested by @meuh


With sudo, it actually ran, but did not show on the desktop notifbar. Without sudo, it ran and showed on the desktop notifbar.

  • gsettings needs a dbus, and sudo is dropping the variable DBUS_SESSION_BUS_ADDRESS from the env. You need to preserve it. – meuh Jun 5 '18 at 8:09
  • @meuh does this apply to nm-applet as well? – pagkly Jun 5 '18 at 12:26
  • @meuh I have been looking online about but didnt really understand how to apply that to my script. How can I do that? – pagkly Jun 5 '18 at 12:33
  • Use the env command to see what variables it lists before using sudo. Try running the script with sudo -E to preserve these variables. – meuh Jun 5 '18 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.