I have the following startupHook in my xmonad.hs

, startupHook = spawn "~/.xmonad/autostart"

I am starting many apps and daemons in this script and it is written to run only once on startup. But this is being run everytime I hit Mod-q, i.e., to reload and test out my changes to xmonad.hs.

I could conditionally run the programs in the autostart script but that turned out to be quite flaky. So, I was wondering if there is a way to xmonad to run this startup hook only on the initial startup and not on reloads.

I tried googling around but came up empty. Is such a thing possible?

Edit: I am running xmonad 0.11 installed from cabal on ubuntu 12.04.

3 Answers 3


As mentionned in the FAQ, the startupHook is run each time xmonad is restarted with MOD+Q.

Also from a software point of view, it seems important that a restart is not different than a stop followed by a start. Thus there is no way to differentiate a start from a restart from within Xmonad. Hence each time Xmonad is started, it will launch the startupHook

To run programs on startup, they propose to use the usual manner by adding them to .xsession or .Xinitrc.

In your case you can just put


at the end of your .Xinitrc file.

  • Yeah, but I don't think the .Xinitrc file is run by LightDM, that is used by Ubuntu. Currently I have a line saying test -n "$(pgrep nm-applet)" && exit 0, so that if nm-applet is already running, I assume its a restart. Not a solution I'm proud of, but it works. Thank you for the answer though.
    – sharat87
    Commented May 1, 2013 at 11:46
  • The problem with this is that I can't use "spawnOn" in .xsession! Maybe there is something I can check in the startupHook, indicating initial start or restart?
    – asjo
    Commented Apr 30, 2015 at 23:18
  • Is there a difference between putting something on the startupHook and putting directly in the main function?
    – ptkato
    Commented Jul 26, 2018 at 19:19

One very slightly dodgy workaround if you're using Ubuntu, and possibly other Debian-based systems, and have installed XMonad via your package manager (which doesn't actually help the person who asked the initial question, but might help others):

Add whatever programs you want to launch to ~/.xmonad/xmonad-session-rc, as if it were a bash script (you don't need to make it executable).


In Ubuntu, XMonad is launched from the login screen thanks to /usr/share/xsessions/xmonad.desktop. The program executed by this is not xmonad itself, but the bash script xmonad-session (present at /usr/bin/xmonad), which sources .xmonad/xmonad-session-rc and hence the contents of that file will be executed when you first log in to xmonad.

The workaround is slightly dodgy, as I think that the purpose of the xmonad-session-rc file was to set environment variables, not run programs, but I'm not certain.


If you use xmonad-contrib, simply import Xmonad.Util.SpawnOnce and then use spawnOnce instead of spawn in your startup hook.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .