I'm trying to automate installing Erl using shell script. Here's my shell script-

unzip OTP-18.0-rc1.zip
cd otp-OTP-18.0-rc1

export ERL_TOP=`pwd`
./otp_build autoconf
echo $ERL_TOP
echo "################### MAKE ###################"
make -j8 && make install

echo "Creating soft links......"
ln -snf /home/user/erlang/otp-OTP-18.0-rc1/bin/erl /usr/bin/erl
ln -snf /home/user/erlang/otp-OTP-18.0-rc1/bin/erlc /usr/bin/erlc

# Install other packages

I'm trying to execute this script on a remote machine this way-

ssh root@host_ip 'bash -s' < my_script.sh 

However the script exits after the make command, without creating the soft links. I've verified that there's no error during install process. Also the script works perfectly fine when executed locally on that same system.

Interestingly, if I modify the script to put make command on two separate lines like this-

make -j8
make install

The script exits without doing make install

What am I missing here?

  • 2
    Have you checked to see that make -j8 returns an exit code of 0? – saiarcot895 Oct 20 '15 at 13:45
  • 1
    might be worth adding one or more -v flags to your ssh to see if it's getting a signal, or exiting for some other reason – Jeff Schaller Oct 20 '15 at 13:52
  • @saiarcot895 No I haven't. I guess that could be a reason.How can check that? And if it's returning exit code of 0, how can i make sure my script execution is not interrupted. Thanks. – pragmatic Oct 20 '15 at 13:58
  • 1
    Watch what happens by adding the -x flag to bash like this - ssh root@host_ip 'bash -xs' < my_script.sh. Another possibility is that the make -j8 command has a dependency on the terminal (use ssh -t ...) – roaima Oct 20 '15 at 14:00

The problem is you can have only 1 stdin, and so if your bash script runs a command that reads from stdin at all, it will gobble up lines from the script.

Try resetting stdin for the make commands, eg:

( make -j8 && make install ) </dev/null
  • This seems to be working. However I'm curious why i didn't faced the same issue while installing Openssl on my machine, since even there i was doing the same thing make followed by make install ? – pragmatic Oct 20 '15 at 14:58
  • A makefile runs arbitrary shell commands; it will read from stdin only if those commands do. What exactly the makefile runs will make all the difference. – Tom Hunt Oct 20 '15 at 15:11

meuh's answer works for you, but here's another option:

scp my_script.sh root@host_ip:/tmp/
ssh root@host_ip bash /tmp/my_script.sh
  • Thanks. Do you mind explaining what changes if we run the script after copying on the remote machine. – pragmatic Oct 20 '15 at 16:51
  • 1
    The problem was caused by redirection of stdin. by copying and running the script, there is no redirection of stdin. – cas Oct 20 '15 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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