I have a custom command I need to execute on X session open (mouse config).

I made a script out of it, now I'd like to make sure it gets executed on session open.

There are distribution-dependent solutions (startup programs etc.) but I'd rather find a Linux level solution (I happen to change my distro quite often).

I could source my script in .zshrc (or .bashrc), but I don't think .zshrc gets executed before any terminal is open, isn't it?

Also, I'd like this config to be user specific, within my ~ repo, not in the system.

How can I achieve that?

  • Please edit your question and explain what you mean by "session open". Do you mean each time a new graphical session is initiated? Each time a new shell instance is initiated (each time you open a termina, for example)? Each time you log in? Each time the machine reboots? – terdon Mar 21 '16 at 15:16

The usual setup is that X display managers run the /etc/X11/Xsession shell script, and that script sources or runs scripts from the directory /etc/X11/Xsession.d.

On the Ubuntu family of distribution (and probably on other Debian derivatives), one of the standard files in that directory sources the file .xsessionrc in your home directory, if it exists (it doesn't, by default).

If you create that file, you can put relevant commands inside it, including sourcing another .*rc file, or other command. It's mostly useful to set up environment variables for non-shell applications (that is, applications that don't run in any terminal emulation).


  • In some distributions, the .xsessionrc is not sourced. You should check the /etc/X11/Xsession.d directory. Sourcing the user's .xsessionrc is usually done by the /etc/X11/Xsession.d/40x11-common_xsessionrc script. If that file doesn't exist and no other script sources your .xsessionrc, you can always add your own script to source it.
  • The shell that runs /etc/X11/Xsession is usually /bin/sh - which may or may not be a symbolic link to your favorite shell. You should check that shell's capabilities. For example, using [[ may not work. The . and export commands are usually safe.
  • All you said is correct. Unfortunately, I'm struggling because the mouse seems to be initialized after the 40x11-common_xsessionrc gets called. I tried to change it to 55x11-common_xsessionrc as synclient gets initialized in /usr/share/X11/xorg.conf.d/50-synaptics.conf but without success... :( – Augustin Riedinger Mar 21 '16 at 17:39
  • 1
    @AugustinRiedinger there is no connection between the numbers in /etc/X11/Xsession.d and those in /usr/share/X11/xorg.conf. It sounds like you need to run it after the desktop manager loads. If it's just a single command or can be placed in a shell script, you can have the desktop manager launch it automatically. – RealSkeptic Mar 21 '16 at 19:02

I am not sure what you understand by "session open" but the .xxxrc file for a specific shell is called (according to POSIX) whenever an interactive shell is started.

This is true for a login shell as well as for a nested shell in the current session.

  • I mean a X session like in Ubuntu session manager. I just tried it and the .zshrc script did not get executed. – Augustin Riedinger Mar 21 '16 at 15:12
  • .xxxrc needs to be sourced automatically by the shell in such a case as the shell started by an Xtem is an interactive shell. – schily Mar 21 '16 at 15:14
  • How can I do that? Not sure I see what you mean. – Augustin Riedinger Mar 21 '16 at 15:16
  • If you are using zsh and if zsh it not buggy, it should read .zshrc upon startup in an Xterm as this is an interactive shell. – schily Mar 21 '16 at 15:39

.zshrc is a configuration file of zsh. It is read when you start an interactive instance of zsh. An interactive shell is one that you interact with on a terminal. A GUI login does not involve an interactive shell (and, incidentally, does not involve zsh at all on a typical system).

.zshrc cannot help you with GUI configuration. It's read when you open a terminal emulator, or when you log in on a text console or over the network. It is never read at the right time for what you want to do.

There is no fully portable way to run a command on session opening. The most reliable way is to use the startup program facility of your desktop environment of window manager, if it has one (most do). This is the only way that's distribution-agnostic.

If you start the GUI via startx, this executes your ~/.xinitrc, so put the command there.

For logins in graphical mode, if you want a way that's independent of the DE/WM, then you'll need to provide several hooks depending on the distribution. Depending on the display manager¹, on the desktop environment (or session manager or window manager) and on how your distribution (or your system administrator) has set up session types, there may or may not be a file where you can supply commands to run on a GUI login.

  • If you log in to a “custom session”, this executes ~/.xsession only. It's the job of this file to start everything, including the DE/SM/WM.
  • On some configurations, ~/.xprofile is read.
  • On some configurations, ~/.xsessionrc is read.

If a file like .xprofile or .xsessionrc is executed too early, you may need to resort to dirty tricks to get your code to be executed later. Check the scripts in /etc/X11/Xsession.d or similar. This will be even more distribution-dependent. Once again, the simplest and most portable way to execute code on a GUI login, and a way that will be executed after all the system stuff, is to declare a the startup command to your window manager or desktop environment. In fact, the main reason to use a script that hooks into the system session startup script is to have your code executed early, for example because you want to change the way the WM/DE is called.

¹ The display manager is the program that asks for your password when you log in in graphical mode.

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.