Windows how to check which process locks file command – A M$ Windows equivalent of lsof command

windows-how-to-check-which-process-locks-file-command-a-ms-windows-equivalent-of-lsof-command
I've had a task today to deploy a new WAR (Web Application Archive) Tomcat file on Apache Tomcat server running  on Windows server 2008 R2 UAT environment.
The client Tomcat application within war is providing a frontend to an proprietary Risk Analysis application called Risiko Management (developed by a German vendor called Schleupen).
The update of WAR file was part of a version upgrade of application so, both "Risk Analysis" desktop standalone server RiskKit and the Web frontend was developed by Schleupen had to be updated.
In order to update I followed the usual .WAR Tomcat Javafile upadate Tomcat process.
1. Stopped Tomcat running service Instance via services.msc command e.g.

Start (menu) -> Run

services.msc

stopping-tomcat-application-howto-stop-service-ms-windows-screenshot


2. Move (by Renaming) old risk-analysis.war to risk-analysis_backup_2015.war
and also rename the automatically Tomcat extracted folder (named same name as the WAR archive file directory – D:\web\Apache-Tomcat-7.0.33\webapps\Risiko-Analysis\ to :\web\Apache-Tomcat-7.0.33\webapps\Risiko-Analysis_backup_2015, i.e. run:

C:\Users\risk-analysis> D:
D:\>
D:\> CD \Web\Apache-Tomcat-7.0.33\webapps\

D:\Web\Apache-Tomcat-7.0.33\webapps> move risk-analysis.war risk-analysis_2015.war
D:\Web\Apache-Tomcat-7.0.33\webapps> move  
Risiko-Analysis\  Risiko-Analysis_backup_2015\

But unfortunately I couldn't rename it and I got below error:
move-windows-command-access-is-denied-tiny-screenshot
Also I tried copying it using Windows Explorer Copy / Paste but this didn't worked either, and I got below error :
cant-move-risk-analysis-tomcat-java-application-error-ms-windows-screenshot
3. Finding what Locks a directory or File on M$ Windows

Obviously, the reason for unable to copy the directory was something was locking it. Actually there are plenty of locked files many running applications like Explorer do. A good example for all time locked file is Windows (swap file) pagefile.sys – this is Windows Linux equivalent of swap filesystem (enabled / disabled with spapon / swapoff commands).
Having the directory locked was a strange problem, because the Tomcat process was not running as I checked closely both in Windows taskmgr GUI interface and manually grepped for the process with tasklist command like so:

d:\>tasklist /m|find /i "tomcat"

tomcat7.exe                   4396 ntdll.dll, kernel32.dll, KERNELBASE.dll,
For people like me who use primary Linux , above command shows you very precious debugging information, it shows which Windows libraries (DLL) are loaded in memory and used by the process 

(Note that when Tomcat is running, it is visible with command)

D:\> wmic.exe process list brief | find /i "tomcat"
526          tomcat7.exe          8         4396       49           156569600

Just for those wondering the 156569600 number is number of bytes loaded in Windows memory used by Tomcat.
After tomcat was stopped above command returned empty string meaning obviously that tomcat is stopped ..
BTW, wmic command is very useful to get a list of process names (to list all running processes):
 D:> wmic.exe process list brief
get-all-process-names-in-command-line-with-windows-wmic-command-screenshot
Well obviously something was locking this directory (some of its subdirectories or a file name within the directory / folder), so I couldn't rename it just like that.
In Linux finding which daemon (service) is locking a file is pretty easy with lsof command  did a quick google search which pointed me to the famous handle part of SysInternals tools.
The command tool Handle.exe was exactly what I was looking for.
handle-sysinternals-tool-to-windows-see-all-locked-files-and-what-is-locking-them-ms-windows-screenshot
To get list of all opened (locked) files and see which application has opened it just exec command without arguments, you will get
plenty of useful info which will help you to better understand what Windows OS is doing invisible in the background and what app uses what.
handle-command-part-of-sysinternals-witout-any-arguments-display-opened-locked-files-in-windows
handle is pretty much Windows equivalent command of Linux lsof
To get which file was locked by Tomcat I used handle in conjuntion with find /i command which is pretty much like Linux's grep equivalent

C:\TEMP> Handle.exe | FIND /I "Tomcat"
   1C: File  (RW-)   D:\Web\Apache-Tomcat-7.0.33\webapps\Risk-Analysis\images\app

Alternatively if you have sysinternals and prefer GUI environment you can use SysInternals Process Explorer (press CTRL + F) and look for a string:
process-explorer-toolbar-find-what-is-locking-a-file-or-directory-windows
Next to handle I found also another GUI program (Internet Explorer extension) WhoLockMe, that can be used to show you all running programs and locked files by this programs.
WhoLockMe is pretty straight forward to use, though it shows GUI output you have to run the command from cmd line. Below is sample output screenshot of wholockme.

who-lock-me-windows-screenshot-see-which-files-running-programs-are-locking-on-ms-windows

To Install Wholockme 

Unzip "WhoLockMe.zip" in a directory (for exemple : "C:\Program Files\WhoLockMe")
Launch "Install.bat" or execute this Windows registry modification command :

regsvr32 "C:\Program Files\WhoLockMe\WhoLockMe.dll"

To Uninstall WhoLockMe - if you need to later:

Execute command :

regsvr32 /u "C:\Program Files\WhoLockMe\WhoLockMe.dll"

Reboot (Or Kill Explorer.exe).
Removes the "C:\Program Files\WhoLockMe" directory and its contents.
Probably there are other ways to find out what is locking a file or direcotry using powershell scripts or .bat (batch) scripting. If you know of other way using default Windows embedded commands, please share in comments.

Comments

Popular posts from this blog

Why I Wake Up Early (And 9 Reasons You Should Do So Too)

Google launches OnHub, a simple, expensive Wi-Fi router

2 iPhone Apps to Manage Expenses and Budget Efficiently