Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1355
  • Last Modified:

how to find PID on windows server

Hi Folks,

I need to take thread dump of a Java process running on windows server 2003. This process is BO tomcat which runs in background.So i think first i need to find the PID of java process because i want to create a script which will run every 2 hours.Not using CRL+ALT+DEL and then find the PID.

Could you please help me with a script which can find the PID of java process?
0
jayatallen
Asked:
jayatallen
  • 5
  • 4
1 Solution
 
pbnetroCommented:
I would suggest using process explorer from Sysinternals
http://technet.microsoft.com/en-us/sysinternals/bb896653
0
 
komarCommented:
What kind of script are you looking for?

If you're looking for VB.NET, try this: http://www.codeproject.com/KB/vb/killVBprocess.aspx
0
 
jayatallenAuthor Commented:
thank your for quick reply.
Basically , i need to take thread dump of a java process running on windows server 2003 every 2 hours.
I thought:
1) create a script and save output to a file
    As, this process is running in background cant using CTRL+ BRK to generate thread dump.So i am thinking to sendsignal.exe in script but it requires PID of the java process.

2) then run it every 2 hours using scheduler.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
komarCommented:
You can use tasklist with appropriate command line arguments from a batch file to get all PID's of java.exe processes. The following batch files finds all 'java.exe' PID's and echo's them to console. You can modify the script to do whatever you want!

 
@echo off

REM
REM This for loop will find all JAVA.EXE processes and call PROCESS_ME for each PID
FOR /F "tokens=2" %%p IN ('tasklist /FI "IMAGENAME eq java.exe" ^| Find /i "java.exe"') DO CALL :PROCESS_ME %%p

REM End of batch file
GOTO:EOF

:PROCESS_ME
REM
REM This subroutine will be called by the FOR loop above with one argument equal to one of JAVA.EXE PID
REM Your code goes here
REM
echo Found java.exe Process ID: %1

REM End of subroutine
GOTO:EOF

Open in new window

0
 
jayatallenAuthor Commented:
If you dont mind could you plese explain below line:
FOR /F "tokens=2" %%p IN ('tasklist /FI "IMAGENAME eq java.exe" ^| Find /i "java.exe"') DO CALL :PROCESS_ME %%p

/F -what this means?
tokens=2 what this means?
%%p - will be substituted by ('tasklist /FI "IMAGENAME eq java.exe" ^| Find /i "java.exe"')  consecutively. right?
tasklist will show all the process.
/F | -- is it similiar like UNIX piping?
^| Find /i "java.exe  --what this is doing?

Please bear with me for my stupid questions.
0
 
jayatallenAuthor Commented:
did
"TASKLIST /? at cmd line and got answer of couple of my question .now i know what this part means
/FI "IMAGENAME eq java.exe

i tried to run the above given at cmd line but it throwing error. could you please high light where im making mistake?


C:\Documents and Settings\n361203>tasklist /FI "IMAGENAME eq java.exe" ^| Find /
i "java.exe"
ERROR: Invalid Argument/Option - '|'.
Type "TASKLIST /?" for usage.
0
 
jayatallenAuthor Commented:
C:\Documents and Settings\n361203>tasklist /FI "IMAGENAME eq java.exe" ^| Find /
i "java.exe"

removed "^" which in front of "| Find" and its working.
One question:
now cmd window doesnt close itself and asking for input

Terminate batch job (Y/N)?

is there any to get rid of it?
I tried google,and found "start" command would help..but not working in my case.
0
 
komarCommented:
The FOR /F "tokens=2 command is a windows command that can be used to from console to iterate through the content of a file, or, in this case the output of the tasklist command.

The tasklist /FI "IMAGENAME eq java.exe" is used to find all java.exe processes currently running in the system. The results are printed to the console in table format

The "|" is the same as a linux pipe, however, when used inside the for loop it has to be like "^|"

The Find /i "java.exe is used to filter the result of the tasklist command in order to remove the first two entries (which are actually the table header)

All you need to do is create a batch file (example, d:\find_java.bat) and copy/paste the below code in that file.

Now run the batch file either by double clicking on it or by opening a cmd window and typing "find_java"

One last thing, you need to modify the code I provided to include the send signal command, as follows (notice that I removed all comments for simplicity)

@echo off

FOR /F "tokens=2" %%p IN ('tasklist /FI "IMAGENAME eq java.exe" ^| Find /i "java.exe"') DO CALL :PROCESS_ME %%p
GOTO:EOF

:PROCESS_ME
echo Found java.exe Process ID: %1
start SendSignal %1
GOTO:EOF

Open in new window

0
 
jayatallenAuthor Commented:
Thank you Komar for your help .you given script is awesome it works like charm.
Thank you being patience and answering my stupid questions. you are great!.
0
 
komarCommented:
You are most welcome jayatallen, I'm glad I could help :-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now