The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how can i replace "(", "\" "+" in a file mail2sant Shell Programming and Scripting 3 04-22-2008 08:10 PM
Development Releases: Linux Mint 4.0 Beta "Fluxbox", 4.0 Alpha "Debian" iBot UNIX and Linux RSS News 0 01-04-2008 12:00 PM
Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`" Lokesha UNIX for Dummies Questions & Answers 4 12-19-2007 10:52 PM
"sed" to check file size & echo " " to destination file jockey007 Shell Programming and Scripting 6 11-13-2007 06:00 PM
No utpmx entry: you must exec "login" from lowest level "shell" peterpan UNIX for Dummies Questions & Answers 0 01-18-2006 01:15 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 04-25-2007
Registered User
 

Join Date: Jan 2007
Posts: 13
"vi"-ing a constantly updated file

Hi,

A bit of a dumb question here. Sorry folks.

Suppose I have this file which is being updated at irregular intervals (a few seconds, a few minutes, who knows) by a background process.

I 'vi' this file with the intention of editing it, so it opens up a temp copy as the usual /tmp/720cwhatever

Now suppose, whilst I have this file opened, its updated several times by the background process.

I make my changes, save-quit.

What happens to the updates that were made by the background process during the time I had the file opened?


How do I mitigate this?
(unfortunately, stopping the background process is not an option).


The file is fairly big also (about 40Mb) so it takes vi a few seconds to load it up, save to it etc.....


Unix is ksh.
OS is SunOS.

Hope you guys can help! - thanks in advance!
Reply With Quote
Forum Sponsor
  #2  
Old 04-25-2007
sysgate's Avatar
Unix based
 

Join Date: Nov 2006
Location: /root
Posts: 1,200
The updates made by background process are not lost, the question is what happens when you are about to do "save & quit" - you will be prompted with message that the original file has changed : do you want to save anyway ?
The other thing is when someone else is trying to vi the same file opened by you - he will get a message :

Quote:
E325: ATTENTION
Found a swap file by the name ".temp.txt.swp"
owned by: sysgate dated: Wed Apr 25 17:29:23 2007
file name: ~sysgate/temp.txt
modified: no
user name: sysgate host name: $myhost
process ID: 30240 (still running)
While opening file "temp.txt"
dated: Wed Apr 25 17:27:07 2007

(1) Another program may be editing the same file.
If this is the case, be careful not to end up with two
different instances of the same file when making changes.
Quit, or continue with caution.

(2) An edit session for this file crashed.
If this is the case, use ":recover" or "vim -r temp.txt"
to recover the changes (see ":help recovery").
If you did this already, delete the swap file ".temp.txt.swp"
to avoid this message.

Swap file ".temp.txt.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:
but, I suppose you mean large log files, so the solution will vary depending on what exactly you want to do.
Reply With Quote
  #3  
Old 04-25-2007
Shell_Life's Avatar
Registered User
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Percy, as sysgate noted, your solution will heavily depend on what exactly
you want to update in the file.
Is it really necessary to "vi" the file?
Could a shell be written to do the updates you want?
Reply With Quote
  #4  
Old 04-25-2007
Registered User
 

Join Date: Jan 2007
Posts: 13
OK cheers guys!

A bit more info:

What exactly I want to do is this:

This is a big file, full of filenames and a hashcode for each filename.
each line in the file is
filename, date, hashcode
filename, date, hashcode
...

Anyway, so a file comes into a server, gets a hashcode which is then put into this big log file of mine, and then the file is processed.

Should the file be sent in again to the server, its hashcode is checked and if it exists (ie, my process has dealt with it already) it deals with the file appropriately.

I come unstuck when I need to reprocess the same file again - so I need to edit the large log file and remove the entry (the whole line - name,date,hashcode) so that I can feed it through again.


Write a sh to deal with this? - not sure - its one of those where it happens often, but not often enough to justify a sh....but its something I guess I could look into.


One other thing:

Suppose two people sudos to a user (the same user) first, and then edits the file - you don't get the message about the file changing when you save/quit right? - at least that's what it appears to happen over here in bizarro world...
Reply With Quote
  #5  
Old 04-25-2007
Shell_Life's Avatar
Registered User
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Percy,
Here is one possible solution:
1) Rename the "work_file" to a "tmp_file" and immediately create a
new "work_file" so that new records can continue to be added to it.
Code:
mv  work_file  tmp_file
touch  work_file
2) "vi" the "tmp_file" and make all changes you want.

2.a) After you are finished with your changes on the "tmp_file",
do not quit yet.

2.b) Go to the end of the "tmp_file" and in the command line type:
Code:
:r work_file
This will read the "work_file" entirely and place it after the last line
of "tmp_file".

2.c) Write and quit the "tmp_file".
As you say the file is very large, if you try to save the work area
as "work_file", it may take a long time and use a lot of memory
and disk space.

3) Immediately rename "tmp_file" back to "work_file".
Code:
mv  tmp_file  work_file
See if this could work for you.
Reply With Quote
  #6  
Old 04-26-2007
Registered User
 

Join Date: Jan 2007
Posts: 13
Thank you for this.

There is still the slight potential of missing additions to the logfile between steps 2c and 3 - and its mainly due to how fast I can type "mv file file2" - in a panic, I probably spend more time hitting the backspace than actually typing commands correctly.

Its definitely a better catcher of entries than the solution I had in mind - so thanks very much!
Reply With Quote
  #7  
Old 04-26-2007
sysgate's Avatar
Unix based
 

Join Date: Nov 2006
Location: /root
Posts: 1,200
I also want to point out that your question is not dumb at all, in my previous company we were 7 admins with root access + the process who was doing the updates of sensitive data files. There were moments that we had to edit the file manually, and, fortunately, we had the possibility to rewrite the deamon to accept responces that the files is inaccessible now, and batch the data for later insertion. That's why the question is not stupid, instead may turn into headache.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 01:53 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0