Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm trying to run a command remotely with SSH, but my problem is that when I do that, my .basrhc doesn't get executed, and so the PATH is all wrong and my script is breaking.

What I'm doing is

ssh www.remotehost.com 'my_command'

I've also tried things like

ssh www.remotehost.com 'source ~/.bashrc; my_command' and similar variants, but nothing seems to work.

share|improve this question

2 Answers 2

SSH with a single, simple command argument will not start a shell, which means .bashrc does not get executed.

SSH with a compound command argument such as your second example should start a shell, but it will be a non-interactive one, so that won't run .bashrc by default. See Bash Startup Files in the Bash manual for more information.

However, calling 'source' in your second example command should work. Does your .bashrc have a line like if [ "$PS1" ]; then or if [ -z "$PS1" ]; then at the top? The sample bashrc does have one of these lines, and it prevents the script commands from running unless you are on an interactive shell. As mentioned above, SSH does not start an interactive shell by default when you pass a command argument. Try ssh -t www.remotehost.com 'source ~/.bashrc; my_command', as this will force TTY allocation and act like an interactive shell.

share|improve this answer

Zancheys Post gives you the right solution.

An other aproach could be to provide a simple shell script like following.

#!/bin/sh
# program name: /usr/local/bin/my_command_ssh
#
export PATH=/home/custom/bin:/usr/local/bin:/usr/bin:/bin
my_command

And now you can call it via ssh:

ssh -p22 user@www.remotehost.com '/usr/local/bin/my_command_ssh'
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.