Is there a way to ask gnome-shell to run a shell script every time it starts?

(Like after a Alt+F2 then r, not on Login)

NB: If you think it's about the start-up list, it's not : do a Alt+F2 then r and tell me if the apps in start-up list are launched: They're not (at least not on my setup).

Unfortunately, through a shell script it's not working yet :

 gnome-shell --replace
 sleep 10
## reset screen config
 xrandr --output HDMI1 --mode 1920x1080 --pos 0x0 --rotate normal --output VIRTUAL1 --off --output DP1 --off --output eDP1 --off --output VGA1 --off

## reset keyboard
setxkbmap fr

the xrandr command is working as expected on startup

if i type in terminal sh rgnome.sh, gnome is restarted, but the next commands don't seem to get executed (and if i close the terminal it kills gnome); i'm not versed in shell scripting, so i may be doing something obvious very wrong

  • If it's a bug and it's reported, you should wait until they fix it. Through in SO asking for workarounds for bugs is ok, here's not.
    – Braiam
    May 16, 2014 at 20:57
  • ok mister, let's rephrase
    – mikakun
    May 16, 2014 at 22:35
  • so i have to repost now I guess until someone with half a brain and a real ubuntu expert can answer, really feeling this place is run by noobs
    – mikakun
    May 17, 2014 at 12:24
  • How is Alt + f2 +r different from Alt+F2?
    – jobin
    May 19, 2014 at 7:31
  • @mikakun, usually you should use your .bashrc or .bash_profile files to execute this kind of scripts, but you're questions lacks some details about what you really want to do that might keep the experts at a distance, so, what do you want to achieve at gnome-shell start ?
    – Decebal
    May 19, 2014 at 7:50

3 Answers 3


You can create a shell script available within your path, maybe call it r.sh.

That script should call the gnome-shell --replace command, and then the commands you use to fix your display config.

You might need to wait a couple of seconds before running those commands in order to wait for the replace to succeed.


The script might be something along the lines of this:

gnome-shell --replace
sleep 2
  • how can you delay an execeution in shell ? what about && (i thought it was executing the command after after the command before)
    – mikakun
    May 21, 2014 at 9:22
  • I'm not sure how the replace works, you'll have to try it out. If executing just after running the command is enough (like you are trying) then we are ok, if not, you might need to call the sleep command before...
    – adosaiguas
    May 21, 2014 at 9:25
  • no luck so far unfortunately (see edited question)
    – mikakun
    May 21, 2014 at 11:44
  • what happens if you run them step by step from ALT+F2 prompt? First replace, then xrandr?
    – adosaiguas
    May 21, 2014 at 12:53
  • something weird happened : i tested gnome-shell --replace in alt+f2 & it did reset the proper display config on its own... but it did it just once... the xrandr command always work rom alt+f2 or the normal terminal as long as it's not after && or in the same script as the gnome --replace
    – mikakun
    May 21, 2014 at 13:20

Your shell script does not work because the xrandr command is waiting for the gnome-shell --restart to exit. Since that does not happen while gnome is running, the xrandr command is not executed. You need to tell your script to run it in the background, that way the subsequent commands will also be run:

#!/usr/bin/env bash

## Start gnome-shell in the background. That's what the & does
gnome-shell --replace &
sleep 10
xrandr --output HDMI1 --mode 1920x1080 --pos 0x0 --rotate normal --output VIRTUAL1 --off --output DP1 --off --output eDP1 --off --output VGA1 --off

Now, save the script somewhere in the global $PATH, for example as /usr/bin/rgnome.sh, make it executable (sudo chmod 755 /usr/bin/rgnome.sh) and then instead of running Alt+F2 and r, use Alt+F2 and rgnome.sh.

  • great thanks ! gnome-shell restart still messes up my keyboard config though, i'll google that; nb : sharing some points to adosaiguas since he came up first with the idea
    – mikakun
    May 22, 2014 at 15:43

After commenting i decided that i should complete an answer:

Here's something that might help you understand how to use .bashrc and .bash_profile

.bash_profile vs .bashrc

Do not worry if you don't have this file already, if you create it the shell should use it while you're logged in as the user in whose home the .bash_profile is saved.

The suggestions listed above will only work if your user has bash as default shell. So you might consider a deeper level as suggested below.

If you are interested on having the script run for all your users you should think about editing the /etc/profile file .

I think that you might find some answers in this section over help.ubuntu.com

  • i'm busy on other stuff just now but will test asap although tbh i don't quite grasp how gnome would reload any bash file on its restart (independently of the user session that remains open on that gnome restart), have you tested that yourself ?
    – mikakun
    May 20, 2014 at 11:03
  • I have a repo that is based on this principle for customizing my zsh shell. But I only use .bashrc or .zshrc in it. At the time I was doing research around it I tried some of those explained above. I'm still not sure of what you're trying to achieve ...
    – Decebal
    May 20, 2014 at 11:07
  • This only works when restarting bash, I would be really surprised if it works with gnome-shell
    – adosaiguas
    May 20, 2014 at 11:10
  • i want to be able to restart gnome without screwing my multiple display config which it does (i can restart a session fine by putting some scripts in the startup list but if i just restart gnome my multiple display config has to be reset manually after that, my keyboard setting also sometimes).
    – mikakun
    May 20, 2014 at 11:16
  • I'm afraid this is completely irrelevant. ~/bash_profile will only be sourced by bash when it runs a login shell. Gnome shell might source ~/.profile (though I think it doesn't) but it has absolutely no reason to source ~/.bash_profile).
    – terdon
    May 22, 2014 at 14:44

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.