msgbartop
Tips, notes, HOWTOs
msgbarbottom

18 Dec 08 Spawning Sub-Shells

Creating sub-shells in bash is simple: just put the commands to be run in the sub-shell inside parentheses. This causes bash to start the commands as a separate process. This group of commands essentially acts like a separate script file, their input/output can be collectively redirected and/or they can be executed in the background by following the closing parenthesis with an ampersand.

#!/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@example.com <<<CRAP

) 2>&1 >> $log_file &

echo "Server started"

quote and example via LinuxJournal

Tags: , , , ,

SEO Powered by Platinum SEO from Techblissonline