nohup
Overview
The nohup
command runs commands immune to hangups, allowing processes to continue running even after the user logs out or the terminal is closed.
Syntax
nohup command [arguments] &
nohup command [arguments] > output.log 2>&1 &
Key Features
Feature | Description |
---|---|
Hangup immunity | Process survives terminal closure |
Background execution | Runs in background with & |
Output redirection | Saves output to nohup.out |
Signal handling | Ignores SIGHUP signal |
Session independence | Detaches from controlling terminal |
Default Behavior
- Redirects stdout to
nohup.out
- Redirects stderr to stdout
- Ignores SIGHUP signal
- Process continues after logout
Key Use Cases
- Long-running scripts
- Background services
- Remote command execution
- Batch processing
- Server maintenance tasks
Examples with Explanations
Example 1: Basic Usage
nohup ./long_script.sh &
Runs script in background, immune to hangups
Example 2: Custom Output File
nohup python script.py > script.log 2>&1 &
Redirects all output to custom log file
Example 3: Multiple Commands
nohup bash -c 'command1 && command2 && command3' &
Runs sequence of commands with nohup
Output Redirection
Redirection | Description |
---|---|
> file |
Redirect stdout to file |
2>&1 |
Redirect stderr to stdout |
> /dev/null 2>&1 |
Discard all output |
>> file |
Append to file |
2> error.log |
Redirect stderr to file |
Common Usage Patterns
Silent background execution:
nohup command > /dev/null 2>&1 &
With custom log:
nohup ./script.sh > script.log 2>&1 &
Get process ID:
nohup command & echo $! > pid.txt
Process Management
Check running processes:
ps aux | grep script_name
Kill nohup process:
kill $(cat pid.txt)
Monitor output:
tail -f nohup.out
Signal Handling
Signals and nohup: - SIGHUP: Ignored by nohup - SIGTERM: Can still terminate process - SIGKILL: Force kills process - SIGINT: Usually ignored in background
Performance Analysis
- Minimal overhead
- No performance impact on command
- Efficient for long-running tasks
- Good for resource-intensive operations
- Suitable for batch processing
Additional Resources
Best Practices
- Always use & for background execution
- Redirect output to avoid nohup.out clutter
- Save process IDs for management
- Monitor long-running processes
- Use appropriate log rotation
Advanced Usage
With environment variables:
nohup env VAR=value command &
Conditional execution:
nohup bash -c 'if condition; then command; fi' &
With timeout:
nohup timeout 3600 command &
Scripting Examples
Backup script:
#!/bin/bash nohup rsync -av /data/ /backup/ > backup.log 2>&1 & echo $! > backup.pid
Service starter:
start_service() { nohup ./service > service.log 2>&1 & echo $! > service.pid }
Batch processor:
nohup find /data -name "*.txt" -exec process_file {} \; &
Monitoring and Control
Check if process is running:
if ps -p $(cat pid.txt) > /dev/null; then echo "Process running" fi
Monitor resource usage:
top -p $(cat pid.txt)
Follow log output:
tail -f nohup.out
Common Pitfalls
- Forgetting the & symbol
- Not redirecting output properly
- Losing track of process IDs
- Not monitoring disk space for logs
- Assuming process will always run
Integration Examples
With cron for scheduled tasks:
0 2 * * * nohup /path/to/script.sh > /var/log/script.log 2>&1 &
SSH remote execution:
ssh user@server 'nohup ./remote_script.sh > script.log 2>&1 &'
Service management:
nohup java -jar application.jar > app.log 2>&1 &
Alternatives Comparison
Tool | Use Case |
---|---|
nohup |
Simple background execution |
screen |
Interactive session management |
tmux |
Advanced terminal multiplexing |
systemd |
System service management |
supervisor |
Process supervision |
Troubleshooting
- Process not starting
- Output not being captured
- Process dying unexpectedly
- Permission issues
- Resource limitations
Security Considerations
- Log file permissions
- Process ownership
- Resource consumption
- Output sensitive data
- Process monitoring