- Now let's level up and make your shell scripts multitask like a Middleware and DevOps pro.
- In this part, you'll learn how to run processes in the background, manage subshells, and execute multiple tasks in parallel for faster automation.
π Table of Contents
- 1️⃣ What is a Subshell?
- 2️⃣ Running Commands in the Background
- 3️⃣ Managing Jobs (fg, bg, kill)
- 4️⃣ Parallel Execution in DevOps
- 5️⃣ nohup & disown for Long Running Tasks
- 6️⃣ Real-World Examples (Tomcat, Jenkins, Docker)
- π§ Pro Tips & Troubleshooting
- π Summary
1️⃣ What is a Subshell?
A subshell is a new child shell launched from your current shell. It allows running commands in isolation — any variable or environment change inside doesn’t affect the parent shell.
#!/bin/bash
VAR="ParentShell"
( VAR="SubShell"; echo "Inside subshell: $VAR" )
echo "Outside subshell: $VAR"
π¬ Output:
Inside subshell: SubShellOutside subshell: ParentShell
2️⃣ Running Commands in the Background
Add & at the end of a command to run it in the background.
This allows your script to keep running while other tasks continue behind the scenes.
#!/bin/bash
echo "Starting Jenkins backup..."
tar -czf /opt/backups/jenkins_backup.tar.gz /var/lib/jenkins &
echo "Backup started in background (PID: $!)"
π¬ Output:
Starting Jenkins backup...Backup started in background (PID: 2356)
3️⃣ Managing Jobs
When you run background processes, you can manage them using built-in commands:
jobs→ List current background jobsfg %1→ Bring job 1 to foregroundbg %1→ Resume job 1 in backgroundkill %1→ Kill job 1
#!/bin/bash
long_task() {
sleep 30 &
}
long_task
jobs
π¬ Output:
[1]+ Running sleep 30 &
4️⃣ Parallel Execution in DevOps
Let’s say you want to restart multiple Tomcat servers or back up several apps simultaneously.
We can use background jobs with a simple for loop.
#!/bin/bash
servers=("devbox" "testbox" "prodbox")
for srv in "${servers[@]}"; do
echo "Restarting Tomcat on $srv..."
ssh $srv "systemctl restart tomcat" &
done
wait # Wait for all background jobs to finish
echo "✅ All Tomcat servers restarted in parallel!"
π¬ Output:
Restarting Tomcat on devbox...Restarting Tomcat on testbox...
Restarting Tomcat on prodbox...
✅ All Tomcat servers restarted in parallel!
5️⃣ nohup & disown
Sometimes, you need your scripts to survive terminal disconnections — for instance, a long-running Jenkins restore or DB migration.
#!/bin/bash
nohup ./db_migration.sh > /var/log/db_migration.log 2>&1 &
disown
echo "π§ DB migration running safely in background, even if you close terminal."
π¬ Output:
nohup: ignoring input and appending output to 'nohup.out'π§ DB migration running safely in background, even if you close terminal.
6️⃣ Real-World DevOps Examples
π§© Example 1: Parallel Log Collection
#!/bin/bash
servers=("dev" "test" "prod")
for s in "${servers[@]}"; do
scp $s:/var/log/tomcat/catalina.out /opt/logs/$s.log &
done
wait
echo "✅ Logs collected from all servers."
π§° Example 2: Monitor Jenkins & Docker in Background
#!/bin/bash
( while true; do pgrep -f jenkins >/dev/null || systemctl restart jenkins; sleep 10; done ) &
( while true; do docker ps | grep -q "webapp" || docker start webapp; sleep 15; done ) &
echo "π©Ί Jenkins and Docker monitored in background."
π¦ Example 3: Run Multiple CI Jobs Simultaneously
#!/bin/bash
for job in build-test build-stage build-prod; do
curl -s -X POST "http://jenkins.local/job/$job/build" &
done
wait
echo "✅ All Jenkins build jobs triggered in parallel!"
π§ Pro Tips & Troubleshooting
π§© Process Control Best Practices
- Use
waitto synchronize parallel tasks before continuing. - Log PIDs (
$!) to monitor background tasks easily. - Redirect background job output to separate log files using
> file 2>&1 &. - For persistent background services, prefer
systemdover manual nohup scripts.
⚙️ Debugging Issues
- Run
ps -ef | grep scriptnameto confirm background processes. - Use
set -xorbash -x script.shfor detailed command tracing. - Use
trapto clean up child processes on script exit. - If parallel SSH commands hang, add
-o ConnectTimeout=5in SSH for safety.
π Summary
- ✅ Learned to create and manage subshells.
- ⚙️ Mastered process control using
&,jobs,fg,bg, andkill. - π Ran real-world DevOps tasks in parallel — Jenkins builds, Tomcat restarts, Docker checks.
- π‘ Used
nohupanddisownfor persistent automation tasks.
No comments:
Post a Comment