Simple Tasks
Simple tasks are the foundation of Xec automation. They encapsulate single commands or basic operations that can be executed repeatedly across different environments.
Command Tasksβ
The simplest form of task is a single command:
tasks:
# Inline command
hello: echo "Hello, World!"
# Command with description
backup:
command: tar -czf backup.tar.gz /data
description: Create backup archive
Task Propertiesβ
Basic Propertiesβ
tasks:
example:
# Required (one of these)
command: echo "Command to execute"
# OR
script: |
console.log("JavaScript code");
# Optional metadata
description: Task description
target: hosts.production
workdir: /app
timeout: 30000
Complete Exampleβ
tasks:
deploy-static:
command: rsync -av ./dist/ /var/www/html/
description: Deploy static files to web server
target: hosts.web-server
workdir: /home/deploy/project
timeout: 60000
Target Specificationβ
Single Targetβ
tasks:
check-disk:
command: df -h
target: hosts.backup-server
Multiple Targetsβ
tasks:
check-all:
command: uptime
targets:
- hosts.web-1
- hosts.web-2
- hosts.db-server
Dynamic Targetsβ
tasks:
update-all:
command: apt-get update
targets: "hosts.*" # All hosts
Environment Variablesβ
Set environment for task execution:
tasks:
build:
command: npm run build
env:
NODE_ENV: production
API_URL: https://api.example.com
BUILD_NUMBER: ${env.CI_BUILD_NUMBER}
Working Directoryβ
Control where commands execute:
tasks:
compile:
command: make all
workdir: /project/src
# Relative to target's workdir
relative:
command: ./scripts/deploy.sh
target: hosts.build-server
Timeout Configurationβ
Prevent hanging tasks:
tasks:
quick:
command: ping -c 1 google.com
timeout: 5000 # 5 seconds
long-running:
command: ./backup-database.sh
timeout: 3600000 # 1 hour
no-timeout:
command: tail -f /var/log/app.log
timeout: 0 # No timeout
Shell Configurationβ
Control shell behavior:
tasks:
# Default (use shell)
with-shell:
command: echo $HOME && ls -la
# No shell (direct execution)
no-shell:
command: ["/usr/bin/python3", "script.py"]
shell: false
# Custom shell
custom-shell:
command: echo $0
shell: /bin/zsh
Output Handlingβ
Capture Outputβ
tasks:
get-version:
command: cat /etc/version
register: version_info
use-output:
command: echo "Version is ${version_info.stdout}"
Silent Executionβ
tasks:
quiet:
command: ./noisy-script.sh
silent: true # Suppress output
Error Handlingβ
Basic Error Controlβ
tasks:
# Fail on error (default)
strict:
command: test -f /important/file
throwOnNonZeroExit: true
# Continue on error
lenient:
command: rm -f /tmp/maybe-exists
throwOnNonZeroExit: false
Exit Code Handlingβ
tasks:
check-service:
command: systemctl is-active nginx
acceptExitCodes: [0, 3] # 0=active, 3=inactive
Common Patternsβ
System Informationβ
tasks:
system-info:
command: |
echo "=== System Information ==="
uname -a
echo "=== CPU Info ==="
lscpu | head -10
echo "=== Memory Info ==="
free -h
echo "=== Disk Usage ==="
df -h
description: Gather system information
Service Managementβ
tasks:
restart-nginx:
command: systemctl restart nginx
target: hosts.web-server
description: Restart Nginx web server
check-nginx:
command: systemctl status nginx
target: hosts.web-server
description: Check Nginx status
File Operationsβ
tasks:
create-backup:
command: |
BACKUP_FILE="/backup/data-$(date +%Y%m%d).tar.gz"
tar -czf "$BACKUP_FILE" /var/data/
echo "Backup created: $BACKUP_FILE"
target: hosts.backup-server
cleanup-old:
command: find /backup -name "*.tar.gz" -mtime +30 -delete
description: Remove backups older than 30 days
Git Operationsβ
tasks:
git-pull:
command: git pull origin main
workdir: /var/www/app
git-status:
command: git status --short
workdir: /var/www/app
git-reset:
command: git reset --hard origin/main
workdir: /var/www/app
description: Reset to remote main branch
Docker Operationsβ
tasks:
docker-cleanup:
command: docker system prune -af
description: Clean up unused Docker resources
docker-logs:
command: docker logs --tail 100 -f app-container
target: containers.app
docker-restart:
command: docker restart app-container
target: hosts.docker-host
Database Operationsβ
tasks:
db-backup:
command: |
mysqldump -u root -p${secrets.db_password} \
production > /backup/db-$(date +%Y%m%d).sql
target: hosts.db-server
timeout: 600000
db-optimize:
command: mysqlcheck -o --all-databases
target: hosts.db-server
db-connections:
command: |
mysql -e "SHOW PROCESSLIST" | wc -l
target: hosts.db-server
description: Count active database connections
Task Executionβ
Command Lineβ
# Run task
xec run hello
# Run on specific target
xec run backup --target hosts.backup-server
# Override timeout
xec run long-task --timeout 120000
From Scriptsβ
// In xec scripts
await xec.run('deploy-static');
// With options
await xec.run('backup', {
target: 'hosts.backup-server',
timeout: 60000
});
From Other Tasksβ
tasks:
prepare:
command: echo "Preparing..."
main:
steps:
- task: prepare
- command: echo "Main task"
Best Practicesβ
1. Use Descriptive Namesβ
# Good
tasks:
backup-database:
command: pg_dump production > backup.sql
# Bad
tasks:
bd: # Unclear abbreviation
command: pg_dump production > backup.sql
2. Add Descriptionsβ
tasks:
migrate-data:
command: ./migrate.sh
description: |
Migrate user data from v1 to v2 schema.
WARNING: This modifies production data.
Duration: ~10 minutes
3. Set Appropriate Timeoutsβ
tasks:
health-check:
command: curl http://localhost/health
timeout: 5000 # Quick check
data-processing:
command: ./process-large-dataset.sh
timeout: 3600000 # Long operation
4. Handle Errors Gracefullyβ
tasks:
safe-cleanup:
command: rm -f /tmp/tempfile
throwOnNonZeroExit: false # OK if file doesn't exist
5. Use Environment Variablesβ
tasks:
flexible:
command: deploy --env ${DEPLOY_ENV:-staging}
env:
DEPLOY_ENV: ${params.environment}
Debugging Tasksβ
Test Executionβ
# Dry run
xec run deploy --dry-run
# Verbose output
xec run deploy --verbose
# Debug mode
xec run deploy --debug
Task Informationβ
# Show task details
xec task info deploy
# List all tasks
xec task list
# Search tasks
xec task search backup
Common Issuesβ
Command Not Foundβ
# Ensure command exists on target
tasks:
fixed:
command: /usr/local/bin/custom-tool # Use full path
# OR
command: custom-tool
env:
PATH: /usr/local/bin:$PATH
Permission Deniedβ
# Use appropriate permissions
tasks:
privileged:
command: sudo systemctl restart nginx
target: hosts.server
Working Directory Issuesβ
# Ensure directory exists
tasks:
safe:
command: |
mkdir -p /app/logs
cd /app/logs
touch app.log
Next Stepsβ
- Multi-Step Tasks - Complex workflows
- Task Overview - Task management concepts
See Alsoβ
- Task Command - Running tasks
- Command Execution - Direct command execution
- Best Practices - Task design patterns