Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file change notify 1

Status
Not open for further replies.

Bangieff

Technical User
Feb 12, 2001
52
BG
Hi all
I'm trying to make something like authomatized "who" program. It must be able to untherstand when a user is logging in or out so it needs to know innediatly if the utmp file is changed. How may I do it without making something like:
...
while (1)
{
u_entry=getutent();
....
}
...
?
thanks
 
Hi Bangieff,

You can easily do this with a shell program
running in the background.

Here is a program which might help you.

abp
:cool:

# watchfor : watch for people logging in
# and out
#/bin/sh
new=/tmp/who1.$$
old=/tmp/who2.$$
slt=60 # sleep time
> $old # create an empty file

while (1)
do
who | sort > $new
diff $old $new
mv $new $old
sleep $slt
done | awk '/>/ { $1 = "in: "; print;}
/</ { $1 = &quot;out: &quot;; print;}'

 
Some errors in the prev program ! :)

Replace 1 & 2nd line with
new=&quot;/tmp/who1.$$&quot;
old=&quot;/tmp/who2.$$&quot;

and the while loop should be

while :
instead of
while (1)

If you want to monitor more frequently slt
can be set to a smaller value.

Sorry for the errors :)

abp

 
thanks for this source but what I'm doing should be a C program. And it's almost done except I don't want to use any loops because of the CPU time waste. There should be some kind of function (may be a kind of signal???) that watch if the file has been changed.
 
I don't know such function.
You can set timer to run your check programm.

Or run your loop with sleep(1) (-in seconds) command or usleep(1000); -in microseconds (will work with ANSI C too)
while(bCheckFile)
{
sleep(1);
/*do your check*/
}
sleep() will stop your loop but will not use CPU.
For exemple loop with sleep(3); will check file each 3 seconds and will use CPU only to performe this check.
If you programm should do any other stuff run it in separate thread if not just do this. Any other possible function will do almost the same.
Regards.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top