Ubuntu: How to restart GNOME Shell from command line?
Channel: Roel Van de Paar
Date: 28/06/2019

Ubuntu: How to restart GNOME Shell from command line?


The Question: I would like to implement a keyboard shortcut to restart gnome-shell whenever
this one crashes (https://bugzilla.gnome.org/
buglist.cgi?query_format=specific&order=relevance%20desc&bug_status=__open__&product=gnome-
shell&content=crash aren't currently fixed just yet). For this I would need a
command line to restart the shell.
The Alt+F2 and restart command won't work when the shell is crashed because the
prompt is implemented in the shell.
Hence, how to restart the gnome-shell from command line?

Solutions Sample (Please watch the whole video to see all solutions, in order of how many people found them helpful):

== This solution helped 5 people ==
The command should just be gnome-shell --replace.

== This solution helped 40 people ==
The command to replace the gnome-shell would be gnome-shell --replace.
From the manual page of gnome-shell:
--replace Replace the running window manager

== This solution helped 28 people ==
The easiest way is to Alt+F2 and type r then ?.

== This solution helped 14 people ==
You can also do a killall -3 gnome-shell.

== This solution helped 1 person ==
I came up with this function to logout users (see at the bottom)
It assumes
 * you have sudo permissions
 * you have bash
 * the users have 1 running X session (although you should be able to issue
 the same logout command multiple times to get rid of remaining sessions)
You can give it multiple usernames:
logout john jane mike elisa
And you can give it additional options:
logout john --force
So, I have the following helper function:
function forcelogout() {
logout "$@" --no-prompt --force
}
Notes:
 * This is a blunt instruments and works by just copying the entire session
 environment. This could be more selective.
 * Sometimes logout seems to take a while
 * In rare occasions the session keeps being reported until someone visits
 the vt where the session ran, but nothing is there anymore)
**** CODE ****
function logout() {
local USERNAME
export USERNAMES=( )
while [ -n "$1" ]; do case "$1" in
-* ) break ;;
*) USERNAMES+=( "$1" ); shift ;;
esac; done

for USERNAME in "${USERNAMES[@]}"; do
local SESSION_PID=$(pgrep -fu "$USERNAME" gnome-session|head -1)
if [ -n "$SESSION_PID" ]; then
(
sudo -u "$USERNAME" cat "/proc/$SESSION_PID/environ" | xargs -0 -n 1 echo
export
echo "gnome-session-quit --logout $@"
) | sudo -u "$USERNAME" sh -;
fi
done
}

== This solution helped 1 person ==
I defined an alias: alias gnomeshellrestart='echo "gnome-shell --replace -d" $
(w| grep "$USER"| awk "{print $3}"|grep ":"|head -1)| at now'
You may be able to start a terminal by right-clicking with the mouse on
background and type there (alt-tab is dysfct then, too), if not, login to a tty
with e.g. ctrl-alt-F2 and run from there.

With thanks & praise to God! With thanks to the many people who have made this project possible! | Content (except music & images) licensed under cc by-sa 3.0 | Music: https://www.bensound.com/royalty-free-music | Images: https://stocksnap.io/license & others | With thanks to user sehe (https://askubuntu.com/users/17941), user muru (https://askubuntu.com/users/158442), user MoreIT (https://askubuntu.com/users/238625), user jokerdino (https://askubuntu.com/users/25798), user ino (https://askubuntu.com/users/778081), user Denis (https://askubuntu.com/users/584232), user Christopher Kyle Horton (https://askubuntu.com/users/18612), user Benjamin (https://askubuntu.com/users/15710), user agustibr (https://askubuntu.com/users/129422), and the Stack Exchange Network (http://askubuntu.com/questions/100226). Trademarks are property of their respective owners. Disclaimer: All information is provided "AS IS" without warranty of any kind. You are responsible for your own actions. Please contact me if anything looks amiss at Roel D.OT VandePaar A.T gmail.com.