Sunday, June 7, 2015

RaspberryPi Crontab run bash script for python in every 30 min

I wrote a bash script to check python program is running. If it's not running send me an email and start that program. After a quick research I found the solution:

creating Bash Script:
----> checkProgram.sh

#!/bin/sh
SERVICE='python'
 
if ps ax | grep -v grep | grep $SERVICE > /dev/null
then
    echo "$SERVICE service running, everything is fine"
else
    #echo "$SERVICE is not running"
    python /home/pi/receive.py &
    echo "$SERVICE is not running!" | mail -s "$SERVICE down" root
fi

CronTab:

-> crontab -e
-> */15 * * * * /home/pi/checkProg.sh
-> /etc/init.d/cron restart
This will run the program for every 15 min.


How to set RaspberryPi Send Mail

It was long day of working with Pi and I want to setup a email server where if anything happen with Pi(ya! right...) , I will get an email. After few research I found nice and easy (Very easy, only took 2 hour!) solution.

Go to : /etc/ssmtp/ssmtp.conf 
Save and Exit...

root=me@gmail.com
mailhub=smtp.gmail.com:465
hostname=raspberrypi
FromLineOverride=YES
AuthUser=me@gmail.com
AuthPass=mypass
UseSTARTTLS=YES

Test email echo "Hello world email body" | mail -s "Test Subject" recipientname@domain.com

It should work :)