title: Linux Script to check if process is running and restart if not
body: <p>I am having this script which looks for the process <strong>filebeat</strong> and restarts it if is not running. Cron runs this script every 5 minutes. Most of the time this works fine except sometime it creates multiple filebeat process. Can someone please point out what is the issue in my script.</p>
  
  <pre><code>#!/bin/bash
  PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  service=filebeat
  servicex=/usr/share/filebeat/bin/filebeat
  pid=`pgrep -x "filebeat"`
  if [ $pid &gt; /dev/null ]
  then
  echo "$(date) $service is running!!!"
  else
  echo "$(date) starting $service"
  cd /home/hpov/beats/filebeat
  ./filebeat -c filebeat.yml &amp;
  fi
  </code></pre>