0

I use the following command to find out if file descriptor is opened:

/usr/sbin/lsof -a -c sqlplus -u ${USER} | grep -l "${FILE_NAME}”

If it is not, I perform some actions. The file is a log spooled from sqlplus.

Sometimes lsof tells that file descriptor is not opened, but then I find some new data in this file. It happens very seldom, so I cannot reproduce it.

  1. What can be the reason?
  2. How does sql spool work? Does it keep open file descriptor from the SPOOL file command till the SPOOL OFF comand or does it open and close file descriptor several times?
idobr
  • 1,537
  • 4
  • 15
  • 31

1 Answers1

0

You probably have a "race condition". Sqlplus opened the file, put some new data in it and closed it in between the time lsof checked the file and when you used the result of lsof to process the file.

Often, the best way to avoid race conditions in a file system is to rename the file concerned before processing it. Renaming is a relatively cheap operation and this stops other processes from opening/modifying the file while your process deals with it. You need to make sure that if the file is open in another process when it is renamed that you wait until it is no longer being accessed via the open file handle before your process deals with it.

Most programmers write code that is littered with race conditions. These cause all sorts of unreproducible bugs. You'll be a much better programmer if you keep in mind that almost all programs have multiple processes sharing resources and that sharing must always be managed.

  • Thank you for the response. I cannot just rename the file. I need to know that all data was written there. I can add some PROMPT to the end of each sql file and grep for it, but I guess there should be a better way. – idobr Apr 23 '13 at 23:35