title: How to kill a process by reading from pid file using bash script in Jenkins?
body: <p>Inside <strong>Jenkins</strong>, I have to run 2 separate scripts: <strong>start.sh</strong> and <strong>stop.sh</strong>. These scripts are inside my application which is fetched from a SCM . They are inside same directory.</p>
  
  <p>The <strong>start.sh</strong> script runs a process in the background using <strong>nohup</strong>, and writes the <code>processId</code> to <code>save_pid.pid</code>. This script works fine. It successfully starts my application.</p>
  
  <p>Then inside <strong>stop.sh</strong>, I am trying to read the <code>processId</code> from <code>save_pid.pid</code> to delete the process. But,I am unable to delete the process and the application keeps running until I kill the process manually using: <code>sudo kill {processId}</code>. </p>
  
  <p>Here are the approaches that I have tried so far inside <strong>stop.sh</strong> but none of these work:</p>
  
  <pre><code>kill $(cat /path/to/save_pid.pid)
  
  kill `cat /path/to/save_pid.pid`
  
  kill -9 $(cat /path/to/save_pid.pid)
  
  kill -9 `cat /path/to/save_pid.pid`
  
  pkill -F /path/to/save_pid.pid
  </code></pre>
  
  <p>I have also tried all of these steps with <code>sudo</code> as well. But, it just doesn't work. I have kept an <code>echo</code> statement inside <strong>stop.sh</strong>, which prints and then there is nothing.</p>
  
  <p>What am I doing wrong here ?</p>
  
  <p><strong>UPDATE:</strong> </p>
  
  <p>The <code>nohup</code> command that I am using inside <strong>start.sh</strong> is something like this:</p>
  
  <pre><code>nohup deploy_script &gt; $WORKSPACE/app.log 2&gt;&amp;1 &amp; echo $! &gt; $WORKSPACE/save_pid.pid
  </code></pre>
  
  <p><em>Please Note:</em></p>
  
  <blockquote>
    <p>In my case, the value written inside <code>save_pid.pid</code> is surprisingly
    always less by 1 than the value of actual <code>processId</code>. !!!</p>
  </blockquote>