9

I have a basic understanding of how to get a job in foreground switch to background and vice-versa but I am trying to come up with a way so that I can run multiple jobs in the background.I tried to put multiple jobs in the background but only one of which was in running state.I want to have a scenario where I can run multiple jobs in the background.

I came across this website where I see multiple jobs running in the background.Can someone please break it down for me as to how can I run multiple jobs in the background?

2
  • 1
    Knowing what you tried here that you expected to work and didn't would be very helpful to answer this. There are lots of ways this could happen (including just sending as jobs to the background one at a time and keep going until as many are running as you like). What specifically you did that was wrong might be more useful to understand than the myriad of ways it could
    – Caleb
    Feb 13, 2018 at 13:18
  • 1
    Depending on what you are trying to do, GNU Parallel may be the solution for you. youtube.com/playlist?list=PL284C9FF2488BC6D1
    – Ole Tange
    Feb 13, 2018 at 16:57

3 Answers 3

16

You can use the & to start multiple background jobs.

Example to run sequentially:

(command1 ; command2) &

Or run multiple jobs in parallel

command1 & command2 &

This will start multiple jobs running in the background.

If you want to keep a job running in the background, once you exit the terminal you can use nohup. This will ensure that SIGHUP, is not sent to a process once you exit the terminal.

example:

nohup command &
8
  • But it won't keep them running. Feb 13, 2018 at 12:27
  • @GerardH.Pille under what circumstance?
    – alpha
    Feb 13, 2018 at 12:37
  • 2
    @GerardH.Pille Those signals don't mean anything until a background process attempts to read or write a TTY.
    – Blrfl
    Feb 13, 2018 at 15:10
  • 1
    You need to nohup a process to prevent it from going away when you log off depending on the flavor of Unix; not sure on Linux, but on Solaris even if your process does not read/write to TTY it will die. Feb 13, 2018 at 16:16
  • 1
    @MarkStewart Quite correct, I should add that to my answer. Feb 13, 2018 at 19:24
2

When you start a job with an & sign at the end, you send it into the background. You can have as many background jobs as you like.

When a background job requires terminal input, it is automatically suspended and you receive a notification about that, either before the next command prompt (i.e. after you have run another command), or immediately if you used set -b.

When you log out, all running background processes will be sent a signal to notify them that the terminal has gone away. The default behavior for this signal is to kill the process, but you can change this by starting the background job through the nohup command.

For long-running background jobs, you can also use the at command to have the command started by a separate daemon at a specific time (which can be now). In this case, the output of the command will be sent to you by mail, but this requires the mail system on your computer to be set up properly.

1

You only need to make sure your jobs don't need terminal in- or output. Those should come from or go to files, and they'll run happily ever after.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .