3

I want to write a bash script that runs two commands in the background. I am using nohup for this:

nohup cmd1 &
nohup cmd2 &

However, only the 1st command runs in the background.

When I run nohup cmd1 & manually in the command line. First, I type nohup cmd1 & then hit enter; this starts the process:

enter image description here

But, then I need to hit enter again to be able to type another command:

enter image description here

I think this is "clogging" up the command line, and is causing my bash script to get stuck at the first nohup ... & command.

Is there a way to prevent this?

3
  • Can you try something like: sleep 5 in betweent he nohup commands?
    – ryekayo
    Commented Apr 11, 2016 at 15:48
  • @ryekayo it get's clogged up forever as opposed to 5 secs only.
    – applecider
    Commented Apr 11, 2016 at 15:51
  • There is no need to hit enter; the text which moved the cursor is disturbing but harmless.
    – tripleee
    Commented Apr 11, 2016 at 16:02

2 Answers 2

4

Nothing is "clogged". The first command, running in the background, prints some output after your shell prints its next prompt. The shell is waiting for you to type a command, even though the cursor is no longer on the same line as the prompt. That extra Enter is an empty command, causing the shell to print another prompt. It's harmless but unnecessary.

2

Let me say something to nohup because I'm not sure if you are certain about what it is doing. In short, the nohup command is not necessary to run a process in background. The ampersand at the end of the line is doing it.

nohup prevents the background process from receiving SIGHUP (hup for hang up) if you close the terminal where the starting shell runs it. SIGHUP would effectively terminate the process.

If started with nohup the process will not receive that event and will continue running, owned by the init process (pid 1) if the terminal will being closed.

Furthermore the nohup command will redirect standard output of the controlled process to a file, meaning it will not appear on screen any more. By default this file is called nohup.out.

Not the answer you're looking for? Browse other questions tagged or ask your own question.