I am running LXDE/Openbox on Debian Linux. On logging in I want to:

  • add a user specific directory to the $PATH variable (A)
  • execute some (non-graphical) commands (e.g. mount directories) (B)

The setup should work for graphical login as well as login shells (e.g. SSH). Especially the part about extending the $PATH variable seems to be tricky...

My ideal solution would be to put all the commands (A and B) in a separate script file (F) and source it from .xsessionrc (for graphical login via the session manager) and .profile (for login shells). Sourcing F from .profile works nicely, but sourcing from .xsessionrc seems to fail.

So I made the workaround to set the new $PATH directly in .xsessionrc (and also in .profile) and just execute F (now containing only the commands B) from .xsessionrc and .profile. This works fine, but is not really satisfactory, as I now some duplicate commands (A) in .xessionrc and .profile.

Is there some magic incanation that I have to do in order to source another file from .xsessionrc or are there any other obvious flaws in my thinking?

  • 1
    The setup should work for graphical login as well as login shells (e.g. ssh) login shells won't do anything with xsessionrc, seeing as you aren't starting X. – Rob Jan 25 '13 at 19:24
  • Right, that's why I also want to source the file from .profile for the login shells. – BinaryBongo Jan 25 '13 at 19:30
  • if it's all stuff like mounting, you can do it from .bash_profile or similar. It'll still get run before the xsession starts. No need to have it in multiple places. – Rob Jan 25 '13 at 19:32
  • I just did a test and put some commands into .bash_profile (yes, my shell is bash): they didn't get executed when logging in graphically. – BinaryBongo Jan 25 '13 at 19:58
  • Hmm... I wonder what user your xsession is starting from then. I forgot not everybody logs in then does startx / xinit. – Rob Jan 25 '13 at 19:59

There's no reason why sourcing a file, with the standard sourcing command, that is . not source (which is a cshism also supported by tcsh, bash, ksh and zsh but not standard) wouldn't work from .xsessionrc.

You have to make sure the syntax in the sourced file is standard sh syntax.


Why are you doing this in .xsessionrc and not e.g. in .bash_profile or whatever other file your shell uses on login (don't confuse with .bashrc, which is sourced by each shell)? Here (Fedora) it even has a helpful comment User specific environment and startup programs...

  • 1
    The commands should also be executed when no bash (or any other shell) gets executed. E.g. the remote directories should also be mounted when only starting a file manager – BinaryBongo Jan 25 '13 at 19:27
  • @BinaryBongo Many setups do read .profile when logging in with a display manager. It does depend on the distribution, display manager and session type, I haven't verified whether your does read .profile. If it doesn't, put . ~/.profile in your .xsessionrc. – Gilles Jan 25 '13 at 23:39

Depending on your distro, you can add scripts to run automatically on boot. If you're using initscripts, they'd be added to rc.local (/etc/rc.local IIRC) and with systemd I believe you'd make a service file for them and then enable it so it ran on boot.

With debian, it'd be initscripts and thus rc.local.

Mounting should be done with /etc/fstab which will run on boot.

  • This is not applicable here. The requirement is to run some code at session startup time, not at system boot time. – Gilles Jan 25 '13 at 23:37

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.