title: Linux Script to check if process is running and act on the result
body: <p>I have a process that fails regularly &amp; sometimes starts duplicate instances..</p>
  
  <p>When I run:
  <code>ps x |grep -v grep |grep -c "processname"</code>
  I will get:
  <code>2</code>
  This is normal as the process runs with a recovery process..</p>
  
  <p>If I get
  <code>0</code>
  I will want to start the process
  if I get:
  <code>4</code>
  I will want to stop &amp; restart the process</p>
  
  <p>What I need is a way of taking the result of <code>ps x |grep -v grep |grep -c "processname"</code></p>
  
  <p>Then setup a simple 3 option function</p>
  
  <pre><code>ps x |grep -v grep |grep -c "processname"
  if answer = 0 (start process &amp; write NOK &amp; Time to log /var/processlog/check)
  if answer = 2 (Do nothing &amp; write OK &amp; time to log /var/processlog/check)
  if answer = 4 (stot &amp; restart the process &amp; write NOK &amp; Time to log /var/processlog/check)
  </code></pre>
  
  <p>The process is stopped with
  <code>killall -9 process</code>
  The process is started with
  <code>process -b -c /usr/local/etc</code></p>
  
  <p>My main problem is finding a way to act on the result of <code>ps x |grep -v grep |grep -c "processname"</code>.</p>
  
  <p>Ideally, I would like to make the result of that grep a variable within the script with something like this:</p>
  
  <p><code>process=$(ps x |grep -v grep |grep -c "processname")</code></p>
  
  <p>If possible.</p>