Take the 2-minute tour ×
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.

I know that .profile / .bash_profile are loaded when a terminal session is started, either through local machine or SSH. Are there any files that are loaded/called when the session terminates?

Reason: I have the .profile set to log the date and IP address that connects to a terminal session for a specific user.

share|improve this question
2  
You're aware of wtmp/last, right? –  derobert Dec 12 '12 at 17:52
    
Rahul's answer is a good and valid one but I think the only foolproof way to do this is via an independent process, ie, a daemon that monitors current logins perhaps via wtmp or w or whatever interface you can find available in whatever language you do that in. –  TAFKA 'goldilocks' Dec 12 '12 at 19:12
    
@derobert, as I stated in a comment below, I'm already getting the login info from the $SSH_CLIENT variable, I just wanted to know if there were any files that were executed when a session terminated. @goldilocks, I'm no where near experienced or versed enough with any language to do that yet. What I have now, thanks to Rahul, will be perfectly sufficient. –  Stephen S Dec 12 '12 at 19:40
    
@Morph It sounds like you're trying to build a log of user logins/logouts & IP address (at least for one specific user). That's what wtmp/last already is. That's why I want to be sure you're aware of it, as it sounds like you're reinventing the wheel. And if you need something slightly different, you may want to look at how it works (PAM) and see if you can just customize it. –  derobert Dec 12 '12 at 21:30
1  
@derobert, that would be an accurate description, however, it's more like reinventing it without knowing it already existed. –  Stephen S Dec 13 '12 at 3:49
add comment

2 Answers

up vote 1 down vote accepted

~/.bash_logout executed by bash when login shell exits. but you can also get IP address and date details using lastlog , did your try that ?

~/.bash_logout , but it will not run when session kill -9 $$ or may be close forcefully

share|improve this answer
    
There's no way around the kill -9, or stuff like "someone just cut the power" but a normal logout will not do that. +1 –  TAFKA 'goldilocks' Dec 12 '12 at 19:04
    
I was just googling around about this and also beware that closing an X terminal emulator sends SIGHUP to the shell, and ~/.bash_logout will not run :( So it is a SIGTERM only event (eg, you use exit). –  TAFKA 'goldilocks' Dec 12 '12 at 19:08
    
I'm using echo "${SSH_CLIENT%% *}" to get the IP address already. All connections are done through SSH to an offshore server. So cutting power isn't a problem. –  Stephen S Dec 12 '12 at 19:23
add comment

All shells have a way to run some code when the shell exits: a trap. “Trap” is the shell word for signal handler. You can set a trap for a signal by listing its name (INT, HUP, …) or its number, or use the special value 0 or EXIT for when the shell exits normally (by reaching the end of the input or if exit was called).

trap 'echo "$IP_ADDRESS logged out at $(date)" >>"$LOG_FILE"' EXIT HUP INT TERM

You can't trap kill -KILL or power failures.

I doubt that what you're doing is actually useful. This information is already recorded in the system logs. It's even more reliable there, because you get an entry for kill -KILL too. The command last shows the start date, end date and origin (terminal or IP address) for all past and current sessions since the last log rotation.

share|improve this answer
    
As I said before, I didn't know last existed. I'm not very versed in shell/bash. So like derobert said, it's like I was reinventing the wheel without knowing the wheel already existed. –  Stephen S Dec 13 '12 at 3:48
add comment

Your Answer

 
discard

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.