Operating System - HP-UX
1833314 Members
2904 Online
110051 Solutions
New Discussion

Re: two processes is accessing the same file and i lose infos

 
federico_3
Honored Contributor

two processes is accessing the same file and i lose infos


hi to all,
i'm tryim=ng to send the stdout of a process to a file that periodically is accessed by another process ( command date to have a reference in the log file) that starts from cron. What is happening is that the date is lost because, for sure, there is not synchronization between two processes that is accessing the file.
How can i do to fixe the problem ?

Thanks
Federico
6 REPLIES 6
Vikas Khator
Honored Contributor

Re: two processes is accessing the same file and i lose infos

Hi ,

Since these are two different processes , one way of doing them is using a file ( could be just a 0 byte ) as a lock file .

Each time before they access the file they check for the existence of this lock file and if it exist then they sleep for secs and try again.

Once the process is done accessing the file it removes the lock file .
Keep it simple
Dan Hetzel
Honored Contributor

Re: two processes is accessing the same file and i lose infos

Hi Frederico,

Another solution would be to have your first process talk to the second via a pipe.

The second process should simply read the pipe and, if there is any data waiting, add a timestamp and write to a log file.
Depending of the amount and type of data logged by the first process, the second one could even be a shell script.

Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
federico_3
Honored Contributor

Re: two processes is accessing the same file and i lose infos


OK,
but what is the procedure?


Federico
RikTytgat
Honored Contributor

Re: two processes is accessing the same file and i lose infos

Hi,

Why don't you use logger and syslog.conf to create your own logfiles and write to them using the syslogd.

See the logger manpage for more info. A number of user definable syslog 'facilities' are available.

Bye,
Rik.
Dan Hetzel
Honored Contributor

Re: two processes is accessing the same file and i lose infos

Hi Frederico,

Here is a 'quick and dirty example to put you on the traks:

#!/usr/bin/sh
while true
do
read DATA
date >> logfile$$
print $DATA >> logfile$$
sleep 5
done

Adjust the sleep time to whatever value you find adequate.

Save this code into a script, make it executable and run it as:
your_prog | this_script &
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com
Dan Hetzel
Honored Contributor

Re: two processes is accessing the same file and i lose infos

Hi again,

I've hit the return key too quickly...

I like Rik's suggestion ;-)

Best regards,

Dan
Everybody knows at least one thing worth sharing -- mailto:dan.hetzel@wildcroft.com