Lifecycle Hooks
Overview
Laio provides lifecycle hooks to run commands and scripts when sessions start or stop. This is useful for:
- Starting/stopping services (Docker, databases, etc.)
- Setting up environment state
- Cleaning up resources
- Initializing development tools
Startup Hooks
Startup Commands
Commands that run before the session is created:
startup:
- command: docker-compose
args:
- up
- -d
- command: npm
args:
- run
- db:migrate
Commands execute sequentially in the order defined.
Startup Script
A script that runs after startup commands:
startup_script: |
#!/usr/bin/env bash
echo "Initializing session..."
sleep 2
curl http://localhost:8000/health
Requirements:
- Must include a shebang line (e.g.,
#!/usr/bin/env bash) - Runs after all
startupcommands complete - Blocks session creation until complete
Shutdown Hooks
Shutdown Commands
Commands that run when the session stops:
shutdown:
- command: docker-compose
args:
- down
- command: echo
args:
- "Session stopped"Shutdown Script
A script that runs after shutdown commands:
shutdown_script: |
#!/usr/bin/env bash
echo "Cleaning up..."
rm -rf /tmp/session-*
echo "Goodbye!"
Requirements:
- Must include a shebang line
- Runs after all
shutdowncommands complete
Execution Order
On Session Start
startupcommands execute (sequential)startup_scriptexecutes- Windows and panes are created
- Pane-level commands and scripts execute
- Session is ready
On Session Stop
shutdowncommands execute (sequential)shutdown_scriptexecutes- Panes are destroyed
- Session is terminated
Skipping Lifecycle Hooks
Skip startup hooks:
laio start myproject --skip-cmds
Skip shutdown hooks:
laio stop myproject --skip-cmdsPane-Level Scripts
In addition to session-level hooks, panes can have their own scripts:
windows:
- name: logs
panes:
- commands:
- command: docker-compose
args: [up, -d]
script: |
#!/usr/bin/env bash
sleep 3
docker-compose logs -f app
Pane scripts run after pane commands complete.
Pane Command Execution
On tmux, pane commands are batched into a single send-keys invocation. On zellij, all pane commands plus the pane script run in one shell invocation: the pane node gets command="<shell>" with a child args node containing -c and the joined commands (<cmd1>; <cmd2>), plus close_on_exit true and start_suspended false. The wrapper shell resolves from the session shell, then the login shell, then $SHELL, then sh. On herdr, each command is sent to the pane individually. On rex, each pane's commands and its script are joined and sent to the pane's block in one go.
windows:
- name: dev
panes:
- commands:
- command: npm
args: [install]
- command: npm
args: [run, dev]
On tmux these two commands are batched: tmux send-keys -t pane "npm install" C-m "npm run dev" C-m
Nushell Compatibility
If you're using Nushell as your shell on tmux or herdr, you may need to add a delay before pane commands are sent. Nushell requires shells to fully initialize before receiving batched commands:
name: my-nushell-session
shell: /path/to/nu
# Add delay for Nushell (milliseconds)
pane_cmd_delay: 500
windows:
- name: dev
panes:
- commands:
- command: cd
args: [src]
- command: cargo
args: [watch]
When to use pane_cmd_delay:
- Required for Nushell users (recommended: 500ms)
- Not needed for bash/zsh (they handle batched commands correctly)
- Only affects pane-level commands (not startup/shutdown commands)
- No effect on zellij, which runs all pane commands in one shell invocation (
pane_cmd_delayis ignored)
Note: On tmux the delay is applied once after all panes are created. On herdr it is applied once before the first pane command is sent. On rex it is applied once before the first pane command is sent. On zellij pane_cmd_delay is ignored.
rex (experimental)
rex runs the whole session in one rex session named after the config: laio recognizes its rex sessions by the sidecar store, so labels are plain names. Lifecycle hooks run in the session path, without the session env: rex has no environment store, so env is ignored. Pane style is ignored as well. Attach runs bare rex do session.select session_id=<full session:-prefixed id>; Remote Control must be enabled in Rex.app under Settings > Rex Server, and a failing attach only logs a warning, so the start still succeeds. Session records live in LAIO_STATE_DIR when set, otherwise $XDG_STATE_HOME/laio/rex, otherwise ~/.local/state/laio/rex. laio session yaml loses pane commands and ignores floating layers.
Common Patterns
Docker Development
startup:
- command: docker-compose
args: [up, -d]
startup_script: |
#!/usr/bin/env bash
# Wait for services to be healthy
timeout 30 bash -c 'until docker-compose exec db pg_isready; do sleep 1; done'
echo "Database ready!"
shutdown:
- command: docker-compose
args: [down]Service Health Checks
startup_script: |
#!/usr/bin/env bash
check_service() {
until curl -s http://localhost:$1/health > /dev/null; do
echo "Waiting for service on port $1..."
sleep 1
done
echo "Service on port $1 is ready!"
}
check_service 8000
check_service 3000Environment Setup
startup_script: |
#!/usr/bin/env bash
# Load environment secrets
if [ -f .env.local ]; then
export $(cat .env.local | xargs)
fi
# Initialize tools
direnv allow
echo "Environment ready!"Cleanup on Exit
shutdown_script: |
#!/usr/bin/env bash
# Clean temporary files
rm -rf tmp/*
rm -rf .cache/*
# Reset git state (optional)
git checkout -- .
echo "Cleanup complete!"Error Handling
If a startup command or script fails:
- Session creation is aborted
- Any resources created are cleaned up
- Error message is displayed
If a shutdown command or script fails:
- An error is displayed and laio exits non-zero
- Session termination continues
- Resources may not be fully cleaned
Best Practices
- Keep scripts idempotent - Safe to run multiple times
- Add timeouts - Prevent hanging on failed services
- Check for dependencies - Verify tools exist before using them
- Use absolute paths - Scripts run from session
path - Test separately - Run scripts manually before using in configs
- Log output - Echo progress for debugging
Example: Full Lifecycle
name: api-development
path: ~/projects/my-api
startup:
- command: docker-compose
args: [up, -d, postgres, redis]
startup_script: |
#!/usr/bin/env bash
set -e
echo "Waiting for database..."
timeout 30 bash -c 'until docker-compose exec -T postgres pg_isready; do sleep 1; done'
echo "Running migrations..."
npm run db:migrate
echo "Seeding test data..."
npm run db:seed
echo "API environment ready!"
shutdown:
- command: docker-compose
args: [stop]
shutdown_script: |
#!/usr/bin/env bash
echo "Backing up logs..."
cp logs/app.log logs/app.log.$(date +%Y%m%d-%H%M%S)
echo "Stopping containers..."
docker-compose down
echo "Session ended."
windows:
- name: api
panes:
- commands:
- command: npm
args: [run, dev]