0

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)

2
  • Avoid writing output redirection to variables. Commented Apr 7, 2017 at 22:28
  • When I print the command the name of the output file is the correct, so for the first parameter it would be nohup mycommand -p param1 1> param1_nohup.out 2>&1 & but the output will go to nohoup.out regardless. Commented Apr 7, 2017 at 22:45

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.