Command Breakdown: Background Execution with Stream Redirection
The following command demonstrates several powerful Linux shell features combined:
nohup data-processor -f queries.hql > execution.log 2>&1 &
Component Analysis
1. nohup (No Hang Up)
The nohup utility makes a process immune to SIGHUP signals. When a terminal session terminates—whether through SSH disconnection, terminal closure, or logout—the operating system normally sends SIGHUP to all child processes, causing them to exit. By prefixing a command with nohup, the process ignores this signal and continues running independently of the controlling terminal.
2. data-processor -f queries.hql
This represents the target application and its arguments. The -f flag instructs the program to read input from a file—in this case, queries.hql, which presumably contains HiveQL statements for batch processing.
3. > execution.log
The single-angle bracket redirects standard output (stdout, file descriptor 1) from the command to execution.log. This captures all normal program output that would otherwise display in the terminal.
4. 2>&1
This construct merges standard error (stderr, file descriptor 2) with standard output. The &1 syntax references the current destination of stdout, ensuring both output streams are written to the same file. Without this, errors would still appear in the terminal or be lost when the session ends.
5. &
The trailing apmersand places the entire command pipeline into background execution. The shell immediately returns control, allowing continued interactive use while the process runs in a subshell. The combination with nohup ensures the background process survives terminal termination.
Practical Redirection Patterns
Stream Consolidation and Splitting
To display output on screen while simultaneously logging it:
myapp -c config.yaml 2>&1 | tee combined-output.log
For seperate log files without mixing streams:
myapp > stdout.log 2> stderr.log
Error Suppression and Filtering
To discard errors entirely while preserving standard output:
critical-task > results.txt 2>/dev/null
To capture only specific error patterns for alerting:
batch-job 2>&1 >/dev/null | grep -i "fatal\|critical" | alert-monitor
Advanced File Descriptor Manipulation
Swapping stdout and stderr using a temporary descriptor:
complex-operation 3>&1 1>&2 2>&3
Dynamically processing error streams with process substitution:
data-loader 2> >(error-parser --format=json > error-report.json)
Script-Wide Redirection
Using exec to apply redirection to all subsequent commands in a script:
#!/bin/bash
exec 1>>application.log
exec 2>>error-capture.log
process-data --input=dataset1.csv
validate-results --threshold=0.95
generate-report --type=summary
Debugging with Execution Tracing
Enable verbose mode to see each command and its redirection behavior:
set -x
pipeline-stage1 | pipeline-stage2 > final-output.log 2>&1
set +x
Conditional Logging Based on Exit Status
Capture output only when a command fails:
if ! deployment-script > deploy.log 2>&1; then
cat deploy.log | mail -s "Deployment Failed" ops-team@example.com
fi