How can i TRAP a user Logout action?

When the temp user logs in i see his session as below.

[root@techx ~]# who
root     pts/0        2017-08-18 08:32 (121.87.51.113)
temp     pts/1        2017-08-18 09:06 (121.87.51.113)
root     pts/2        2017-08-18 08:59 (121.87.51.113)

When he logs out by either firing exit command or closing the putty window or clicking disconnect on mRemote i see temp user session destroyed as below.

[root@techx ~]# who
root     pts/0        2017-08-18 08:32 (121.87.51.113)
root     pts/2        2017-08-18 08:59 (121.87.51.113)

This is what i m doing catch a user logout action.

more .bash_logout
# ~/.bash_logout
rm -f flag_login

and in the

more .bash_profile
#!/bin/bash
if [ -f flag_login ]
then
echo "You have switched your working SHELL"
else
touch flag_login
fi

This works fine when the user types exit command to logout of the session.

But when he logs out by closing the putty window or disconnect mRemote session to my L\Unix server the user session terminates but ~/.bash_logout is not triggered and thus flag_login file remains denoting the user is still on.

I do have the root privileges.

Can you please let me know how can i catch putty window close or mRemote disconnect the same way as i am catching the exit command to work ?

Try adding:

trap 'source ~/.bash_logout' exit

in your .bash_profile file. This will force the .bash_logout file to be sourced every time you exit.

Andrew

1 Like

Thank you. After implementing your suggestion it works no matter how you close the session.:slight_smile:

I bet it wouldn't work if someone exec kill -9ed their own shell.

1 Like