# Long-running / overnight agent runs — keep-awake + detachable tmux + logged output. # Full guide: AI.dev/CHEATSHEETS/long-running-jobs.md (in learning_ai_common_plat). # macOS: keep the machine awake while a command runs (prevents sleep stalling the job). # On Linux this alias is a no-op label; use `systemd-inhibit` instead. alias awake='caffeinate -dimsu' # longrun [args...] # Runs in a DETACHED tmux session, wrapped in caffeinate (macOS) so the # machine won't sleep, teeing all output to ~/longrun--.log. # Survives closing the terminal; reattach with `ta `, stop with # `tmux kill-session -t `. # e.g. longrun phase3 codex --dangerously-bypass-approvals-and-sandbox "Read ... and execute it" longrun() { if [ "$#" -lt 2 ]; then echo "usage: longrun [args...]" >&2 echo " e.g. longrun phase3 codex --full-auto \"\"" >&2 return 2 fi command -v tmux >/dev/null 2>&1 || { echo "longrun: tmux is not installed" >&2; return 1; } local sess="$1"; shift local ts log keep cmd inner ts="$(date +%Y%m%d-%H%M%S)" log="$HOME/longrun-${sess}-${ts}.log" keep="" command -v caffeinate >/dev/null 2>&1 && keep="caffeinate -dimsu " cmd="$(printf '%q ' "$@")" inner="${keep}${cmd}2>&1 | tee \"$log\"" tmux new-session -d -s "$sess" "$inner" echo "[longrun] session=$sess" echo "[longrun] log=$log" echo "[longrun] attach: ta $sess | tail: tail -f \"$log\" | stop: tmux kill-session -t $sess" }