0

@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

  • Is the bundle command found when your in the root shell (before switching to user madison)? If so, a good starting point would be the outputs of type -a bundle and/or which bundle in that shell – steeldriver Feb 26 '23 at 11:52
  • I should have thought of that :) bundle isn't in root shell, but when in the mastodon user shell, type -a bundle shows bundle is /home/mastodon/.rbenv/shims/bundle. So I added export PATH="$HOME/.rbenv/shims/bundle:$PATH" to .profile but still get the error. – BlueDogRanch Feb 26 '23 at 15:56
  • PATH should contain directories containing commands, not commands themselves. If you're having trouble finding the right place to put it, one option is to set PATH="$PATH:$HOME/.rbenv/shims" inside the -c argument – steeldriver Feb 26 '23 at 16:10
  • Ok, using sudo 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:01
  • No that's not right either, you'd want sudo su -l mastodon -c 'PATH="$PATH:$HOME/.rbenv/shims"; cd live; ...' – steeldriver Feb 26 '23 at 17:06
  • That works! I didn't realize I now needed to add ; after each command with path inside the c command. Add that as an answer. – BlueDogRanch Feb 26 '23 at 17:19
  • You need some kind of separator - the newlines in as posted in your question are equivalent. – steeldriver Feb 26 '23 at 17:20

0 Answers0