167

I would like to implement a keyboard shortcut to restart gnome-shell whenever this one crashes (some bugs 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?

muru
  • 207,228
neydroydrec
  • 4,780

9 Answers9

125

The easiest way is to Alt+F2 and type r then ↵.

agustibr
  • 1,675
53

Since GNOME Shell 3.30.1: You can also do a killall -3 gnome-shell.

Pablo Bianchi
  • 17,371
Denis
  • 539
16

In the case where the whole GNOME Shell got frozen, there is a way to restart it from the terminal without restarting the whole X window:

  1. Press Ctrl+Alt+F2 to switch to a TTY terminal.

  2. Log in with your credentials (username and password) and then run:

    DISPLAY=:0 gnome-shell -r & 
    
  3. Log out by running:

    exit 
    
  4. Press Alt+F1 to switch back to a graphical X windows session.

ino
  • 261
6

Before GNOME Shell 3.30.1 the command should just be gnome-shell --replace.

Pablo Bianchi
  • 17,371
6

The Gnome 40 the equivalent of Alt+F2 restart is:

busctl --user call org.gnome.Shell /org/gnome/Shell org.gnome.Shell Eval s 'Meta.restart("Restarting…")'

Got this tip myself from https://www.linuxuprising.com/2020/07/how-to-restart-gnome-shell-from-command.html.

mipmip
  • 180
2

Sending TERM signal to Gnome Shell 42.1 works for me on Ubuntu 22.04:

pkill -TERM gnome-shell

Note: TERM is the default signal for pkill.

1

In order to proper restart all related, I prefer restarts of whole stack (if tty1..4 works)

sudo killall -9 gdm
sudo killall -9 gdm3
sudo killall -9 lightdm
1

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
}
sehe
  • 223
1

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.

MoreIT
  • 111