Reputation: 2471
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:
But, then I need to hit enter again to be able to type another command:
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?
Upvotes: 3
Views: 2580
Reputation: 158250
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
.
Upvotes: 2
Reputation: 263637
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.
Upvotes: 4