0

So I've run into an interesting problem, where I made a program in java that seems to have crashed once. I don't know the exact conditions that caused it to crash (since it seemingly stopped logging for that period of time). However it is being called via a batch file (that runs:

java -jar MyProg.jar

)

With that said, when the program failed to execute properly (despite some amount of safety nets in place) it stopped running when it should have. Is there some way that I can check to see if user x is running java.exe, and if they are, do a:

taskkill /IM java.exe /f 

Before I try to run my program or something similar? Thanks.

A_Elric
  • 3,508
  • 13
  • 52
  • 85
  • I am not sure I understood it correctly but if you are asking to see the process and close them so in windows it can be done via task manager - processes tab. – TheNewOne Oct 02 '12 at 13:47
  • I'm looking to do this programmatically since the task will run every 5 minutes even when I'm away / asleep. As a result it would need to detect if the program is running as username ____________ (it's a system account that only runs this one thing) and if it is, kill it before running the jar file. – A_Elric Oct 02 '12 at 13:49
  • Are you asking how to kill a task that was started by a particular user using taskkill? – Martin Wilson Oct 02 '12 at 13:57
  • @MartinWilson essentially. Assume I wanted to kill "Java.exe" but only if it were opened by user DBell whenever I'm running my batch file. – A_Elric Oct 02 '12 at 14:22
  • How about storing the pid in a file when you start the process and then using that to kill it later? Capturing the pid is the hard part. There are a few ideas here: http://stackoverflow.com/questions/1807794/how-to-capture-the-pid-of-a-process-when-launching-it-in-dos/11616359#11616359 – Martin Wilson Oct 02 '12 at 16:42

1 Answers1

1
tasklist /nh /fi "imagename eq java.exe" | find /i "java.exe" >nul && taskkill /im java.exe /f || rem Java not running, run your program
Bali C
  • 30,582
  • 35
  • 123
  • 152
  • Unfortunately this might not be the best way to do things, since there are multiple system accounts running their respective programs on this box at different intervals. I know that there's at least 1-2 other accounts that run 5-minute tasks that use java. I would need to first find out that my account (let's say DBell) was calling java.exe to run, is there any way to do this? – A_Elric Oct 02 '12 at 13:56
  • Not that I know of, I don't think tasklist has filters for what user is running the process. Unless you knew what session ID you were, then you might be able to do it. – Bali C Oct 02 '12 at 14:28