Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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'm logging in to a system via SSH and I'd like to be able to force a command to be run on logout, whether that's an intentional logout (exiting the shell) or because my SSH connection has dropped/been terminated. Is there any way to set this up? My best guess right now is to change the login shell to a program that intercepts the signal sent on SSH closing (SIGHUP?) and executes the command then, but I'm wondering if there's a cleaner solution.

share|improve this question
Do you want to execute the command on the remote-machine or on the one you initialized the connection from? – xx4h Jun 11 '14 at 5:50
up vote 11 down vote accepted

The best way to do this would be to use pam.

In /etc/pam.d you will have several files, one of them will be called sshd. If you only want to affect ssh, and not other logins (such as a GUI, or real TTY), you want this file.
If you want to affect all logins, you'll want a 'common' file. The name of this other 'common' file varies by distro, but you can track it down by following the include and substack statements in the sshd file until you get to the base file.

Once you've identified the file in /etc/pam.d that you want to use, add a line such as the following to the session section:

session     optional    pam_exec.so quiet /etc/pam_session.sh

This will result in calling /etc/pam_session.sh every time someone logs in and logs out (whether gracefully or ungracefully).

Now you just need to create /etc/pam_session.sh. Below is an example you could use to run something every time someone logs out:

if [ "$PAM_TYPE" = "close_session" ]; then

(don't forget to chmod a+x the script)

share|improve this answer
This is a great approach, thanks! – David Jun 11 '14 at 6:03

I would try to do it with .bash_logout on the remote system.
This will only work if you logout/disconnect normally invoked by you.
If e.g. your connections is killed by somewhat or someone,
there is no execution of .bash_logout.

share|improve this answer
This answer only works for a very small subset of situations. – Shadur Jun 11 '14 at 8:53
But it works and it is quick&easy. And for somebody who just wants to execute something for a specific user on logout it is a nice working solution without big effort. – xx4h Jun 11 '14 at 9:08
Except that it only works if someone 1) uses bash; 2) doesn't change their .bash_logout and 3) always logs off gracefully rather than killing their SSH connection. – Shadur Jun 11 '14 at 11:14
Except that this is the case in probably 90% of all setups... – xx4h Jun 11 '14 at 11:17

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.