Website monitor - if site is down, kill the process, contact admin and restart server
Indent code blocks

To improve readability,
it's good to indent code blocks in loops and if statements.
For example:

while true
do
    /usr/bin/curl -sSf --max-time 10 $SERVER &gt; /dev/null 2&gt;&amp;1
    CS=$?


You indented some if statements, but not all. Indent all.

Replace elif with else if possible

In these conditions:


    if [ $CS -ne 0 ]
    then
    echo "$SERVER is down"

    elif [ $CS -eq 0 ]
    then
    echo "$SERVER is up"



If $CS is not-not-zero, then when can it be? Zero, of course!
So the elif can be a simple else.

Use $(...) instead of `...`

`...` is old-fashioned and error prone. Use $(...) instead.

ps ... | grep ... | grep -v grep

Instead of:


ps -eaf | grep 'python -m fore.server' | grep -v grep



A common workaround is:


ps -eaf | grep '[p]ython -m fore.server'



This works, because this way in the output of ps, the process python -m fore.server will still match, but grep '[p]ython -m fore.server' will not match.

However, in this particular example you should go a bit further, see the next point.

Look suspiciously at grep in a pipeline ending with awk

awk is fast and powerful,
and very often can easily do grep's job too when it's in the same pipeline.

For example instead of this:


ps -eaf | grep 'python -m fore.server' | grep -v grep | awk '{print $2}'



You can rewrite the whole thing using awk alone:

ps -eaf | awk '/python -m fore.server/ {print $2}'


This is not only shorter and less troublesome (no need for the grep [p]ython trick),
and there are less processes executed in the pipeline.

Don't repeat yourself

You do some things twice, with identical or very similar commands:


Finding the PID of the Python process
Sending an email with the message that the server is down
Sending an email with the message that the server is back up


It would be better to move common operations to helper functions.

Other simplifications

Instead of this:


    if [[ "" !=  "$PID" ]]



You can write as:

    if [[ $PID ]]