Did you know LQ has a Linux Hardware Compatibility List?
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 11-04-2007, 04:49 PM   #1
LQ Newbie
Registered: Jun 2006
Distribution: Ubuntu 7.10
Posts: 15

Rep: Reputation: 0
Help with script execution in bashrc


I am running Ubuntu 7.10 and yesterday i added a script in my .bashrc file.

I want this script to be executed only when i login via SSH to my account. I have tested the script execution and it works.

Unfortunately though, the script gets executed also when i locally open a terminal window.

Is there a way i can make this script executed ONLY when i login via SSH?

Thank you in advance.
Old 11-04-2007, 07:53 PM   #2
LQ Newbie
Registered: May 2005
Location: Romania
Distribution: Gentoo
Posts: 8

Rep: Reputation: 0
Hi there,

All the scripts in .bashrc will be automatically executed with each new terminal.

The trick is to use an alias ssh_new_script='whatever_path_to_real_script', and in this way you can call only the alias.

Have fun!
Old 11-05-2007, 12:41 AM   #3
Registered: Oct 2007
Distribution: rhel, fedora, gentoo, ubuntu, freebsd
Posts: 104

Rep: Reputation: 15
Basically, you have two possibilities.

Option 1: add the script invocation to .bash_profile, and your other stuff in .bashrc (with .bash_profile sourcing .bashrc, assuming you've got something you want in there too). .bash_profile is sourced on login sessions, .bashrc on interactive non-login sessions. But this would also get you console logins and screen session starts, so may not be ideal.

Option 2: add it to .bash_profile or .bashrc with a conditional
[[ -n $SSH_CLIENT ]] && /path/to/
(which checks if SSH_CLIENT is a nonzero-length string, and if it is executes the script). This takes advantage of the fact that ssh sessions set a couple variables that interactive logins don't, but the downside is that those variables propagate to child processes, so descendants wouldn't be able to tell.

Combining the two approaches and a little more gives us a complete solution though.
In .bash_profile (or, really, .bashrc would work at this point too):
[[ -n SSH_CLIENT && -z $SSH_IS_CONNECTED ]] && export SSH_IS_CONNECTED=1 && /path/to/script
This checks if the SSH variables are defined and the SSH_CONNECTED variable isn't. If that's the case, then it sets it, and executes the script. The nonzero SSH_CONNECTED variable propagates with the other ssh vars, so that no longer evaluates true even if you source .bash_login again (eg: invoking screen).

Last edited by complich8; 11-05-2007 at 12:42 AM.
Old 11-07-2007, 03:28 AM   #4
LQ Newbie
Registered: Jun 2006
Distribution: Ubuntu 7.10
Posts: 15

Original Poster
Rep: Reputation: 0
Hi complich8,

thanks for the prompt reply.

I have used
[[ -n SSH_CLIENT && -z $SSH_IS_CONNECTED ]] && export SSH_IS_CONNECTED=1 && /etc/
in /etc/bash.bashrc file and it still executes the when i open a terminal window.

When i open a terminal window though, as you correctly said above, the $SSH_IS_CONNECTED env. variable is not defined. When i connect via SSH then it is.

Any suggestions?

Thanks in advance.
Old 11-07-2007, 03:42 AM   #5
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 657Reputation: 657Reputation: 657Reputation: 657Reputation: 657Reputation: 657
Put your commands in ~/.ssh/rc.
Commands in this file are executed by ssh when the user logs in,
just before the user's shell (or command) is started. See the
sshd(8) manual page for more information.

Last edited by jschiwal; 11-07-2007 at 07:33 AM.


bashrc, execute, script, ssh

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
script execution yakoub Linux - Software 3 11-12-2006 01:26 AM
Script execution remotely immortaltechnique Red Hat 4 11-09-2006 05:08 AM
Script execution salmanucit Linux - Software 1 12-08-2005 08:51 AM
script execution Johng Programming 2 08-21-2004 04:58 PM
auto execution script nimrod Linux - General 2 12-09-2002 03:20 PM

All times are GMT -5. The time now is 07:06 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration