0

I have just learned about nohup which is so far very useful - my jobs aren't crashing due to my ssh session unintentionally disconnecting.

Can someone tell me what happens if I do ctrl-C after submitting a nohup job? I'm hoping it continues to run. After starting the job it says

$ nohup: appending output to ‘nohup.out'

But it does not take me to a fresh line of input to do other commands.

Can anyone explain why the & is necessary at the end of my command?

nohup sh -c 'for file in ../orig_data/merged/*Inp*.gz; do zcat $file | bowtie2 -p 8 -q -x hg_mm -U - -S "${file:20:-9}".sam; done' &

EDIT:This nohup command is within a bash script executed as ./job.sh. So if I kill this bash script, does it also kill my nohup command?

zachg
  • 13
  • 4
  • & runs your command as a background process (i.e. outside your shell). You need to read up on this. – MandyShaw Jul 31 '18 at 19:28
  • 1
    BTW, in general, well-written bash has no need to use `nohup`; everything `nohup` can do, you can do yourself with redirections and the `disown` command -- and doing the redirections yourself means you have more control over where logs are emitted to. – Charles Duffy Jul 31 '18 at 19:29
  • 1
    When you say "does not take you to a fresh line of input" -- that fresh line of input was the `$` at the beginning of your line of output. Because of the `&`, nohup is run **in the background**, so the prompt is printed **before** `nohup` is finished starting up. If you just press enter again, you'll get a new prompt. – Charles Duffy Jul 31 '18 at 19:33
  • @CharlesDuffy Thanks. I was told to use nohup to avoid my jobs dying due to ssh disconnections. See the edit for why I don't get a `$` after running this command – zachg Jul 31 '18 at 19:34
  • 1
    I linked this to a separate duplicate that explains that. That said, the person who told you to use nohup arguably didn't know bash particularly well; it can be necessary with `sh` (a more limited shell), but `bash` is more capable. – Charles Duffy Jul 31 '18 at 19:37
  • 1
    Consider for example: `for file in ../orig_data/merged/*Inp*.gz; do bowtie2 -p 8 -q -x hg_mm -U - -S "${file:20:-9}".sam < <(zcat "$file") & disown -h "$!"; done >bowtie.log 2>&1 – Charles Duffy Jul 31 '18 at 19:41
  • @CharlesDuffy Thank you this is very helpful to see written out – zachg Jul 31 '18 at 19:59

1 Answers1

0

The & sign at the end of any shell command puts your process in the background so you can continue interacting with your terminal, and at the same time, your command executes in the background.

user1506104
  • 6,554
  • 4
  • 71
  • 89