Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


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

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:

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

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)? – RobotHumans 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

share|improve this answer

Your Answer


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.