158

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
  • 197,895
  • 55
  • 485
  • 740
neydroydrec
  • 4,620
  • 1
    NOTE: Alt+F2 and restart will stop all the running programs. (I still don't know how to restart the GUI without stopping the programs.) – Andriy Makukha Aug 17 '18 at 06:38

10 Answers10

181

GNOME Shell 3.30.1 on Ubuntu 18.10

The command to replace gnome-shell would be sending a SIGQUIT signal to it with:

killall -3 gnome-shell

or:

killall -SIGQUIT gnome-shell

As of GNOME Shell 3.30.1 on Ubuntu 18.10, the solution below no longer works, and will kick the user to the login screen, losing all of their work.

Before GNOME Shell 3.30.1

The command to replace gnome-shell would be:

gnome-shell --replace &

The ampersand is added to return the shell prompt after running the command; pressing Ctrl+C or closing the terminal instead would make the desktop unusable, and require a full restart.

From the manual page of gnome-shell:

-r, --replace
       Replace the running window manager

Unsure which version you are using?

The command to check your GNOME Shell version is:

gnome-shell --version
jokerdino
  • 41,320
  • 4
    Thanks I wasn't sure --replace = "restart". – neydroydrec Jan 31 '12 at 15:56
  • 7
    As of GNOME Shell 3.30.1 on Ubuntu 18.10, this solution no longer works, and will kick the user to the login screen, losing all their work. The correct solution is now commandline: "killall -3 gnome-shell", or ALT+f2 and r (if possible). – C.Rogers Nov 15 '18 at 10:48
  • 2
    ALT+f2 (show command prompt) + gnome-shell --replace will work nicely in 18.04. All running programs won't be stopped. – lifeisfoo Jun 10 '19 at 12:42
  • 1
    I add the ampersand to the end of the command to put the process in the background so I can close the terminal without killing gnome-shell: gnome-shell --replace & – Teddy Oct 04 '19 at 13:01
  • WARNING: gnome-shell --replace on my Ubuntu 19.10 machine caused most running applications to close and gnome-shell to enter a problem state with a white screen and a prompt to restart the computer. – Sparr Nov 06 '19 at 17:20
  • 2
    @Sparr - thanks for the warning. Fortunately, the killall -3 gnome-shell answer seems to work on Ubuntu 19.10. – bitinerant Nov 11 '19 at 21:03
  • I had installed and removed a GNOME manually (via ~/.local/share/gnome-shell/extensions/), but GNOME didn't see that and Gnome Tweaks kept showing the extension as installed. Tried gnome-shell --replace hoping to fix the situation, but that didn't help. Only rebooting worked. – Dan Dascalescu Dec 13 '19 at 04:52
  • gnome-shell --version for checking the version, in case anyone else needs it – orangenarwhals Jun 30 '20 at 17:07
  • When gnome-shell v3.28.3 wasn't responding to Ctrl+Alt+F2 for me, gnome-shell --replace did nothing, but killall -3 gnome-shell did restart it without logging me out. – Mark Booth Mar 05 '21 at 10:15
  • 1
    Ubuntu 20.04 Gnome version 3.36.8, doing killall -3 gnome-shell forced me to login again and closed all my apps. – wxz Nov 10 '23 at 17:43
118

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

agustibr
  • 1,597
52

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

Pablo Bianchi
  • 15,657
Denis
  • 521
  • 1
    Probably because this is a good solution where the other command doesn't work. I tried both, the choosen one is good but didn't solve my problem, while this did. – Mitro Nov 05 '18 at 10:00
  • 4
    As of Ubuntu 18.10, GNOME Shell 3.30.1 - THIS IS THE CORRECT SOLUTION. All other solutions restart the session and kick the user out to the login screen, losing all their work. – C.Rogers Nov 15 '18 at 10:44
  • 3
    SIGQUIT (3) doesn't kill the process – Jack Wasey Jan 01 '19 at 11:22
  • 1
    This seemed to work for me. Whereas I was using gnome-shell --replace & before. It worked but, kept running in the terminal. The issue I was having was with Youtube videos in full screen somehow causing Dash-to-Dock and the panel to freeze closed. https://github.com/micheleg/dash-to-dock/issues/298#issuecomment-473622040 – Natetronn Apr 18 '19 at 23:48
  • Matter of style: a Unix sysadmin might prefer pkill -3 gnome-shell rather than killall, which means something different outside of the GNU OS. – Rich Aug 08 '19 at 18:57
  • On some systems, applications will be child processes of gnome-shell, so when you restart it with this method you will STILL lose your work. – Jelle De Loecker Nov 03 '19 at 18:38
  • @smac89 you should remove your comment. It only serves to confuse users. – admirabilis Jan 20 '21 at 12:23
  • @TeresaeJunior done – smac89 Jan 20 '21 at 19:12
  • You need this if Gnome Shell and desktop is freezing and cursor is the only thing that's moving. Just press Ctrl + Alt + F2 then type the above command. It was successful in 18.04. – fsevenm Jan 27 '21 at 16:54
  • Thank goodness for this when my xrdp session had some weird stuck Meta key issue (which I couldn't disable using the sticky keys or accessibility settings) – Sridhar Sarnobat Jan 12 '23 at 08:03
15

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
  • 251
6

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

Pablo Bianchi
  • 15,657
  • 5
    As of GNOME Shell 3.30.1 on Ubuntu 18.10, this solution no longer works, and will kick the user to the login screen, losing all their work. The correct solution is now commandline: "killall -3 gnome-shell", or ALT+f2 and r (if possible). – C.Rogers Nov 15 '18 at 10:50
  • doesn't work for a remote session, so when gnome shell has frozen, this doesn't work. – Jack Wasey Jan 01 '19 at 11:21
5

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
  • 170
  • worked on Debian 11.0 – rofrol Oct 05 '21 at 19:46
  • I wanted to note that this took a few seconds (screen turned black) and then all my apps were closed on 21.10. – LiveWireBT Nov 26 '21 at 03:28
  • This doesn't work for me anymore in GNOME Shell 42.5 – curusarn Oct 26 '22 at 19:03
  • For Gnome Shell 43 (on Xorg), the command is now: busctl --user call org.gnome.Shell /org/gnome/Shell org.gnome.Shell Eval s 'Meta.restart("Restarting…", global.context)'.

    However, executing GJS like this is unfortunately no longer supported by default. You have to run global.context.unsafe_mode = true in Looking Glass first, which makes this rather unhelpful for frequent restarts

    – ZimbiX Jan 18 '23 at 08:24
4

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
  • 224
  • logout USERNAME --force, gives me an output telling me a number is necessary. – Quidam Jun 04 '20 at 05:11
  • 1
    @Quidam That indicates that the variable is not set to a value. You need to set it (which is what happens in the script) – sehe Jun 04 '20 at 09:39
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
  • 1
  • 2