File size: 3,075 Bytes
3ccaf5a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!/usr/bin/env bash
###############################################################################
# StepProbe — Background Runner
#
# Launches the full pipeline in the background with:
#   - nohup for persistence after SSH disconnect
#   - tmux session for easy re-attach
#   - Email notification on completion (optional)
#
# Usage:
#   bash scripts/run_background.sh                # default full run
#   bash scripts/run_background.sh --quick        # quick test
#   bash scripts/run_background.sh --tmux         # use tmux instead of nohup
#   bash scripts/run_background.sh --notify email # send email on completion
###############################################################################
set -euo pipefail

PROJECT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
LOGS_DIR="${PROJECT_DIR}/logs"
mkdir -p "$LOGS_DIR"

TIMESTAMP=$(date +%Y%m%d_%H%M%S)
LOG_FILE="${LOGS_DIR}/background_${TIMESTAMP}.log"
PID_FILE="${LOGS_DIR}/run.pid"

MODE="nohup"
EXTRA_ARGS=""
NOTIFY_EMAIL=""

while [[ $# -gt 0 ]]; do
    case $1 in
        --tmux)     MODE="tmux"; shift ;;
        --notify)   NOTIFY_EMAIL=$2; shift 2 ;;
        *)          EXTRA_ARGS="$EXTRA_ARGS $1"; shift ;;
    esac
done

# Build the command
CMD="cd ${PROJECT_DIR} && bash run_all.sh ${EXTRA_ARGS}"

# Optional notification on completion
if [[ -n "$NOTIFY_EMAIL" ]]; then
    CMD="${CMD} ; echo 'StepProbe experiment completed at \$(date)' | mail -s 'StepProbe Done' ${NOTIFY_EMAIL}"
fi

if [[ "$MODE" == "tmux" ]]; then
    # Check tmux
    if ! command -v tmux &>/dev/null; then
        echo "tmux not found. Install with: sudo apt install tmux"
        echo "Falling back to nohup."
        MODE="nohup"
    fi
fi

case $MODE in
    tmux)
        SESSION="stepprobe"
        # Kill existing session if any
        tmux kill-session -t "$SESSION" 2>/dev/null || true
        # Create new session
        tmux new-session -d -s "$SESSION" "$CMD 2>&1 | tee $LOG_FILE"
        echo "=================================================================="
        echo "  StepProbe running in tmux session: $SESSION"
        echo ""
        echo "  Attach:   tmux attach -t $SESSION"
        echo "  Detach:   Ctrl+B then D"
        echo "  Kill:     tmux kill-session -t $SESSION"
        echo "  Monitor:  bash scripts/monitor.sh --watch"
        echo "  Log:      tail -f $LOG_FILE"
        echo "=================================================================="
        ;;

    nohup)
        # Launch in background
        nohup bash -c "$CMD" > "$LOG_FILE" 2>&1 &
        local_pid=$!
        echo "$local_pid" > "$PID_FILE"

        echo "=================================================================="
        echo "  StepProbe running in background"
        echo ""
        echo "  PID:      $local_pid"
        echo "  Log:      tail -f $LOG_FILE"
        echo "  Monitor:  bash scripts/monitor.sh --watch"
        echo "  Status:   ps aux | grep $local_pid"
        echo "  Stop:     kill $local_pid"
        echo "=================================================================="
        ;;
esac