Bash Sub Shells

Taken from here.

#!/bin/bash

server_cmd=server
pid_file=$(basename $server_cmd .sh).pid
log_file=$(basename $server_cmd .sh).log

(
    echo \"Starting server\"
    echo \"Doing some init work\"
    $server_cmd   # server becomes a daemon

    while true
    do
        if [[ -f $pid_file ]]; then
            sleep 15
        else
            break
        fi
    done
    mail -s \"Server exitted\" joe@blow.com <<<CRAP

) 2>&1 >> $log_file &

echo \"Server started\"

Now this was something which is not new that you can run multiple commands together with \”()\” or that you can run a command in the background with \”&\”, but putting it together in this fashion is really good. I am impressed.

About Amit Agarwal