title: Prevent jar from running if it's already running?
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/13857979/java-shellscript-code-to-find-out-if-a-jar-file-is-already-running-on-current-ma">java/shellscript code to find out if a jar file is already running on current machine</a>  </p>
  </blockquote>
  
  
  
  <p>I would love to get a cross-platform solution for this, but even if it's unix only- that would be fine.</p>
  
  <p>The simple solution would be to do this from the shell (Pseudocode)(if ps -aux | grep myJar | print {awk 2}.contains myPID, don't run myProgram.</p>
  
  <p>Now unfortunately our linux team doesn't want a script like that running in production since it can (admittedly) have undesired behaviors.</p>
  
  <p>So what I need is to be able to have a file run, and when it runs see if another program is running.   If the program is already running and it's below that time limit, it should prevent the program from running.</p>
  
  <p>A bit of an example:</p>
  
  <p>Myprog.jar  -- timeout 5 min</p>
  
  <p>Myprog.jar is in a cron that gets called every 4 minutes, </p>
  
  <p>the first time it's called it launches, the second time it's called it's still running, but since it's not over the timeout, it's fine. </p>
  
  <p>If it's still running when the third check comes through (at 8 minutes into execution) it's killed, and its process is replaced by itself afterwards.</p>
  
  <p>If someone can help me understand how to do this (We've been trying to set up a lock file with limited success)</p>
  
  <p>Thanks!</p>