Issue
I was given a command "ChanDataStream_Sim" (on a linux box) that I need to run against 40 channels simultaneously. The command once executed runs indefinitely until someone manually kills the PID. Moreover the command will halt if interrupted or the session ended. A new dedicated PuTTy session would need to be launched for each channel we want to test.
What I would like to do is instantiate an instance of the command with EACH iteration of channel in my array. Essentially if I list 30 channels in my array, I'd like to see 30 instances (with their own PID) of the command line running. I want to avoid having the engineer open 40 PuTTy sessions just to run the thing or having to craft 40 tiny scripts if at all possible.
Desired outcome:
PID User
1112 ChanDataStream_Sim+ <--- For Channel 3002
1113 ChanDataStream_Sim+ <--- For Channel 1001
1114 ChanDataStream_Sim+ <--- For Channel 4003
1115 ChanDataStream_Sim+ <--- For any additional channels in the array
Here's what I wrote so far.
#bash
#!/bin/bash
# all the channels to simulate data flow against.
channels=(3002 1001 4003)
# how fast the simulated data should hit the channel
priority=(IDLE REALTIME HIGH)
# selecting one of the 225 daemons available for the process to run against
objserv=(83 82 81)
length=${#channels[@]}
for (( i=0; i<$length; i++ ))
do
# running the command with the requisite channel variables
ChanDataStream_Sim ${channels[$i]} -d -p ${priority[$i]} -s ${objserv[$i]}
done
Any clues would be greatly appreciated.
Solution
Solution: This snippet creates iterations of the embedded command based on the number of channels in the array
#bash
#!/bin/bash
# Testing Channels: 1001, 2001, 2004
# Setting up my channels and openChan_sim controls
channels=(3002 1001 4003)
priority=(REALTIME REALTIME REALTIME)
objserv=(83 82 81)
# Get the length of one of the arrays
length=${#channels[@]}
# Function to perform the iteration in a subshell
perform_iteration() {
local index=$1
openChan_sim ${channels[$index]} -d -p ${priority[$index]} -s ${objserv[$index]}"
}
# Loop through the channels and create subshell instances
for ((i=0; i<length; i++))
do
(perform_iteration "$i") &
done
wait
Answered By - NanoNet Answer Checked By - Cary Denson (WPSolving Admin)