π ** Command Chaining using cmd & (Background Execution)**
π― What You Will Learnβ
- Understand how the ampersand (
&) operator works in Bash. - Learn how to run commands in the background, freeing up your terminal for other tasks.
- Explore process control β how to list, manage, and bring back background jobs.
- Apply background execution in WordPress VPS operations, server maintenance, and automation scripts.
- Learn how background tasks interact with system resources and shell sessions.
- Identify and avoid common mistakes (like detached or orphaned jobs).
1. 5W + 1H Frameworkβ
| Element | Description |
| What | The & operator runs a command in the background, allowing the shell to continue executing the next command immediately. |
| Why | To save time and multitask, especially when a command takes a long time to finish (e.g., large backups, scans, updates). |
| Who | Linux administrators, DevOps engineers, and WordPress VPS managers managing multiple server tasks. |
| When | When a command doesnβt require immediate terminal output β e.g., cron jobs, backups, or log processing. |
| Where | In CLI, scripts, or system automation tasks that handle long-running or non-interactive commands. |
| How | Append & at the end of a command: command & |
2. Prerequisitesβ
- Basic understanding of:
- Bash CLI
- Foreground vs background process behavior
- Process IDs (PID)
- Optional:
jobs,fg,bg, andpsutilities installed (theyβre standard in Bash).
3. Core Syntax & Concept ( Revised )β
| # | Syntax Formula | Example Command | Purpose / Usage | Behavior / Notes |
| 1 | command & | sleep 60 & | Run a single command in background. | Shell prints job ID and PID, returns prompt immediately. |
| 2 | cmd1 & cmd2 | tar -czf backup.tar.gz /var/www/html & echo "Backup started" | Start cmd1 in background, then instantly run cmd2. | cmd1 continues independently while cmd2 executes. |
| 3 | { cmd1 & cmd2 & } | { sleep 10 & sleep 5 & } | Launch multiple tasks simultaneously. | Both run in parallel; shell returns after both complete or are disowned. |
| 4 | ( cmd1 & ) ; cmd2 | ( mysqldump -u root -p wpdb > backup.sql & ) ; echo "Dump started" | Run background job inside a subshell, then a foreground command. | Subshell isolates environment; cmd2 executes immediately after. |
| 5 | nohup command & | nohup wp plugin update --all > /root/logs/wpupdate.log 2>&1 & | Execute background job immune to terminal hang-ups. | Continues after SSH logout; redirect output to file for logs. |
| 6 | nice -n 10 command & | nice -n 10 tar -czf backup.tar.gz /var/www/html & | Run a background job with lower CPU priority. | Helps prevent I/O or CPU starvation on busy servers. |
| 7 | command > log.txt 2>&1 & | clamscan -r /var/www/html > /root/logs/scan.log 2>&1 & | Send stdout and stderr to log file while running in background. | Keeps terminal clean and stores output for review. |
4. Conceptual Flowβ
# Example 1
sleep 60 &
Expected Output:
[1] 2457
Explanation:
[1]β Job number assigned by shell.2457β Process ID (PID).- The shell immediately returns to the prompt while the process runs in background.
# Example 2
tar -czf /root/wpbackup.tar.gz /var/www/html & echo "Backup started..."
Expected Output:
[1] 2498
Backup started...
Explanation:
- Backup starts in the background; shell moves on instantly.
- Ideal for large WordPress directories.
# Example 3 (with nohup)
nohup wp plugin update --all > /root/logs/wpupdate.log 2>&1 &
Expected Output:
[1] 2520
appending output to 'nohup.out'
Explanation:
- Runs even if SSH session disconnects.
- Redirects output to
/root/logs/wpupdate.log.
5. WordPress VPS Use Casesβ
| Scenario | Example Command | Description |
| 1. Background Plugin Update | wp plugin update --all & | Updates all plugins in background while continuing other work. |
| 2. Long Backup Process | tar -czf /root/wpbackup.tar.gz /var/www/html & | Create full backup without blocking shell. |
| 3. MySQL Dump | mysqldump -u root -p wpdb > /root/backup.sql & | Backup database in background. |
| 4. Log Rotation | logrotate /etc/logrotate.conf & | Run log rotation while performing updates. |
| 5. Virus Scan | clamscan -r /var/www/html & | Perform malware scan in background. |
| 6. Remote Deployment | rsync -avz /var/www/html root@backup:/mirror/ & | Sync site to remote backup server in background. |
6. Best Practicesβ
| Practice | Description |
| β Always monitor jobs | Use jobs or ps to check running background processes. |
| β Redirect output | Example: command > output.log 2>&1 & |
β
Use nohup for SSH | Prevents process termination when SSH disconnects. |
| β Assign logs for each background task | Easier debugging and validation. |
| β οΈ Avoid running too many at once | Can overload CPU or disk I/O. |
βοΈ Combine with nice or ****ionice | Control resource priority. Example: nice -n 10 tar -czf backup.tar.gz /var/www/html & |
7. Process Management Essentialsβ
| Command | Description | Example |
jobs | Lists all background jobs with job numbers. | jobs |
fg %1 | Bring job #1 to foreground. | fg %1 |
bg %1 | Resume a stopped job in background. | bg %1 |
kill %1 | Terminate job #1. | kill %1 |
| `ps aux | grep process` | Check background process status. |
disown %1 | Detach job so it wonβt stop when terminal closes. | disown %1 |
8. Common Mistakes & Fixesβ
| Mistake | Example | Problem | Fix |
| Forgetting output redirection | wp plugin update --all & | Output floods the screen. | Redirect: wp plugin update --all > /root/logs/wpupdate.log 2>&1 & |
| Running too many jobs | for i in {1..20}; do sleep 100 & done | System overloads. | Limit background jobs with wait or job control. |
| Losing jobs on disconnect | SSH closes, job stops | Use nohup or screen/tmux | |
| Forgetting to stop | Background process keeps running | Check with jobs, then kill %1 |
9. Troubleshooting Matrixβ
| Issue | Cause | Solution |
| Background job stops after logout | Terminal session ended | Use nohup, screen, or tmux. |
| Canβt find running job | Job detached or PID unknown | Use `ps aux |
| Too many background jobs | Overloaded CPU or I/O | Limit concurrency using wait. |
| Output missing | Not redirected | Always specify > logfile 2>&1. |
10. Quick Lab: Practice on VPSβ
Objective: Backup WordPress files while running other commands.
tar -czf /root/wpbackup.tar.gz /var/www/html > /root/logs/backup.log 2>&1 &
echo "Backup started in background..."
sleep 5
jobs
Expected Output:
Backup started in background...
[1]+ Running tar -czf /root/wpbackup.tar.gz /var/www/html > /root/logs/backup.log 2>&1 &
Now check progress:
ps aux | grep tar
Expected Output:
root 3130 95.0 0.5 22000 5400 pts/0 R 14:35 0:20 tar -czf /root/wpbackup.tar.gz /var/www/html
11. Combined Example with AND, OR, and &β
wp plugin update --all && echo "β
Update done" || echo "β Failed" & disown
Behavior:
- Update runs in background.
- Success/failure message logged.
- Process detached safely from terminal. Expected Output:
[1] 3421
β
Update done
12. Cheat Sheetβ
| Pattern | Meaning | Example |
cmd & | Run command in background | sleep 60 & |
cmd1 & cmd2 | Run first in background, continue with next | tar -czf site.tar.gz /var/www/html & echo "Done" |
nohup cmd & | Run in background, persist after logout | nohup backup.sh & |
jobs | List background tasks | jobs |
fg %1 | Bring job to foreground | fg %1 |
disown %1 | Detach job permanently | disown %1 |
13. Mini-Quizβ
- What does
cmd &do? β Runs the command in the background, freeing the terminal. - Whatβs the difference between
;and&? β;runs sequentially in foreground;&runs concurrently in background. - How do you view running background jobs?
β Use
jobs. - How can you ensure a background process continues after SSH logout?
β Use
nohupordisown. - Whatβs the role of the number in
[1] 2457? β Job number and PID assigned by Bash.