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:
~/.xsessionrc
#!/bin/bash
. /home/pi/.rvm/scripts/rvm
cd /home/pi/base/ && rails s -b 10.3.141.1 -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
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:
#!/bin/bash
cd [FULL_PATH_TO_DIRECTORY] && [PATH_TO_RAILS] s -b 10.3.141.1 -p 3000 -d &
Where:
- [PATH_TO_RAILS] = /home/pi/.rvm/gems/ruby-2.5.3/wrappers/rails
- [FULL_PATH_TO_DIRECTORY] = /home/pi/...
-b 10.3.141.1
- 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
Explore related questions
See similar questions with these tags.
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/bin/
to/wrappers/
in the path given bywhich rails
as per the accepted answer here