Linux which process locked file

Finding and removing locked files on a GNU/Linux system

In this post we will see how you can find and remove locked files on a GNU/Linux system.

For this purpose we are going to use lsof(8) and lslk(8) tools.

Sometimes it may happen that an application is not able to start up, or even not working properly due to some files being locked by other processes. This may happen in an environment where multiple clients are using shared resources, like for example a shared home which is mounted on multiple machines.

In this post we will see how to find and remove such locked files under a Debian GNU/Linux system.

Requirements

Installation

First let’s install the needed tools:

$ sudo apt-get update $ sudo apt-get install lsof lslk 

Now that we have the tools, le’s have a look at the files that are currently being locked.

Finding the locked files

In order to view all locked files on the current system, simply execute lslk(8) .

In this document as an example, we will find and remove a locked file from a KDE session on a shared storage, where multiple clients are mounting their home partitions from an NFS server.

Now let’s view the locked files on the system:

$ sudo lslk SRC PID DEV INUM SZ TY M ST WH END LEN NAME (unknown) 1190 254,1 308115263 r 0 0 0 0 0 /mnt/homes (/dev/mapper/vg0-homes) (unknown) 2294 254,1 308115263 r 0 0 0 0 0 /mnt/homes (/dev/mapper/vg0-homes) (unknown) 2392 254,1 308115263 r 0 0 0 0 0 /mnt/homes (/dev/mapper/vg0-homes) (unknown) 2397 254,1 308115263 r 0 0 0 0 0 /mnt/homes (/dev/mapper/vg0-homes) lpd 3028 254,0 1212425 5 w 0 0 0 0 0 /var/run/lpd.pid master 3187 254,0 262203 17 w 0 0 0 0 0 /var/spool/postfix/pid/master.pid atd 3260 254,0 1212456 5 w 0 0 0 0 0 /var/run/atd.pid (unknown) 3262 254,0 1212458 w 0 0 0 0 0 /var (/dev/mapper/vg0-var) nmbd 14654 254,0 688137 32768 r 0 4 0 4 0 /var/lib/samba/public/unexpected.tdb nmbd 14654 254,0 688148 696 r 0 4 0 4 0 /var/lib/samba/public/messages.tdb nmbd 14654 254,0 1212429 6 w 0 0 0 0 0 /var/run/samba/public/nmbd.pid smbd 14656 254,0 688138 8192 r 0 4 0 4 0 /var/lib/samba/public/locking.tdb smbd 14656 254,0 688132 8192 r 0 4 0 4 0 /var/lib/samba/public/brlock.tdb smbd 14656 254,0 688133 163840 r 0 4 0 4 0 /var/lib/samba/public/connections.tdb smbd 14656 254,0 688147 188416 r 0 4 0 4 0 /var/lib/samba/public/sessionid.tdb smbd 14656 254,0 688148 696 r 0 4 0 4 0 /var/lib/samba/public/messages.tdb smbd 14656 254,0 1212430 6 w 0 0 0 0 0 /var/run/samba/public/smbd.pid nmbd 14669 254,0 655876 32768 r 0 4 0 4 0 /var/lib/samba/users/unexpected.tdb nmbd 14669 254,0 655875 696 r 0 4 0 4 0 /var/lib/samba/users/messages.tdb nmbd 14669 254,0 1212448 6 w 0 0 0 0 0 /var/run/samba/users/nmbd.pid smbd 14671 254,0 655870 8192 r 0 4 0 4 0 /var/lib/samba/users/locking.tdb smbd 14671 254,0 655719 696 r 0 4 0 4 0 /var/lib/samba/users/brlock.tdb smbd 14671 254,0 655718 57344 r 0 4 0 4 0 /var/lib/samba/users/connections.tdb smbd 14671 254,0 655843 106496 r 0 4 0 4 0 /var/lib/samba/users/sessionid.tdb smbd 14671 254,0 655875 696 r 0 4 0 4 0 /var/lib/samba/users/messages.tdb smbd 14671 254,0 1212450 6 w 0 0 0 0 0 /var/run/samba/users/smbd.pid 

What is interesting in the above output are the first four lines, which are marked as unknown .

SRC PID DEV INUM SZ TY M ST WH END LEN NAME (unknown) 1190 254,1 308115263 r 0 0 0 0 0 /mnt/homes (/dev/mapper/vg0-homes) (unknown) 2294 254,1 308115263 r 0 0 0 0 0 /mnt/homes (/dev/mapper/vg0-homes) (unknown) 2392 254,1 308115263 r 0 0 0 0 0 /mnt/homes (/dev/mapper/vg0-homes) (unknown) 2397 254,1 308115263 r 0 0 0 0 0 /mnt/homes (/dev/mapper/vg0-homes) 

