rluisr
rluisr

Reputation: 351

How I can execute to script when ssh sessions is exited?

.ssh/rc can execute script when user logged in.
But when user logged out with ssh, how it execute script?

Upvotes: 0

Views: 128

Answers (1)

John Slotsky
John Slotsky

Reputation: 171

You can add a trap to execute your script when the user logs out:

trap 'bash /path/to/yourscript.sh; exit' 0

If the shell is bash then any code you put in the ~/.bash_logout file will be executed on logout. For csh, the file is .logout.

Another option would be to run a daemon like this:

setsid mydaemon.sh >/dev/null 2>&1 < /dev/null &

Which can execute your script at any time after the user has logged out.

Upvotes: 1

Related Questions