Menu

Sunday, 2 November 2025

⚙️ Shell Scripting for Beginners – Part 13: Subshells, Process Control & Parallel Execution.

  • 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?

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: SubShell
Outside 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 jobs
  • fg %1 → Bring job 1 to foreground
  • bg %1 → Resume job 1 in background
  • kill %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 wait to 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 systemd over manual nohup scripts.

⚙️ Debugging Issues

  • Run ps -ef | grep scriptname to confirm background processes.
  • Use set -x or bash -x script.sh for detailed command tracing.
  • Use trap to clean up child processes on script exit.
  • If parallel SSH commands hang, add -o ConnectTimeout=5 in SSH for safety.

🏁 Summary

  • ✅ Learned to create and manage subshells.
  • ⚙️ Mastered process control using &, jobs, fg, bg, and kill.
  • πŸš€ Ran real-world DevOps tasks in parallel — Jenkins builds, Tomcat restarts, Docker checks.
  • πŸ’‘ Used nohup and disown for persistent automation tasks.

No comments:

Post a Comment