We get actually quite useful information from the lslk(8) output — we know where the file resides on the file system (in our case that is /mnt/homes ), what is it’s inode number, a PID, etc. Please refer to the manual page of lslk(8) for more information about the different options and what the different column meaning is.

Читайте также:  Suse linux enterprise server i586

What we can also notice is that there are four processes that are using the same file from /mnt/homes .

Now let’s try to find the files opened by these processes using the lsof(8) tool:

$ sudo lsof -p 1190,2294,2392,2397 

The above command will list all open files for the specified processes. Sometimes it may happen that we don’t actually get any result from that command.

What we can do in that case is to try to find the file using it’s inode number. So let’s search for the file by it’s inode number:

$ sudo find /mnt/homes -inum 308115263 /mnt/homes/home/foo/.qt/.qtrc.lock 

After searching for the file using it’s inode number we’ve found the locked file and we can remove it:

$ sudo rm -f /mnt/homes/home/foo/.qt/.qtrc.lock 

Now that we have removed the locked file, we can start up our applications again.

Источник

fcntl how to know which process hold lock file?

I’m new with fcntl locking and following this example to create a sample lock in linux using c code: http://www.informit.com/articles/article.aspx?p=23618&seqNum=4 I wonder how can we can print out which process hold the lock file and which process is waiting for lock. I consider using l_pid to figure out the process id which is holding the lock but i’m not sure the right way to do it. What is the best way to print out which process is holding the lock?

2 Answers 2

As the man 2 fcntl page describes, you can use the F_GETLK to obtain the process ID that has the conflicting lock (if the conflicting lock is a process-associated one). So, for example,

/* Return 0 if descriptor locked exclusively, positive PID if a known process holds a conflicting lock, or -1 if the descriptor cannot be locked (and errno has the reason). */ static pid_t lock_exclusively(const int fd) < struct flock lock; int err = 0; if (fd == -1) < errno = EINVAL; return -1; >lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; lock.l_len = 0; if (!fcntl(fd, F_SETLK, &lock)) return 0; /* Remember the cause of the failure */ err = errno; lock.l_type = F_WRLCK; lock.l_whence = SEEK_SET; lock.l_start = 0; lock.l_len = 0; lock.l_pid = 0; if (fcntl(fd, F_GETLK, &lock) == 0 && lock.l_pid > 0) return lock.l_pid; errno = err; return -1; > 

Do note that fd must be open for reading and writing. I recommend using open(path, O_RDWR | O_NOCTTY) or open(path, O_WRONLY | O_NOCTTY) . Closing any file descriptor to the same file will release the lock.

Some may say that re-setting the lock memebers before the second fcntl() call is unnecessary, but I’d rather err on the side of caution here.

As to how to report it, I would simply use

int fd; pid_t p; fd = open(path, O_RDWR | O_NOCTTY); if (fd == -1) < fprintf(stderr, "%s: Cannot open file: %s.\n", path, strerror(errno)); exit(EXIT_FAILURE); >p = lock_exclusively(fd); if (p < 0) < fprintf(stderr, "%s: Cannot lock file: %s.\n", path, strerror(errno)); exit(EXIT_FAILURE); >else if (p > 0) < fprintf(stderr, "%s: File is already locked by process %ld.\n", path, (long)p); exit(EXIT_FAILURE); >/* fd is now open and exclusive-locked. */ 

The user can always run e.g. ps -o cmd= -p PID to see what command that is (or you can try reading /proc/PID/cmdline in Linux).

Читайте также:  Linux on mac sierra

Источник

[How To] find process information in Linux (PID and more).

Each process in the operating system has its own unique identifier, by which you can get information about this process, as well as send it a control signal or complete it.
In Linux, name of this identifier is PID, and it can be recognized in several ways. In this article, we will discuss how to find out the PID of process, and why you might need it.

The content of the article

Find the Process ID (PID)

Command ps

The most common way to find out the Linux PID is to use the ps command: ps aux | grep name_of_process

