
nohup Command in Linux – Keep Processes Running After Logout
The nohup command is one of those essential Linux utilities that every developer and sysadmin should know, especially when working with remote servers and long-running processes. It allows you to run commands that continue executing even after you log out or close your terminal session. If you’ve ever started a lengthy script or deployment process only to lose it when your SSH connection dropped, nohup is your solution. This guide covers everything you need to know about using nohup effectively, including practical examples, troubleshooting common issues, and comparing it with alternative approaches.
How nohup Works Under the Hood
When you run a process in a terminal, it becomes part of that terminal’s process group and receives a SIGHUP (hangup signal) when the parent terminal closes. This signal typically terminates the process. The nohup command works by:
- Ignoring the SIGHUP signal for the specified command
- Redirecting stdout and stderr to a file called nohup.out (by default)
- Allowing the process to continue running as an orphaned process
- Making the process immune to terminal disconnections
The basic syntax is straightforward:
nohup command [arguments] &
The ampersand (&) at the end runs the process in the background, though it’s not strictly required for nohup to work.
Step-by-Step Implementation Guide
Let’s start with basic usage and work up to more complex scenarios.
Basic nohup Usage
Running a simple long-running command:
# Basic command that will survive logout
nohup python3 long_running_script.py &
# Check the process is running
ps aux | grep python3
# View output (redirected to nohup.out by default)
tail -f nohup.out
Custom Output Redirection
By default, nohup redirects output to nohup.out, but you can specify custom files:
# Redirect to custom file
nohup ./backup_script.sh > backup.log 2>&1 &
# Separate stdout and stderr
nohup python3 app.py > app.log 2> error.log &
# Discard output entirely
nohup ./process.sh > /dev/null 2>&1 &
Managing Multiple nohup Processes
When running multiple processes, it’s crucial to track them properly:
# Start multiple processes with descriptive names
nohup python3 data_processor.py &
echo $! > data_processor.pid
nohup ./web_scraper.sh &
echo $! > web_scraper.pid
# Check all background jobs
jobs -l
# Kill specific process using saved PID
kill $(cat data_processor.pid)
Real-World Examples and Use Cases
Database Backup Automation
Running database backups that shouldn’t be interrupted:
# PostgreSQL backup that runs overnight
nohup pg_dump -h localhost -U backup_user -d production_db \
--compress=9 --file=backup_$(date +%Y%m%d).sql.gz \
> backup.log 2>&1 &
# MySQL backup with rotation
nohup mysqldump --single-transaction --routines --triggers \
--all-databases | gzip > mysql_backup_$(date +%Y%m%d).sql.gz &
Web Application Deployment
Starting web servers that need to persist:
# Node.js application
nohup node server.js > app.log 2>&1 &
# Python Flask/Django development server
nohup python3 manage.py runserver 0.0.0.0:8000 > django.log 2>&1 &
# Custom API server with proper logging
nohup ./api_server --port=3000 --env=production \
> /var/log/api_server.log 2>&1 &
Data Processing and Analysis
Long-running data processing tasks:
# Machine learning training
nohup python3 train_model.py --epochs=1000 --dataset=large.csv \
> training.log 2>&1 &
# Large file processing
nohup awk 'BEGIN{FS=","} {sum+=$3} END{print sum}' huge_dataset.csv \
> processing_result.txt &
Monitoring and Managing nohup Processes
Effective process management is crucial when using nohup:
# Find all processes started with nohup
ps aux | grep -v grep | grep nohup
# More detailed process information
ps -eo pid,ppid,cmd,etime | grep your_process_name
# Monitor resource usage
top -p $(pgrep -f "your_process_name")
# Check if process is still running
if ps -p $PID > /dev/null; then
echo "Process is running"
else
echo "Process has stopped"
fi
Comparison with Alternative Approaches
Method | Pros | Cons | Best Use Case |
---|---|---|---|
nohup | Simple, lightweight, built-in | Basic process management, no advanced features | Simple scripts and one-off tasks |
screen | Full terminal session, can reattach | More resource-intensive | Interactive debugging and development |
tmux | Advanced session management, multiplexing | Learning curve, overkill for simple tasks | Complex development workflows |
systemd | Proper service management, auto-restart | Requires root access, more complex setup | Production services and daemons |
Docker | Isolation, portability, resource control | Overhead, requires containerization knowledge | Microservices and scalable applications |
When to Use Each Alternative
Use screen when:
# Start a screen session
screen -S myapp
python3 long_running_app.py
# Detach: Ctrl+A, then D
# Reattach later: screen -r myapp
Use systemd for production services:
# Create service file: /etc/systemd/system/myapp.service
[Unit]
Description=My Application
After=network.target
[Service]
Type=simple
User=appuser
ExecStart=/usr/bin/python3 /opt/myapp/server.py
Restart=always
[Install]
WantedBy=multi-user.target
# Enable and start
sudo systemctl enable myapp
sudo systemctl start myapp
Common Issues and Troubleshooting
Process Dies Despite nohup
Sometimes processes still terminate unexpectedly:
# Check system logs for clues
journalctl -u your-service-name
dmesg | tail -20
# Monitor system resources
free -h
df -h
ps aux --sort=-%cpu | head -10
Common causes and solutions:
- Out of memory: The OOM killer may terminate your process
- Disk space: Process fails when trying to write output
- Permissions: Process loses access to required files
- Dependencies: Required services or databases become unavailable
Output Redirection Problems
Handle output redirection issues:
# Check if nohup.out is being created
ls -la nohup.out
# Verify write permissions
ls -la $(dirname $(pwd))/
# Force output to specific location
nohup command > /tmp/output.log 2>&1 &
# Monitor output in real-time
tail -f /tmp/output.log
Process Management Issues
Finding and controlling orphaned processes:
# Find processes without a controlling terminal
ps -eo pid,ppid,sid,tty,cmd | awk '$4=="?" {print}'
# Find all background processes started by your user
ps -u $(whoami) -o pid,cmd --no-headers
# Gracefully terminate stuck processes
kill -TERM $PID
sleep 5
kill -KILL $PID
Best Practices and Advanced Techniques
Process Monitoring and Logging
Implement proper logging and monitoring:
# Create comprehensive logging wrapper
#!/bin/bash
LOGDIR="/var/log/myapp"
mkdir -p $LOGDIR
TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
nohup python3 myapp.py \
> ${LOGDIR}/app_${TIMESTAMP}.log \
2> ${LOGDIR}/error_${TIMESTAMP}.log &
echo $! > ${LOGDIR}/app.pid
echo "Started process $! at $(date)" >> ${LOGDIR}/startup.log
Resource Limiting
Prevent runaway processes from consuming all system resources:
# Limit CPU usage (nice value)
nohup nice -n 10 cpu_intensive_task.py &
# Limit memory usage with ulimit
ulimit -v 1048576 # 1GB virtual memory limit
nohup ./memory_intensive_app &
# Use timeout for processes that might hang
nohup timeout 3600 ./might_hang_script.sh & # Kill after 1 hour
Health Checking and Auto-Restart
Create self-monitoring scripts:
#!/bin/bash
# health_check.sh
PIDFILE="/tmp/myapp.pid"
LOGFILE="/var/log/myapp.log"
if [ -f $PIDFILE ]; then
PID=$(cat $PIDFILE)
if ! ps -p $PID > /dev/null; then
echo "Process died, restarting..." >> $LOGFILE
nohup python3 myapp.py >> $LOGFILE 2>&1 &
echo $! > $PIDFILE
fi
else
echo "Starting new process..." >> $LOGFILE
nohup python3 myapp.py >> $LOGFILE 2>&1 &
echo $! > $PIDFILE
fi
Performance Considerations and Optimization
When using nohup with resource-intensive applications, consider these optimizations:
Scenario | Optimization | Command Example |
---|---|---|
I/O intensive tasks | Use ionice to lower I/O priority | nohup ionice -c 3 ./backup_script.sh & |
CPU intensive tasks | Set CPU affinity | nohup taskset -c 0,1 ./cpu_task.py & |
Memory intensive tasks | Monitor and limit memory usage | nohup systemd-run --user --scope -p MemoryLimit=2G ./app & |
Security Considerations
Important security practices when using nohup:
- File permissions: Ensure nohup.out and log files have appropriate permissions
- Process ownership: Run processes with minimal required privileges
- Log rotation: Implement log rotation to prevent disk space exhaustion
- Monitoring: Set up alerts for process failures or resource consumption
# Set secure permissions for log files
chmod 640 nohup.out
chown appuser:appgroup nohup.out
# Run with specific user privileges
sudo -u appuser nohup ./application &
# Implement basic log rotation
logrotate_config='
/var/log/myapp/*.log {
daily
missingok
rotate 7
compress
notifempty
create 0644 appuser appgroup
}'
The nohup command remains one of the most reliable tools for ensuring process persistence in Linux environments. While alternatives like systemd services or containerization might be more appropriate for production environments, nohup excels in development, testing, and ad-hoc administrative tasks. For robust server management, consider exploring VPS solutions that provide the stability and resources needed for running persistent applications, or dedicated servers for demanding workloads that require guaranteed resources and performance.
For more detailed information about process management and signals in Linux, refer to the official nohup manual page and the GNU Coreutils documentation.

This article incorporates information and material from various online sources. We acknowledge and appreciate the work of all original authors, publishers, and websites. While every effort has been made to appropriately credit the source material, any unintentional oversight or omission does not constitute a copyright infringement. All trademarks, logos, and images mentioned are the property of their respective owners. If you believe that any content used in this article infringes upon your copyright, please contact us immediately for review and prompt action.
This article is intended for informational and educational purposes only and does not infringe on the rights of the copyright owners. If any copyrighted material has been used without proper credit or in violation of copyright laws, it is unintentional and we will rectify it promptly upon notification. Please note that the republishing, redistribution, or reproduction of part or all of the contents in any form is prohibited without express written permission from the author and website owner. For permissions or further inquiries, please contact us.