Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I know that I can run a local command when I establish a ssh connection, something which is quite useful for setting up terminal title for example.

Still, I'm looking for a solution to run a command when the ssh connection is closed.

Usage:

>ls
>ssh server2
# want to run something here on the client, this can be done with .ssh/config ProxyCommand
>ls
>exit
# I want to run something on the client, how!?
>ls
share|improve this question

2 Answers 2

According to Dill on Ubuntuforums you can implement a logout script like so:

If you don't already have one, create a .bash_logout file:

http://rcsg-gsir.imsb-dsgi.nrc-cnrc.gc.ca/documents/advanced/node125.html

Here are the contents of my default .bash_logout on an Ubuntu machine and Debian server:

# ~/.bash_logout: executed by bash(1) when login shell exits.

# when leaving the console clear the screen to increase privacy

if [ "$SHLVL" = 1 ]; then
[ -x /usr/bin/clear_console ] && /usr/bin/clear_console -q
fi  

Feedback seems good to his answer. Link for reference

share|improve this answer
    
Correct me if I'm wrong: is is going to run on the local machine? What if I use sh or something else than bash, when I initiate the ssh. Nothing that I have to put on the remote machine would be acceptable, it has to be a local only solution. –  sorin Apr 8 '12 at 18:37
    
Can you clarify what you're trying to do in the question? Do you want it to run on the server or the client? Can you funnel it through bash by running a shell script with #!/bin/bash(even if you normally use /bin/sh)? –  hbdgaf Apr 8 '12 at 18:43

If you want the command to continue running after you close the SSH connnection, use the nohup command - see http://superuser.com/questions/143925/what-happens-to-running-processes-when-i-lose-a-remote-connection-to-a-nix-box

share|improve this answer

Your Answer

 
discard

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

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