$ ps aux | grep apache root 15778 0.0 0.4 204904 19008 ? Ss 23:25 0:00 /usr/sbin/apache2 -k start www-data 15780 0.0 0.2 205248 9932 ? S 23:25 0:00 /usr/sbin/apache2 -k start www-data 15781 0.0 0.2 205248 9932 ? S 23:25 0:00 /usr/sbin/apache2 -k start www-data 15782 0.0 0.2 205248 9932 ? S 23:25 0:00 /usr/sbin/apache2 -k start www-data 15783 0.0 0.2 205248 9932 ? S 23:25 0:00 /usr/sbin/apache2 -k start www-data 15784 0.0 0.2 205248 9932 ? S 23:25 0:00 /usr/sbin/apache2 -k start root 15867 0.0 0.0 17484 888 pts/1 R+ 23:26 0:00 grep --color=auto apache

In addition, the command will also display the PID for grep, because the process was started during the search. To remove it, add the following filter: ps aux | grep name_of_process | grep -v grep
For example, let’s find the PID of all processes whose name contains the word “Apache”:

$ ps aux | grep apache | grep -v grep root 15778 0.0 0.4 204904 19008 ? Ss 23:25 0:00 /usr/sbin/apache2 -k start www-data 15780 0.0 0.2 205248 9932 ? S 23:25 0:00 /usr/sbin/apache2 -k start www-data 15781 0.0 0.2 205248 9932 ? S 23:25 0:00 /usr/sbin/apache2 -k start www-data 15782 0.0 0.2 205248 9932 ? S 23:25 0:00 /usr/sbin/apache2 -k start www-data 15783 0.0 0.2 205248 9932 ? S 23:25 0:00 /usr/sbin/apache2 -k start www-data 15784 0.0 0.2 205248 9932 ? S 23:25 0:00 /usr/sbin/apache2 -k start

Command pgrep

If you do not need to see detailed information about the process, but only the PID is enough, you can use the pgrep utility:

$ pgrep apache 15778 15780 15781 15782 15783 15784

Command pidof

The pidof command searches for the PID of a particular process by its name. No occurrences, the process name should only match the desired one:
Using the -s option, you can ask the command to display only one PID:

$ pidof apache2 15784 15783 15782 15781 15780 15778 $ pidof -s apache2 15784

Command pstree

The pstree command allows you to view a list of child processes for a specific process, as well as their pid identifiers. For example, look at the Apache process tree:

$ pstree -p | grep apache2 |-apache2(15778)-+-apache2(15780) | |-apache2(15781) | |-apache2(15782) | |-apache2(15783) | `-apache2(15784)

Video

Which process have locked the file

Above, we looked at how to get the PID of a Linux process by name, and now let’s find out the PID from the file that the process uses. For example, we want to delete a file, and the system tells us that it is being used by another process.
With the help of the command lsof you can see which processes are using the directory or file at this moment. For example, open the audio file in the totem player, and then see which process uses its file:

Читайте также:  Установить mysql сервер linux

At the beginning of the line we see the name of the program, and then comes its PID. There is another command that allows you to perform a similar task – it is a fuser:

Only the file and process PID will be displayed here. After the PID, there is one letter that indicates what this process does with the file or folder:
C – is the current directory;
r – is the root directory;
f – the file is open for reading or writing;
e – the file is executed as a program;
m – the file is included as a library.

Who used the file in Linux

To know the process that now locks the file is quite simple. But how do you know which process accesses a file for a long time, for example, executes it as a program or reads data from there? This task is more difficult, but completely solvable with the help of the auditd subsystem of kernel.In Ubuntu, it will have to be installed using the command:

Now create a rule for monitoring. For example, let’s track who runs the who utility:

auditctl -w /usr/bin/who -px -k who_exec

Here -w is the address of the file that we will be tracking, p is the action to be monitored, k is an arbitrary name for the rule. As an action, the following options can be used:
x – execution;
w – record;
r – reading;
a – change attributes.

Now we will perform a who once and see what happens in the log using the ausearch command :

sudo ausearch -i -k who_exec

Here in the SYSCALL section there is the PID of the process under which the program was launched, and also the PPID – the program that launched our who. We copy this PID and look at the information about it with the help of ps:

It becomes clear that this is bash.

What process is using a port in Linux

Sometimes you need to know the PID of a Linux program that uses a network port, for example 80. You can use the ss command to do this:

We see that these are several Apache processes. Using the dport option , you can find out which process is sending data to the specified port:

Conclusion

In this article, we looked at how to find out the PID process in Linux by various conditions: name or file. As you can see, everything is quite simple, and in a few minutes, you can understand what is happening with your operating system, and what process is responsible for it.

Источник

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