Linux check if file is open

How to check if a file is opened in Linux?

The thing is, I want to track if a user tries to open a file on a shared account. I’m looking for any record/technique that helps me know if the concerned file is opened, at run time. I want to create a script which monitors if the file is open, and if it is, I want it to send an alert to a particular email address. The file I’m thinking of is a regular file. I tried using lsof | grep filename for checking if a file is open in gedit, but the command doesn’t return anything. Actually, I’m trying this for a pet project, and thus the question.

4 Answers 4

The command lsof -t filename shows the IDs of all processes that have the particular file opened. lsof -t filename | wc -w gives you the number of processes currently accessing the file.

I would recommend wc -l instead of wc -w which works in more cases and lsof is not going to put more than one filename per line.

The fact that a file has been read into an editor like gedit does not mean that the file is still open. The editor most likely opens the file, reads its contents and then closes the file. After you have edited the file you have the choice to overwrite the existing file or save as another file.

You could (in addition of other answers) use the Linux-specific inotify(7) facilities.

I am understanding that you want to track one (or a few) particular given file, with a fixed file path (actually a given i-node). E.g. you would want to track when /var/run/foobar is accessed or modified, and do something when that happens

In particular, you might want to install and use incrond(8) and configure it thru incrontab(5)

If you want to run a script when some given file (on a native local, e.g. Ext4, BTRS, . but not NFS file system) is accessed or modified, use inotify incrond is exactly done for that purpose.

PS. AFAIK, inotify don’t work well for remote network files, e.g. NFS filesystems (in particular when another NFS client machine is modifying a file).

If the files you are fond of are somehow source files, you might be interested by revision control systems (like git) or builder systems (like GNU make); in a certain way these tools are related to file modification.

You could also have the particular file system sits in some FUSE filesystem, and write your own FUSE daemon.

If you can restrict and modify the programs accessing the file, you might want to use advisory locking, e.g. flock(2), lockf(3).

Perhaps the data sitting in the file should be in some database (e.g. sqlite or a real DBMS like PostGreSQL ou MongoDB). ACID properties are important .

Notice that the filesystem and the mount options may matter a lot.

You might want to use the stat(1) command.

It is difficult to help more without understanding the real use case and the motivation. You should avoid some XY problem

Читайте также:  Linux mount android img

Probably, the workflow is wrong (having a shared file between several users able to write it), and you should approach the overall issue in some other way. For a pet project I would at least recommend using some advisory lock, and access & modify the information only thru your own programs (perhaps setuid) using flock (this excludes ordinary editors like gedit or commands like cat . ). However, your implicit use case seems to be well suited for a DBMS approach (a database does not have to contain a lot of data, it might be tiny), or some index locked file like GDBM library is handling.

Remember that on POSIX systems and Linux, several processes can access (and even modify) the same file simultaneously (unless you use some locking or synchronization).

Reading the Advanced Linux Programming book (freely available) would give you a broader picture (but it does not mention inotify which appeared aften the book was written).

Источник

How to check if a file is open and/or being edited in Linux

Solution 2: I wanted to note that on your system only root (file owner) has read / write permissions. You can use I would like you to go through the below link : Files are open or not This is a sample script for your reference: Solution 1: I assume that you are normally not root on your bash shell.

How to ignore «can’t open file» error message in unix

Use 2>/dev/null so that stderr will not appear:

grep table_name home/abcd/abc 2>/dev/null | cut -d":" -f1 > output.txt 
Test
$ ls -ltr a* -rwxr-xr-x 1 me me 24 Oct 9 14:06 a --wx-wx-wx 1 me me 0 Oct 9 14:10 a1 /dev/null a:123 abc 

Use the -s option with grep . The command will be

grep -s table_name home/abcd/abc | cut -d":" -f1 > output.txt 

PowerShell Open File | Syntax & Parameters of Open File, Definition of PowerShell Open File. PowerShell open file commands are meant to open the file using the PowerShell cmdlet or using the .Net Namespace and once the file is opened it can be used for reading the contents from the file by reading a single line or the whole content, writing the content to the file and then close the …

How to check if a file is open and/or being edited in Linux

The lsof helps to figure out if a file is open or not. You can use

I would like you to go through the below link : Files are open or not

This is a sample script for your reference:

#!/bin/bash while : do if ! [[ `lsof | grep filename.ext` ]] then break fi sleep 1 done echo "done" 

«No such file or directory» error when I try to run a Linux, The more strange thing is, when I written scripts directly in Linux systems by vi, then it will run without any problem. When I created a script file in Linux, download to my Windows7 PC, modified content but keep filename untouched, and then uploaded to Linux; the script file will run without any …

