@steeldriver's answer to In a bash script, how do I change from root user to another user using su and then exit? works great for using sudo and su in a bash script, but a new issue has to do with the Rails path in the .profile file.
I'm running Mastodon and am trying to use a shell script to run all the different Rails commands and restart mastodon after changing files. Running all of the commands manually outside of the script works fine.
But in the script, the three Rails commands all throw the error bundle: command not found
, even while the rest of the shell script runs and completes:
#!/bin/bash
sudo su -l mastodon -c '
cd live
RAILS_ENV=production bundle exec rake tmp:cache:clear
RAILS_ENV=production bundle exec rails assets:generate_static_pages
RAILS_ENV=production bundle exec rails assets:precompile
exit
'
systemctl restart mastodon-*
I've added export RAILS_ENV=production
to ~/.profile with no luck.
What is the correct path to export for Rails so that the commands will execute?
This could be related to this: "Mastodon default settings not sensibly usable, mastodon not a login user" https://github.com/NixOS/nixpkgs/issues/199029
Or is this a different issue?
~/.profile:
# ~/.profile: executed by Bourne-compatible login shells.
if [ "$BASH" ]; then
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
fi
mesg n 2> /dev/null || true
export RAILS_ENV=production
bundle
command found when your in theroot
shell (before switching to usermadison
)? If so, a good starting point would be the outputs oftype -a bundle
and/orwhich bundle
in that shell – steeldriver Feb 26 '23 at 11:52type -a bundle
showsbundle is /home/mastodon/.rbenv/shims/bundle
. So I addedexport PATH="$HOME/.rbenv/shims/bundle:$PATH"
to .profile but still get the error. – BlueDogRanch Feb 26 '23 at 15:56PATH
should contain directories containing commands, not commands themselves. If you're having trouble finding the right place to put it, one option is to setPATH="$PATH:$HOME/.rbenv/shims"
inside the-c
argument – steeldriver Feb 26 '23 at 16:10sudo su -l mastodon -c PATH="$PATH:$HOME/.rbenv/shims" ' cd live ...
doesn't throw the error, but it blows by and doesn't execute the rails commands. – BlueDogRanch Feb 26 '23 at 17:01sudo su -l mastodon -c 'PATH="$PATH:$HOME/.rbenv/shims"; cd live; ...'
– steeldriver Feb 26 '23 at 17:06;
after each command with path inside the c command. Add that as an answer. – BlueDogRanch Feb 26 '23 at 17:19