Bash – How to run two ongoing processes at once in linux/bash

background-processbashlinuxprocess

I would like to know how I can run two ongoing processes at the same time in Linux/bash. basically, I have a Node web server, and a MJPG-Streamer server. I want to run both these processes at once, but they are ongoing processes. I heard about running them as background processes, but I want them to be the same priority as a foreground process.

Best Answer

When you say priority, you probably mean the nice-level of the process. To quote Wikipedia:

nice is a program found on Unix and Unix-like operating systems such as Linux. It directly maps to a kernel call of the same name. nice is used to invoke a utility or shell script with a particular priority, thus giving the process more or less CPU time than other processes. A niceness of −20 is the highest priority and 19 or 20 is the lowest priority. The default niceness for processes is inherited from its parent process, usually 0.

Running a process in the background does not inflict on it's nice-level. It's entirely the same as when you're running it in the foreground.

So you can easily run your application/process in the background by invoking it with a trailing '&'-sign:

my-server &

You can also send a foreground-process to the background, by pressing ctrl+z (pauses the execution) followed by bg+enter.

You can list running background-tasks with the command jobs.

To get it back to the foreground you must find out its job-ID with the jobs-command, and run fg [job-ID] (for example: fg 1)

Background tasks will send all their output to your shell. If you don't want to see their output, you'll need to redirect it to /dev/null:

my-server 1>/dev/null &

...which will redirect normal output into the void. Errors will still be visible.

Related Question