1845948 Members
2768 Online
110250 Solutions
New Discussion

Re: Unix command

 
SOLVED
Go to solution
sheevm
Regular Advisor

Unix command

Hi All,

Some files are coming into the directory via remote FTP transfer. The application is processing those files after the FTP is complete.

We have a requirement to write a Unix script to check for a given file that is still open for writing. If it is open the script passes back to the application "true" or "false".

What is the best command to acheive this?

We are running HP-X 11.23

Thanks in advance for your help.
be good and do good
10 REPLIES 10
Ivan Krastev
Honored Contributor

Re: Unix command

Use fuser to see if file is oppened from process.
Another option is lsof - http://hpux.cs.utah.edu/hppd/hpux/Sysadmin/lsof-4.77/


regards,
ivan
Marco A.
Esteemed Contributor

Re: Unix command

Using fuser on that file you can see the processes that are using currently the file, if shows only the file name, then the file is not in use by another application or process.

I hope this helps,
Just unplug and plug in again ....
James R. Ferguson
Acclaimed Contributor
Solution

Re: Unix command

Hi:

I have used this with success:

PIDS=`fuser ${FILE} 2> /dev/null` #...look for any process using file...
if [ ! -z "${PIDS}" ]; then
print -u2 "Error: '${FILE}' is inuse by process(es): ${PIDS}"
... # do whatever you need to do ...
fi
...

Regards!

...JRF...
Marco A.
Esteemed Contributor

Re: Unix command

In addition, this is the man page :

http://www.techsolutions.hp.com/en/B2355-60105/fuser.1M.html

Rgds,
Just unplug and plug in again ....
sheevm
Regular Advisor

Re: Unix command

Thanks for the responses.

James,

I used your command to test.

I opened up a file for editing in one session.

vi p3i.sh

On another session I did the

fuser /home/user/p3i.sh

It only returned the "/home/user/p3i.sh" No processes were returned.

I am not sure why it did not the process name on "vi p3i.sh"

Can you please see what is wrong with the command I am executing?

Thanks
be good and do good
spex
Honored Contributor

Re: Unix command

Hello,

According to vi(1):

In vi, the terminal screen acts as a window into a memory copy of the
file being edited. Changes made to the file copy are reflected in the
screen display. The position of the cursor on the screen indicates
the position within the file copy.

So, in your example, vi is operating on a cached version of 'p3i.sh'. Since the actual file is not open, running fuser on the file returns nothing.

Try this instead:
$ tail -f ./p3i.sh
$ fuser ./p3i.sh

PCS
James R. Ferguson
Acclaimed Contributor

Re: Unix command

Hi (again):

Using 'vi' for your test will fail, as you found. 'Vi' opens a file and reads it into memory for the duration of your session's work. Only when you write and quit does your (disk) copy get updated.

You can use this small Perl snippet to open a file called '/tmp/data' and keep it open for your test:

# perl -le 'open(FH,"+>>","/tmp/data") or die;print FH "\n";sleep 120' &

# fuser /tmp/data

Regards!

...JRF...
sheevm
Regular Advisor

Re: Unix command

Hi,

The file I am checking for usage is located in the NFS mount point. Do I have to give the full server:/mountpoint/file to fuser?

Thanks
be good and do good
spex
Honored Contributor

Re: Unix command

Hi,

No, the filename is sufficient. However, realize that fuser will not tell you if the file is open by another system.

PCS
Steven Schweda
Honored Contributor

Re: Unix command

If you have some control over the FTP sender,
a relatively simple way to deal with this
problem is to "put" the file into some
temporary name (or directory), and then, when
the transmission is complete, "rename" it to
its final destination name (or directory).
That way, when the file appears at its final
destination, you know that the sender thinks
that it's complete, and it will be closed.