Issue
I have a shell script like the following which I want to use to execute a series of commands in a non-blocking way, i.e. as soon as one of the commands in the for loop is execute I want the script continue with the next command.
#!/bin/bash
declare -a parameters=("param1" "param2" "param3")
for parameter in "${parameters[@]}"
do
command="nohup mycommand -p ${parameter} 1> ${parameter}_nohup.out 2>&1 &"
${command}
done
However, for every command the script blocks until it finishes its execution before continuing to the next iteration of the for loop to execute the next command. Moreover, the output of the command is redirected to nohup.out
instead of the filename I specify to log the output. Why is the behavior of nohup different inside a shell script?
My distribution is Debian GNU/Linux 8 (jessie)
Solution
When executing a command the way you are trying to, the redirections and the &
character lose their special meaning.
Do it like this :
#!/bin/bash
declare -a parameters=("param1" "param2" "param3")
for parameter in "${parameters[@]}"
do
nohup mycommand -p $parameter 1> ${parameter}_nohup.out 2>&1 &
done
Answered By - Fred Answer Checked By - Clifford M. (WPSolving Volunteer)