LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 08-16-2004, 03:16 PM   #1
rags2k
LQ Newbie
 
Registered: Aug 2004
Posts: 7

Rep: Reputation: 0
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
 
Old 08-16-2004, 03:46 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,499

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
.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.
 
Old 08-16-2004, 04:04 PM   #3
rags2k
LQ Newbie
 
Registered: Aug 2004
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
Old 08-16-2004, 06:07 PM   #4
bulliver
Senior Member
 
Registered: Nov 2002
Location: Edmonton AB, Canada
Distribution: Gentoo x86_64; Gentoo PPC; FreeBSD; OS X 10.9.4
Posts: 3,743
Blog Entries: 4

Rep: Reputation: 76
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.

Last edited by bulliver; 08-16-2004 at 06:08 PM.
 
Old 08-16-2004, 06:20 PM   #5
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris10, Solaris 11, Mint, OL
Posts: 9,499

Rep: Reputation: 355Reputation: 355Reputation: 355Reputation: 355
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.
 
Old 08-16-2004, 06:49 PM   #6
bastard23
Member
 
Registered: Mar 2003
Distribution: Debian
Posts: 275

Rep: Reputation: 30
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
 
Old 08-17-2004, 02:59 AM   #7
rags2k
LQ Newbie
 
Registered: Aug 2004
Posts: 7

Original Poster
Rep: Reputation: 0
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
 
Old 08-17-2004, 01:11 PM   #8
masand
Guru
 
Registered: May 2003
Location: INDIA
Distribution: Ubuntu, Solaris,CentOS
Posts: 5,522

Rep: Reputation: 58
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
 
Old 08-17-2004, 02:33 PM   #9
bastard23
Member
 
Registered: Mar 2003
Distribution: Debian
Posts: 275

Rep: Reputation: 30
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Give user rights to execute program max2004 Linux - Newbie 7 11-30-2005 07:17 PM
run program after user logs in kinasz Linux - General 2 09-22-2004 03:47 AM
how can execute some commands when a user logs out rddreamz Programming 2 05-25-2004 03:00 PM
Is it possible to make a Linux server execute a program on a WinXP system? Cichlasoma Linux - General 4 04-23-2004 06:00 AM
Program runs when a user logs in mindstormsguy Linux - Software 2 03-31-2004 05:01 PM


All times are GMT -5. The time now is 01:31 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration