title: Bash: Check up, Run a process if not running
body: <p>Bash: Check up, Run a process if not running</p>
  
  <p>Hi , 
  My requirement is that , if Memcache server is down for any reason  in production , i want to restart it immediately </p>
  
  <p>Typically i will start Memcache server in this way with user as nobody with replication as shown below </p>
  
  <p><strong>memcached -u nobody -l 192.168.1.1 -m 2076 -x 192.168.1.2 -v</strong>  </p>
  
  <p>So for this i added a entry in crontab this way </p>
  
  <p><strong>(crontab -e)</strong></p>
  
  <pre><code>*/5 * * * * /home/memcached/memcached_autostart.sh
  </code></pre>
  
  <p>memcached_autostart.sh</p>
  
  <pre><code>#!/bin/bash
  ps -eaf | grep 11211 | grep memcached
  # if not found - equals to 1, start it
  if [ $? -eq 1 ]
  then
  memcached -u nobody -l 192.168.1.1 -m 2076 -x 192.168.1.2 -v
  else
  echo "eq 0 - memcache running - do nothing"
  fi
  </code></pre>
  
  <p>My question is inside memcached_autostart.sh , for autorestarting the memcached server , is there any problem with the above script ??</p>
  
  <p>Or </p>
  
  <p><strong>If there is any better approach for achieving this (rather than using cron job )</strong> 
  Please share your experience .</p>