Monday, February 23, 2015

Stupid Simple Keep Alive Bash Script

Everyone has servers and processes they want to keep alive and if they are not alive they want to restart them. But they don't want the restarts to get out of control.

This is a stupid simple bash example that will allow you to do that. I'm using it to restart a python notebook.
#!/bin/bash
#This silly script was written by @bentaylordata to handle auto restarting of processes in cron

#Check if string is running
string_of_interest="ipython"
OUTPUT="$(ps -ef | grep $string_of_interest | wc -l)"

if [ "$OUTPUT" -gt "1" ]
then
        echo "started"
else
        echo "need to start"
        ipython notebook --profile=nbserver --no-mathjax &   #This backgrounds the notebook server
fi