Check and start a process in Linux
Over the past few days, we realised that, for various reasons, our Sphinx search process (searchd) would occasionally not be running. As Sphinx is pretty much the backbone of our sites, we couldn't have this happening. I wrote a small script that runs permanently, checks if searchd.pid exists (you can have it check anything) and if not, then starts searchd. This is all done in a screen session so I can just leave it running.
The script is very, very simple;
while [ "1 -eq 1" ]
do
if [ ! -e "/var/log/searchd.pid" ]
then
searchd
fi
sleep 2
done
Basically, we have an infinite loop, we check that /var/log/search.pid exists, if not, start it, then sleep ('pause' essentially) for 2 seconds before starting again. You can change it to alert you too, if you'd like.
To get an idea of how screen works, check out this post.
Thought it might help some of you.