Cron to Restart HA

Has anyone implemented a script that can be run via cron that will restart HA after suddenly stopping?

My setup generally runs flawlessly however occasionally it just stops for what I call no apparent reason (or at least I haven’t been able to figure out why). As luck would have it for some reason it typically does this late at night. I’ll wake to find my automation’s not working. I generally can get 1 to 2 weeks maybe before this happens and HA fails. On occasion it will do it more often. I realize that the proper approach is to determine why it stops but unfortunately I haven’t been able to figure that out. Since it seems to always happen at a inopportune time I was hoping this would be an acceptable work around.

My setup is on a RPi2 and I would like to setup a cron job that checks to see if the home-assistant service is running and if not fire it back up. Ideally I would have this check every hour. I actually found this information but before I attempted to try this I wanted to see if anyone already has something in place they are using and if they could share that information. I would also be interested in anyone has any other thoughts.

#!/bin/bash
service=replace_me_with_a_valid_service

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

I suspect the service would be “sudo systemctl start home-assistant.service”

Thanks!

This may be dumb, but why not figure out what it is that’s stopping HASS and fix that?

I know my setup isn’t that great or complex but in 3 weeks I haven’t had it just stop for no reason at all.

I agree that would be the ideal solution however unfortunately I haven’t been able to decipher the issue and my knowledge on making that determination only goes so far. When HA fails it is very random and there hasn’t been anything I can point to that seems to be a common thread. I wouldn’t consider my setup to be overly complex either. I can get long periods of HA running without a problem and then there are times it can occur a couple of times in a week. When I restart HA it generally runs good for awhile. I thought this might be a useful solution for those times when it fails and I am not available to restart it right away.