Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

windows batch command to determine working directory of a process

Why I ask is that my program uses 3rd party software that sometimes leaves behind orphan processes that have no link back to my program or the 3rd party process. These orphan processes start to add up and consume tons of resources over time. I would like to kill them off periodically, but in order for me to do that, I need to know for sure they were created by my program and not some other program. I have viewed the orphan processes in Process Explorer and when looking at the properties of the process, I see a field called "Current Directory". The current directory for the orphaned process is the install directory of my program. This would give me reassurance I am killing a process created by my program.

Since these processes are created by a 3rd party, I need to just kill them after they are created by running taskkill on them or something. Is there a way to figure out the current working directory of a process using out of the box windows commands in a batch file? If this can be done through wmic queries that would be preferable, but I cannot seem to find the current working directory when using wmic. I assume if Process Explorer is able to obtain this info, I should be able to get it too through some batch commands.

like image 310
user972276 Avatar asked Dec 13 '13 22:12

user972276


People also ask

How do I find the working directory of a process?

You can obtained this information by visiting /proc/pid/cwd directory or using the pwdx command. The pwdx command reports the current working directory of a process or processes.

How do I find the current working directory in Windows?

In a Windows command prompt, chdir or cd will print the full path of the current working directory in the console.

What directory is used for running processes?

Within the /proc/ directory, one can find a wealth of information detailing the system hardware and any processes currently running. In addition, some of the files within the /proc/ directory tree can be manipulated by users and applications to communicate configuration changes to the kernel.


1 Answers

tlist from WDK to the rescue! The 2nd line of its output ("CWD: ...") shows the working directory of a process:

> tlist 944
 944 postgres.exe
   CWD:     D:\Lab\Database\pgsql\test\
   CmdLine: "D:/Tools/pgsql/bin/postgres.exe"  -D "."
   VirtualSize:   221116 KB   PeakVirtualSize:   242620 KB
   WorkingSetSize: 17076 KB   PeakWorkingSetSize: 19336 KB
   NumberOfThreads: 4
   9084 Win32StartAddr:0x00000000 LastErr:0x00000000 State:Waiting
   8504 Win32StartAddr:0x00000000 LastErr:0x000000b7 State:Waiting
   8616 Win32StartAddr:0x00000000 LastErr:0x00000000 State:Waiting
   7468 Win32StartAddr:0x00000000 LastErr:0x00000000 State:Waiting
    9.3.5.14202 shp  0x0000000000400000  D:\Tools\pgsql\bin\postgres.exe
 6.1.7601.18247 shp  0x00000000770D0000  C:\Windows\SYSTEM32\ntdll.dll
 ...

See the doc for more info.

like image 117
wdscxsj Avatar answered Sep 20 '22 11:09

wdscxsj