AWS Instance with cron job

This example show how to create a cron job that usually require with sudo command. Let’s say I’d like to ensure mysqld instance is restart when the service is stopped.

Root Crontab

[ec2-user@xxxxx ~]$ sudo crontab -e

 

Check Script

* * * * * /home/ec2-user/mysqlcheck.sh

 

MySqlCheck Script

#!/bin/bash
service=mysqld

if (( $(ps -ef | grep -v grep | grep $service | wc -l) > 0 ))
then
echo "$service is running!!!"
else
/etc/init.d/$service start
fi

 

 

 

 

AWS Instance with cron job

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.