I'm remotely connecting to a shared server and I would like to have a line of code executed automatically in the remote machine before I close the connection (e.g. by pressing Ctrl+D).
More specifically, I'd like to kill the SSH agent before I leave, as I noticed it keeps running even after I'm gone. The agent is started by another program, so I thought it'd be easier to just kill it before I leave rather than changing how it is started.
Is it possible to automatically run an arbitrary command -- like the one below -- before closing an SSH connection?
# user attempts to close connection # an arbitrary code or script runs eval "$(ssh-agent -k)" # connection is closed
Perhaps something similar to the
-t flag (
ssh -t firstname.lastname@example.org 'cd /some/path; bash -l'), but that runs before disconnecting rather than after connecting.