I am using below script to monitor some processes on linux and restart if process is down and send email.

I want to add below improvements in this script, I need help in that case. Can anyone help with examples

  • Seconds to wait between attempts to restart service
  • Number of attempts before giving up
  • Check interval in seconds
  • Generate logs in case of events.

Current script:

###edit the following
export smtp=smtprelay.domainname.com:25
###You can provide multiple mail ID's above in email variable separated by commas
###stop editing

host=`hostname -f`
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 2 ))
echo "$service is running"
/opt/splunk/bin/$service restart
if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 2 ))
subject="$service at $host has been started"
echo "$service at $host wasn't running and has been started" | mailx -s "$subject" $email
subject="$service at $host is not running"
echo "$service at $host is stopped and cannot be started!!!" | mailx -s "$subject" $email
  • If you use the fresh centos use the out-of-box systemd functionality. If you use the old centos, use the monit or something like. – Anton Danilov Apr 18 '19 at 11:49
  1. You need several variables for the attempts counter and the timeouts.
  2. Write these values into the some file in /etc/sysconfig/ directory (/etc/default/ in other distros) and include it into your script.
  3. Use sleep to wait a timeout.
  4. Use logger utility to write data into syslog.
  5. Most complex part is the start of service and abort the start if timeout expires. Better use daemon() function (start-stop-daemon utility in other distros).

Not the answer you're looking for? Browse other questions tagged or ask your own question.