I've spent most of the day trying to launch two commands after login of the GUI on Raspberry OS. I am trying to run a Ruby on Rails server after logging into the GUI (not on boot). The commands I would like to run are:


. /home/pi/.rvm/scripts/rvm
cd /home/pi/base/ && rails s -b -p 3000 -d &

I have been trying to do this using .profile, .bash_profile, .bashrc, .xsession, .xinitrc but it's proving impossible and I haven't got further than breaking my installation. I've also had a go at the .desktop and autostart options and followed various articles and Q&As online. Also tried saving it into a separate script and running that, they all work as I expect from the command line when triggered but the server isn't running automatically after GUI login. The closest I've got is the server running automatically once I start a shell from within the GUI.

I think I should be editing the xsession files as these are run by lightdm after logging in via the GUI? But when I do the GUI breaks and just returns me to the login password screen after each attempt.

Despite a lot of time, I don't seem much closer to the solution - from experience this normally means I've misunderstood something fundamental!

Any help would be amazing

Many thanks

  • Can you do this manually? If so what commands do you run and when do you run those commands?
    – joan
    Jan 1, 2021 at 21:59
  • I can run the two commands in my question manually, or source ~/.xsessionrc, in both cases the rails server will start as expected. I get the following error in ~/.xsession-errors: /etc/X11/Xsession: 3: /home/pi/.xsessionrc: rails: not found. I have updated my question so that it reflects my .xsessionrc file Jan 1, 2021 at 22:24
  • 2
    Do "which rails" and replace && rails with the result.
    – joan
    Jan 1, 2021 at 22:43
  • You're amazing! Finally got there. I also had to switch /bin/ to /wrappers/ in the path given by which rails as per the accepted answer here Jan 1, 2021 at 23:08
  • 2
    It would be useful if you answer your question as a guide for others who have a similar problems.
    – joan
    Jan 2, 2021 at 8:55

1 Answer 1


With Joan's help the solution is to refer to the absolute path of rails rather than rely on rvm.

which rails - provides an absolute path, in my case /home/pi/.rvm/gems/ruby-2.5.3/bin/rails. You then need to replace bin with wrappers as per this answer.

You can add your command to the file .xsessionrc in your home directory, this is run after logging into the GUI. This can be created if it does not exist. The command below will open up a text editor (nano) and create it as required:

nano ~/.xsessionrc

The contents of the file should be:

cd [FULL_PATH_TO_DIRECTORY] && [PATH_TO_RAILS] s -b -p 3000 -d &


  • [PATH_TO_RAILS] = /home/pi/.rvm/gems/ruby-2.5.3/wrappers/rails
  • [FULL_PATH_TO_DIRECTORY] = /home/pi/...
  • -b - binds the server to specific ip address, this is optional.
  • -p 3000 - designates the port that rails should use, again optional
  • -d - runs the server as a daemon, this is important if you're running it in the background
  • & - ensures that the process of booting up continues, and they do not wait for the server to end it's process

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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