check if file is open with lsof
The lines that appear in the output of lsof
are open files. If your file is not there it means it is not open. Among the columns are PID (the process id of the program that has the file open) and the FD (the file descriptor associated with the open file). No particular value for these indicates open/closed. If it appears at all it means it's open.
Check out http://linux.die.net/man/8/lsof and search for the text contains the first nine characters
Check if file opened bash
With lsof command you can get the files that are currently open.
$> lsof | grep abc.txt | wc -l
If that is not 0, the file is open.
If you know the pid of the process that should be using that file, you could use it to filter the results using -p option.
$> lsof -p <pid>
Check if the file is in a open state in Perl
The problem is outside of Perl's control; you're using your shell's redirection to redirect STDOUT to a file, so from Perl's perspective writes are going to STDOUT; it didn't set up the redirection.
From within your Perl program you can ask if -t STDOUT
to see if STDOUT has been redirected to a file or if it is a terminal. But that doesn't really capture whether that file is opened.
One way that would work would be to test lsof /logs/user/data.out
. lsof
is a shell utility, not a Perl function, so you would need to run it with backticks, qx
, or similar. That will provide output of who has a claim on that file, including the PID. So with lsof
you can determine if a file is opened, and who has it opened. If multiple entities have it opened, you'll see that too (try lsof /dev/null
for example; lots of entities will have that opened).
If lsof
doesn't return output for a file, that's because nobody has it opened.
Keep in mind, though, that if the redirection is set up externally to the script itself, it would be fragile to assume that the redirection never changes, by hard-coding that filename. It may be better to lsof based on username where the user matches the user that ran the script, and then find a file that matches expectations.
The lsof -u [UID or USERNAME]
command would list only those files open by a specific user. You could combine that with $<
to run against the script-running user:
my $lsof_output = `lsof -u $< $path`;
Then examine the output for files opened by the same pid: $$
.
For completeness, I'll mention there's a Perl module on CPAN called Unix::Lsof which handles the details of parsing through lsof output for you.
test if file opened from php is still open
using grep with lsof is probably the slowest way, as lsof will scan everything. you can narrow down the scope that lsof uses to one directory by doing:
lsof +D /var/f/ping
or similar.
there's a good and easy-to-read overview of lsof uses here:
http://www.thegeekstuff.com/2012/08/lsof-command-examples/
alternately, you could experiment with:
http://php.net/manual/en/function.fam-monitor-file.php
and see if that meets your requirements better.
How find out which process is using a file in Linux?
You can use the fuser
command, like:
fuser file_name
You will receive a list of processes using the file.
You can use different flags with it, in order to receive a more detailed output.
You can find more info in the fuser's Wikipedia article, or in the man
pages.
Related Topics
How to Boot the Linux Kernel Without Creating an Initrd Image
How to Get Notified for Ip Address Changes Automatically
Magento Apache Configuration (App/Etc/Local.Xml Accessible Warning)
How to Repeat a Dash (Hyphen) in Shell
How to Change the Soname of a Binary Directly
Shell Script to Copy and Prepend Folder Name to Files from Multiple Subdirectories
Determine Usb Device File Path
Where Is Open_Max Defined for Linux Systems
Shell Variable Issue When Trying to Mkdir
Creating a Self-Extracting Zip Archive on a Linux Box
Getting Exit Status Code from 'Ftp' Command in Linux Shell
How to Prefill Command Line Input
How Does Stat Command Calculate the Blocks of a File
Copy a Directory Structure with File Names Without Content
Retrieve Plain Text Script from Compiled Bash Script
Start Rails Server Automatically When Ever I Start My Ubuntu MAChine