In a Bash script, if you want to run multiple commands with nohup without combining them into a single string, you can use one of these approaches:
Use { } to group commands and redirect output for the entire group:
bashnohup { command1 command2 command3 } > output.log 2>&1 &
{ } groups the commands, and the output of all commands is redirected to output.log.& at the end runs the group in the background.Note: There must be a space after { and before }.
You can also use ( ) to run the commands in a subshell:
bashnohup ( command1 command2 command3 ) > output.log 2>&1 &
nohup for Each CommandIf you want each command to have its own nohup and output file:
bashnohup command1 > output1.log 2>&1 & nohup command2 > output2.log 2>&1 & nohup command3 > output3.log 2>&1 &
Define a function and call it with nohup:
bashmy_commands() {
command1
command2
command3
}
nohup my_commands > output.log 2>&1 &
Key Points:
> output.log 2>&1 redirects both stdout and stderr to output.log.& runs the process in the background.nohup "command1; command2") as it can cause parsing issues.This Chat is read-only. Login to resume chatting.