#!/bin/bash CHROOT_NAME="webkit" SESSION_NAME="$CHROOT_NAME-session" SESSIONS=0 SESSIONS_FILE="/tmp/$SESSION_NAME-counter" function write_sessions { echo "$SESSIONS" > "/tmp/$SESSION_NAME-counter" } function read_sessions { if [ ! -f "$SESSIONS_FILE" ] then write_sessions fi read SESSIONS < "/tmp/$SESSION_NAME-counter" } read_sessions ((SESSIONS++)) write_sessions # Create session if this is the first one if [ $SESSIONS -eq 1 ] then echo "This is the first chroot, creating new session" schroot -c $CHROOT_NAME -n $SESSION_NAME -b fi # Enter chroot session schroot -c $SESSION_NAME -r read_sessions ((SESSIONS--)) write_sessions # End session if there are no other ones remaining if [ $SESSIONS -eq 0 ] then echo "This is the last chroot, deleting session" schroot -c $SESSION_NAME -e fi