Vasilis
Vasilis

Reputation: 2832

nohup commands from inside shell script are blocking script exeuction

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)

Upvotes: 0

Views: 1526

Answers (1)

Fred
Fred

Reputation: 6995

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

Upvotes: 2

Related Questions