how to execute my program when any user logs off my linux server ?
Hello,
I am writing a program which collects user statistics on my linux system. I want to get this program automatically executed whenever any user logs out of my linux system. How can I do this? Can I call my program from any shell file which is automatically executed by the linux kernel whenever some body logs out? thanks and Warm Regards Raghu |
.bash_logout is executed when users log out.
It will work only for users having bash as their login shell though, and I'm not sure it is run if the shell is abruptly killed. |
how can i execute my program whenever any user logs off,( on any shell )
hello,
thanks for the reply.. but i do need to figure how i can execute my program whenever any user logs off my system.(assuming i am the root). the user can be using can shell. u mentioned it only for bash users...i need it for all users...with any shell ,.. any solutions please? thanks and regards Raghu |
Well, here's one way to do it. I'm not sure if this will be acceptable for you because it does not work in real-time, that is, it won't run your program immediately after someone logs out, rather, it writes currently logged in users to a file, then checks this against this file at a specified interval (ie: using cron)
Anywho, here's the code: Code:
#!/bin/bash |
bulliver, I think your script can be improved by detecting users whose session has been shorter that the sample time, by parsing the "last" command output, that gives you all the users login and logout time from the wtmp database.
|
Building on what jlliagre said, you can use the glibc http://www.gnu.org/software/libc/man...-Database.html functions to read the wtmp file. I'm sure other langs have this as well.
Then use FAM (probably included in your distro) or a fstat(2) loop to detect when the file gets updated. Remember that the file may rotated out, so you need to check for that. Good Luck, chris |
hello chris,
#thanks for the info ..i got the point wrt the checking for update of the file umtp/wtmp using fam or fstat. but i didnt quite understand what u meant by this - "Remember that the file may rotated out, so you need to check for that." r u talking abt user sessions which have been logged in and logged out inbetween the sample interval or is it something else u r talking abt here in the last sentence? thanks and regards Raghu |
hi
by rotating out he meant that since wtmp is a log file and it may have an entry in logrotate.conf so the file may rotate to different file,i.e to keep a check on the length of log files we use logrotate regards gaurav |
rags2k,
From the UNIX Programming FAQ http://www.erlenstar.demon.co.uk/unix/faq_toc.html Quote:
The reason you need it is as, masand said, log files get rotated. The usual process is to rename to file (wtmp -> wtmp.0), create a new file to be written to, and then update syslog (not done here as the programs write to wtmp themselves). You could also have the logrotate program send you a signal when the file gets updated, then make one last check of your open wtmp file, close it and open the new file. See your log rotating program for info on this. Hope this helps, chris |
All times are GMT -5. The time now is 05:33 PM. |