-1

so I have a alias for hadoop which I defined in bash_aliases which bashrc touches upon I guess.

alias hadoop=/usr/local/hadoop-2.7.2/bin/hadoop

Now the alias works fine when I directly try to run it. But when I try this

ssh aditya@localhost hadoop

I get the following error message

bash: hadoop: command not found

I tried the following too

shopt -s expand_aliases

But that didn't work. Could anyone help me out.

Update:

This question is different from mine, because the ssh command is not generated by me, so the solution to that question does not solve my question. The ssh command is generated and executed by a script inside a library I am using. I do not prefer to modify that script to change the command as I am not completely aware of how that script works.

Aditya
  • 117
  • 6

1 Answers1

1

From man ssh

If command is specified, it is executed on the remote host instead of a login shell.

That implies that shell will run your command as sh -c. That's first of all - this runs in dash, which is default shell and symlinked to /bin/sh. So your alias for bash won't work in the first place.

Second of all, sh -c is non-interactive shell. That means it won't source your ~/.bashrc if you have defined alias there.

But main reason why your command does not work is because you run

alias hadoop=/usr/local/hadoop-2.7.2/bin/hadoop

in the current shell session. It is not exported, nor it is in ~/.bashrc to be sourced to begin with


Here's an alternative. Make alias in your ~/.bashrc:

alias run_hadoop='ssh aditya@localhost /usr/local/hadoop-2.7.2/bin/hadoop'

That way it will be way easier , than defining alias for just command and failing over and over to run it. Alternatively , make a wrapper script run_hadoop.sh in your home directory:

#!/bin/bash
exec /usr/local/hadoop-2.7.2/bin/hadoop

Make sure it is executable with chmod +x run_hadoop.sh. After that you can do

ssh aditya@localhost 'bash /home/user/run_hadoop.sh'
Anwar
  • 76,649
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • Hey, read the update I wrote on my question. Your solution does not work because of that. – Aditya Aug 27 '16 at 04:54
  • @Aditya Well, there's not really much one can do about aliases. You will have to modify your ssh command to source the ~/.bash_aliases , ssh aditya@localhost 'bash -c "source ~/.bashg_aliases; hadoop"'. Sorry, but there's really no way without modifying. – Sergiy Kolodyazhnyy Aug 27 '16 at 05:02
  • Hadoop doesn't need to be an alias. I can make it whatever I want. I can try the path option too, but I am not sure how to do it. I tried path=$path:/usr/local/hadoop-2.7.2/bin like he suggested but that didn't work. Someone in IRC told me to try functions but that didn't work either. – Aditya Aug 27 '16 at 05:08
  • PATH has to be capital. If you are setting it in ~/.bashrc, like i said, that won't work because it's non interactive shell. PATH is only sourced in interactive shells. – Sergiy Kolodyazhnyy Aug 27 '16 at 05:12