67

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?

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
Ehsan
  • 883
  • 2
  • 7
  • 10
  • do you the the `&` at the end of a `nohup`? – Charlie Parker Nov 14 '22 at 22:46
  • A more complicated example: `nohup sh -c 'echo $SU_PASSWORD | /afs/cs/software/bin/reauth; python -u ~/diversity-for-predictive-success-of-meta-learning/div_src/diversity_src/experiment_mains/main_sl_with_ddp.py --manual_loads_name sl_hdb1_5cnn_adam_cl_filter_size --filter_size 4 > $OUT_FILE 2> $ERR_FILE' > nohup.out$SLURM_JOBID &` – Charlie Parker Nov 15 '22 at 18:43

7 Answers7

96

Wrap it in sh -c:

nohup sh -c 'wget "$0" && wget "$1"' "$url1" "$url2" > /dev/null &
jw013
  • 50,274
  • 9
  • 137
  • 141
12

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
assaf
  • 121
  • 1
  • 2
6

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.

janos
  • 11,171
  • 3
  • 35
  • 53
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 &
Didi Kohen
  • 1,813
  • 9
  • 14
  • 4
    The only safe way to pass parameters to `sh -c` is via separate arguments, which can be accessed via the positional parameters (`$0`, `$1`, ...). There is no easy or simple way to quote arguments properly for string parsing. Your attempt to use escaped double quotes fails if `$url` itself contains double quotes. Try `a='problem"'; sh -c "echo \"$a\""` yourself and see - you should get a syntax error because `sh -c` tries to run `echo "problem""`. – jw013 Sep 05 '12 at 15:57
  • @jw013 Out of interest, does your objection apply equally to the accepted answer by -oh. By you. Presumably not, then. But I don't understand why not! :-) – Jonathan Hartley Jun 13 '16 at 18:12
  • 1
    @JonathanHartley I'm not sure I understand your question. If you compare this answer to mine carefully you should notice a difference in the way `$url` and `$url2` are used. – jw013 Jun 13 '16 at 22:50
  • @jw013 Ah, of course. I was being silly. Thank you for setting me straight. – Jonathan Hartley Jun 14 '16 at 16:40
0

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.

Íhor Mé
  • 101
  • 1
0

A more complicated example:

nohup sh -c 'echo $SU_PASSWORD | /afs/cs/software/bin/reauth; python -u ~/diversity-for-predictive-success-of-meta-learning/div_src/diversity_src/experiment_mains/main_sl_with_ddp.py --manual_loads_name sl_hdb1_5cnn_adam_cl_filter_size --filter_size 4 > $OUT_FILE 2> $ERR_FILE' > nohup.out$SLURM_JOBID &
Charlie Parker
  • 1,497
  • 3
  • 16
  • 28
-2

might be helpful for someone:

nohup $(wget $url && wget $url2) &
Nurik
  • 1
  • Welcome to the site. Would you explain the purpose of the command substitution? In its current form, your solution would try to execute the output generated by the two `wget` calls in a `nohup` environment. – AdminBee Jan 08 '21 at 08:37
  • Thanks @AdminBee :) Correct, both wget will output to hohup and for nohup it is like one single command – Nurik Jan 09 '21 at 11:46