User Tools

Site Tools


who_has_this_file_open

Who has this file open?

So you're just be-boppin along, editing files to your heart's content when you get slapped with something like this;

Found a swap file by the name "/etc/.hosts.swp"
          owned by: root   dated: Thu May 25 11:41:46 2023
         file name: /etc/hosts
          modified: no
         user name: root   host name: nagios.cyli.org
        process ID: 1459752 (still running)
While opening file "/etc/hosts"
             dated: Mon Jan 17 08:01:24 2022

(1) Another program may be editing the same file.  If this is the case,
    be careful not to end up with two different instances of the same
    file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
    If this is the case, use ":recover" or "vim -r /etc/hosts"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file "/etc/.hosts.swp"
    to avoid this message.

Swap file "/etc/.hosts.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort:

Obviously you :q out of there, but who has the file open, who do we call?;

# Use lsof on the swap file to find out the PID of the process.
[root@nagios]# lsof /etc/hosts.swp
COMMAND     PID USER   FD   TYPE DEVICE SIZE/OFF      NODE NAME
vim     2552095 root    6u   REG  253,0    16384 202892166 /etc/hosts.swp

# Pass the list of all PIDs through grep, looking for just the interesting one.
[root@nagios]# ps -ef | grep -e [2]552095
root     2552095 3216111  0 May24 pts/7    00:00:00 vim /etc/hosts.swp

# Look through the list of who's logged in on pts/7;
[root@nagios]# who
adam    pts/0        2023-05-17 12:50 (10.10.5.242)
bree    pts/1        2023-05-11 09:53 (10.10.5.232)
chaz    pts/2        2023-05-25 10:01 (10.10.5.227)
dion    pts/3        2023-05-25 10:10 (10.10.5.247)
eric    pts/5        2023-05-10 16:08 (10.10.5.250)
fred    pts/6        2023-05-22 12:20 (10.10.5.216)
gale    pts/7        2023-05-22 16:24 (10.10.5.233)
hank    pts/8        2023-05-15 16:11 (10.10.5.238)

And we know it's Gale we need to call.

who_has_this_file_open.txt · Last modified: by 127.0.0.1