I need to execute multiple commands using nohup. Each command should be executed after the previous command.
I used this command as an example:
nohup wget $url && wget $url2 > /dev/null 2>&1 &
However that command did not work.
What command should I use for this purpose?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Wrap it in sh -c:
nohup sh -c 'wget "$0" && wget "$1"' "$url1" "$url2" > /dev/null &
Method 2
Wouldn’t it be far simpler to create your list of commands in a separate shell file, e.g. command.sh, without any nohup.
Then you call:
nohup bash command.sh
Method 3
Others have already answered about nohup. As a practical side note: I recommend doing this kind of thing within a screen or tmux session. That way you can disconnect, then reconnect later and see the output and final result.
Method 4
It would be simpler like this:
nohup sh -c "wget $url && wget $url2" > /dev/null 2>&1 &
This however may cause issues if the URLs contain special characters so for a safer option you can use:
nohup sh -c "wget "$url" && wget "$url2"" > /dev/null 2>&1 &
Method 5
One alternative is to create an additional file with the sequential part of the script and call it with nohup. Although, if you’re passing parameters, that’s going to add a little bit extra work.
Method 6
might be helpful for someone:
nohup $(wget $url && wget $url2) &
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0