Unable to write on /dev/* files

I assume that you are normally not root on your bash shell. Then this command line

does not what you think. The command is executed in two steps:

  1. The bash setups the output redirection, i.e., it tries to open /dev/scull with the current user privileges.
  2. The command sudo echo 54 is executed whereas stdout is connected to the file.

As you have no write-permissions as non-root user, the first step fails and the bash reports

You must already be root to setup the output redirection. Thus execute

Читайте также:  Linux назначить права только директориям

which gives you an interactive shell with root privileges. The you can execute

I wanted to note that on your system only root (file owner) has read / write permissions. Your (normal) user account has not! So another (fast) solution would be to give all users read / write permissions.

Probably this is not the safest solution! Only do this in your test environment!

But now you test your module with your user account (without sudo)

echo «hello, world!» > /dev/scull
cat < /dev/scull

I know the thread is too old to answer but just in case if someone wants to know alternative method without switching to root user, here is the solution:

sudo bash -c 'echo "54" > /dev/my_dev' 

Cant open file for writing vim linux Code Example, All Languages >> Shell/Bash >> cant open file for writing vim linux “cant open file for writing vim linux” Code Answer. vim cant open file for writing . shell by Outrageous Ostrich on Jan 09 2021 Comment . 0 Source: stackoverflow.com

Cannot write to file with write permission

actually does something like:

sed . file > some-temp-file && mv some-temp-file file 

That last mv does a rename . That is sed -i doesn’t edit the file in place, it replaces it with a modified copy of itself.

Here it’s the rename that is blocked. It is not blocked because of permission issues (you’d get a permission denied error message if it was), but it looks like there’s some administrative restriction either to unlink the inode of your ~/.bash_profile (like some SELinux type of mandatory access control), or to the path to that file (like some AppArmor type MAC).

You can probably find more clue somewhere in the logs.

would list all the extended attributes (ACLs, security contexts) of the file.

for potentially more Linux attributes.

The ‘.’ at the end of the ls permissions output says there are extended data of some kind. chattr(1) gives the list of attributes for ext? file systems, lsattr(1) lists the current ones. Also check tha ACL for the file ( getfacl(1) ). A security policy (like SELinux) could also forbid some operations on the file.

Probably you dont have write permission of parent dir (which is weird because its your home)

Shell — Open and write data to text file using Bash?, Usually the max number of file descriptors the kernel can allocate can be found in /proc/sys/file-max or /proc/sys/fs/file-max but using any fd above 9 is dangerous as it could conflict with fd’s used by the shell internally. So don’t bother and only use fd’s 0-9. If you need more the 9 file descriptors in a bash script you … Code sample#!/bin/bash# Open file descriptor (fd) 3 for read/write on a text file.exec 3<> poem.txt# Let’s print some text to fd 3echo «Roses are red» >&3Feedback

Источник

How to determine, whether a file is open?

My code needs to go through files in a directory, picking only those, which are currently opened (for writing) by any other process on the system. The ideal solution would apply for all Unixes, but I’ll settle for a Linux-only. The program is written in Python, but I can add a custom C-function, if I have to — I just need to know, what API is available for this. One suggestion I found was to go through all file-descriptors under Linux /proc , resolving their links to see, if they point at the file of interest. But that seems rather heavy. I know, for example, that opening a file increases its reference count — filesystem will not deallocate blocks of an opened file even if it is deleted — until it is closed — the feature relied upon by tmpfile(3) . Perhaps, a user process can get access to these records in the kernel?

Читайте также:  Сколько человек используют linux

Yeah, lsof — and fuser — scan /proc . But that yields more information than I need — I don’t care, which processes have the file open. I just want to know, whether any such exist. Perhaps, this information can be obtained more cheaply, than /proc rescan?

The advantage of scanning /proc is that it is backed by direct kernel calls, not a physical file system. That gives /proc a huge performance advantage over opening and reading a directory, even just to find the names.

The advantage of scanning /proc is that it is the only way to get the information without modifying the kernel.

Источник

Is there a faster way to check if a file is in use?

I’m looking for a command line function or c function that will let me know if a file is open/in use by something. lsof and fuser do tell this, but they provide a lot of other info which results in taking up to 300ms in some situations (like when i use this code on MAC OS X, I’m devving for Linux and OS X) (I have a windows solution that takes 5ms so I’m trying to find something in Unix that also is very quick, and just returns true or false if file is in use)

2 Answers 2

If you are using this as a lock, it will not work as neither lsof or fuser prevent race conditions.

The basic process that lsof does is trawl through all processes /proc/*/fs looking for open file descriptors. This is going to take time no matter what you do.

You can do this yourself, but it is not likely to be any faster as you have to check for every open process on the system.

If what you are doing is time critical, figure out another way to do it.

  • If you control the file through a program that you wrote; use a lock file.
  • If you are running some command that operates on the file, look and see what documentation that command/program offers and see if it can’t make a lockfile. Failing that, see if it can’t make a file with its PID inside it. Then you can look at /proc//fs to see if your file is currently open or not. Looking at only one processes open file descriptors will be much faster then mapping across all of them.
  • Otherwise in order to help you I am going to need more information about what you are doing.

You gave more information in a comment that you want to determine if Firefox is running on a given system. The best way to do this is to look for Firefox’s lock files. These are stored in default locations specified on the Mozilla wiki.

For example, on linux, have your program do the following:

  • open up the ~/.mozilla/firefox/ directory.
  • List all directories, filtering for directories ending in .default . (I think all profiles end with .default , if not just crawl into every directory.)
  • In each directory above, look for the existence of a file named lock or .parentlock . If you see one or both files, Firefox is open.

This algorithm ought to execute faster than what you do on windows currently.

Источник

Оцените статью
Adblock
detector