home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / unix / shell / 3135 < prev    next >
Encoding:
Internet Message Format  |  1992-07-25  |  1.6 KB

  1. Xref: sparky comp.unix.shell:3135 comp.unix.programmer:3915
  2. Newsgroups: comp.unix.shell,comp.unix.programmer
  3. Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!uchinews!machine!chinet!les
  4. From: les@chinet.chi.il.us (Leslie Mikesell)
  5. Subject: Re: file locking in shell scripts
  6. Message-ID: <1992Jul25.213443.1900@chinet.chi.il.us>
  7. Date: Sat, 25 Jul 1992 21:34:43 GMT
  8. References: <1992Jul23.124327.20843@cv.ruu.nl>
  9. Organization: Chinet - Public Access UNIX
  10. Lines: 20
  11.  
  12. In article <1992Jul23.124327.20843@cv.ruu.nl> rvloon@cv.ruu.nl (Ronald van Loon) writes:
  13. >Somewhere on our system is a log-file. This logfile is used to gather
  14. >statistical information. However, it is possible that people write to the file
  15. >at the same time, thus resulting in either the merge of the input-streams or
  16. >total garbage in the file.
  17.  
  18. >How can I - within a shell script - lock the file exclusive for writing ? If
  19. >that's not possible, what do I need to use to do so from C, for example ?
  20.  
  21. On unix systems it is pretty common to just ignore this issue for logfiles
  22. where the messages are short. If you open a file in append mode, every
  23. write() is positioned at the current end-of-file, so messages that
  24. are generated in a single write() will be kept intact.  It can be
  25. tricky to get a shell script to output a message in a single write()
  26. but accumulating the string into a variable with an "echo "$VAR" >>file"
  27. to dump it should work.  As long as the other users are using append
  28. mode (and you aren't going over NFS) the messages should stay intact.
  29.  
  30. Les Mikesell
  31.   les@chinet.chi.il.us
  32.