LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to execute my program when any user logs off my linux server ? (https://www.linuxquestions.org/questions/programming-9/how-to-execute-my-program-when-any-user-logs-off-my-linux-server-218417/)

rags2k 08-16-2004 03:16 PM

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

jlliagre 08-16-2004 03:46 PM

.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.

rags2k 08-16-2004 04:04 PM

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

bulliver 08-16-2004 06:07 PM

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
# This creates the temp file for a first-time run to avoid errors,
# else it just updates the timestamp
touch user-current.txt

# Just setting up some variables
# Also change 'current' to 'old'
mv user-current.txt user-old.txt
FILENEW=user-current.txt
FILEOLD=user-old.txt
OLDUSERS=$(cat ${FILEOLD})
USERS=$(who | awk {'print $1'})

# Check who is logged in, and write their username
# to a file for later reference
for i in $USERS
do
        echo "${i} is currently logged in"
        echo ${i} >> $FILENEW
done

# Compare who is currently logged in against
# who was logged in during the last run
for i in $OLDUSERS
do
        X=$(grep ${i} ${FILENEW})
        if [[ $X == "" ]]; then
                echo "${i} is no longer logged in"
                # This is where you launch your program
                # the logged out username can be passed
                # to your program as argv[1] ("$1" in bash)
                /path/to/your/program ${i}
        fi
done

# remove the old username file
rm ${FILEOLD}

So you would run this script as a cron job every 15 minutes (or more frequently if you prefer) and it will launch your program for every user that has logged out since the last check.

jlliagre 08-16-2004 06:20 PM

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.

bastard23 08-16-2004 06:49 PM

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

rags2k 08-17-2004 02:59 AM

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

masand 08-17-2004 01:11 PM

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

bastard23 08-17-2004 02:33 PM

rags2k,

From the UNIX Programming FAQ http://www.erlenstar.demon.co.uk/unix/faq_toc.html
Quote:

2.6 How do I find out if a file has been updated by another process?
====================================================================

This is close to being a Frequently Unanswered Question, because people
asking it are often looking for some notification from the system when a
file or directory is changed, and there is no portable way of getting this.
(IRIX has a non-standard facility for monitoring file accesses, but I've
never heard of it being available in any other flavour.)

In general, the best you can do is to use `fstat()' on the file. (Note: the
overhead on `fstat()' is quite low, usually much lower than the overhead of
`stat()'.) By watching the mtime and ctime of the file, you can detect when
it is modified, or deleted/linked/renamed. This is a bit kludgy, so you
might want to rethink *why* you want to do it.
I'm pretty sure FAM (which the above refers to the non-standard IRIX facility, but it has been open sourced) will help you with this, as long as you are willing to have it installed.

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.