event number tracking


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting event number tracking
# 1  
Old 01-09-2006
event number tracking

Having a bit of trouble trying to sort this one out so any advice is appreciated.
I have several scripts running in cron monitoring different events on different servers. These scripts log information events and critical events to a central log file.

I wanted to add an event number to every logged event in the file so I have a file with a number in it to start - say 000000

Then in every script I have the following when I log any event:
curevent=`tail -1 /logs/eventnums | cut -f1 -d" "`
eventnum=$(($curevent + 1))
echo "$eventnum event info" >>/logs/central_events.log
echo $eventnum > /logs/eventnums

/logs/eventnums is where I keep the latest event number and its updated by the cron scripts everytime they take a number...
Problem is that some of the scripts have to run at the same time in cron and so eventually they take the same eventnum each and update the eventnums file at the same time so I end up with events with the same number which is no good..

Any suggestions on a better way to do this?
# 2  
Old 01-09-2006
Use a lock file. Like this:
Code:
while [ -f /logs/lockfile ]; do
   sleep 2
done
touch /logs/lockfile
curevent=`tail -1 /logs/eventnums | cut -f1 -d" "`
eventnum=$(($curevent + 1))
echo "$eventnum event info" >>/logs/central_events.log
echo $eventnum > /logs/eventnums
rm /logs/lockfile

Before we do anything, we can check for existence of the lock file and loop while it exists. Then when we get the all clear, we touch a lock file and then get to work. Once the job is done, we remove the lock file and let other cron jobs get on with it.

--This is not tested--
# 3  
Old 01-09-2006
Yes - I did think of using a lock mechanism.. but had not tried it as some of these scripts work at the same second and I wondered whether it would work it both scripts see no lock - both touch a lock file and then continue to use the event number...

Any thoughts?
# 4  
Old 01-09-2006
I remember seeing some thread similiar to the one you mentioned.

See if this helps

https://www.unix.com/shell-programming-and-scripting/19781-how-check-if-another-instance-process-running-post76456.html
# 5  
Old 01-09-2006
Quote:
Originally Posted by blowtorch
Use a lock file. Like this:
Code:
while [ -f /logs/lockfile ]; do
   sleep 2
done
touch /logs/lockfile
curevent=`tail -1 /logs/eventnums | cut -f1 -d" "`
eventnum=$(($curevent + 1))
echo "$eventnum event info" >>/logs/central_events.log
echo $eventnum > /logs/eventnums
rm /logs/lockfile

Before we do anything, we can check for existence of the lock file and loop while it exists. Then when we get the all clear, we touch a lock file and then get to work. Once the job is done, we remove the lock file and let other cron jobs get on with it.

--This is not tested--

torch,

if we are using a separate lock file to maintain integrity there is always a problem that it could be corrupted or accidentally deleted during processing.

here is a very similiary approach to yours, to make use of the existing file and work around with the permission bits

Code:
#check whether the file with sequence number is denied of read-access
while [ ! -r <file_with_seqno> ]
do
sleep 1
done

curevent=`tail -1 /logs/eventnums | cut -f1 -d" "`

#revert the read permission to maintain integrity
chmod -r <file_with_seqno>

eventnum=$(($curevent + 1))
echo "$eventnum event info" >>/logs/central_events.log
echo $eventnum > /logs/eventnums

#reset the access permission as before
chmod +r <file_with_seqno>

Login or Register to Ask a Question

Previous Thread | Next Thread

3 More Discussions You Might Find Interesting

1. Programming

Methods for Random Number Tracking

what types of methods and algorithms are used to track random number generators? I'm working on computer security, and I want to know all the different ways to track random number generators so I know what to counter (8 Replies)
Discussion started by: azar.zorn
8 Replies

2. UNIX for Advanced & Expert Users

Tracking down the problem

Is there a way to track down what process is sending to a certain port? I have some thing pounding the network with requests to a multicast IP that doesn't exist. I have shut down all comms related processes and yet it is still there. Need a way to track the port or IP back to the process. Thanks... (3 Replies)
Discussion started by: mattmanuel
3 Replies

3. UNIX for Advanced & Expert Users

Command Tracking

Hi, OS: Solaris9, SPARC Is there any way I can track the commands run by users from the shell prompt? Example: Somebody is deleting files from the system. Who it is is a mystery. That person obviously does not use bash prompt so there is no history. Is there anyway I can find out who... (5 Replies)
Discussion started by: mahatma
5 Replies
Login or Register to Ask a Question