Search code examples
linuxruby-on-rails-4pipeexim

Pipe to program fails, but runs OK in SSH console


I'm trying to get Rails 4.1 to receive bounceback emails but it's been really difficult to even get to this point. I can run the command below in an SSH console when logged in as root, but when I put it in my /etc/valiases file, I get a bounceback from the script saying "the following addresses failed".

runuser -l useraccount -c "cd /home/useraccount/rails_deployments/dev.www/current/bin && rails runner -e development 'EBlast.receive(STDIN.read)'"

/etc/valiases/dev.mydomain.com

[email protected]: "|runuser -l useraccount -c "cd /home/useraccount/rails_deployments/dev.www/current/bin && rails runner -e development 'EBlast.receive(STDIN.read)'""

I've also tried escaping the double-quotes to no avail.

I need to run as useraccount because the RVM environment variables don't exist for root. Running the 1st command in an SSH console when logged in as root works, but not when exim receives an email.


Solution

  • There are a few things I had to do to work around the problem:

    1) Move the runner script into its own file as Todd suggested; nested quotes were causing the script to fail to run.

    2) Make the file executable; the permissions were already set to 755.

    3) Even though exim was using my username to execute the script, the environment variables such as PATH and HOME were not set at all! This caused ruby to be an unknown command. This caused many other issues because most of the app relies upon RVM and its gemsets. So I couldn't get ruby to run, much less rails. Even if I were to explicitly call the ruby wrapper, spring would break because $HOME wasn't set. Just a cascade of issues because the user environment wasn't being set. I also couldn't just issue su - username -c 'whatever' because the account that exim was using didn't have authority to use su.


    So the working setup looks like this:

    /etc/valiases/dev.mydomain.com

    [email protected]: "|/bin/bash -l -c '/home/useraccount/rails_deployments/dev.www/current/script/receive_eblast_bounce'"
    *: ":fail: No Such User Here"
    

    /home/useraccount/rails_deployments/dev.www/current/script/receive_eblast_bounce

    D=`pwd`
    
    HOME=/home/useraccount
    
    if [ -f /etc/bashrc ]; then
        . /etc/bashrc
    fi
    
    if [[ -s "/home/useraccount/.rvm/scripts/rvm" ]] ; then
      source "/home/useraccount/.rvm/scripts/rvm"
    fi
    
    cd /home/useraccount/rails_deployments/dev.www/current
    ./bin/rails runner -e development 'EBlast.receive(STDIN.read)'
    
    cd $D
    

    I'm now having problems with ActionMailer using SSL when it shouldn't, and I don't know if that's related to something I did here, but at least it executes the rails script.