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.

migrated from stackoverflow.com Feb 3 '14 at 22:22

This question came from our site for professional and enthusiast programmers.

  • 3
    Having 2 foreground processes in one shell? Not to my knowledge. You can start them in 2 shells, or open 2 windows in a screen session (which each have their own shell). Not that priority and fore/background don't have anything to do with each other here: the priority will stay the same. But ideally, you just want to start them as a deamon. Look at the /etc/init.d directory, or use something like supervisord which can also be configured to restart automatically on failure of the server. – Wrikken Jan 29 '14 at 20:11
  • Presumably you want these server processes to restart following a reboot of the computer. You should look into creating upstart scripts for them. – glenn jackman Jan 29 '14 at 21:46

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.

  • @calumcb: If that answers your question - please flag it as resolution. – Marlon Jan 30 '14 at 11:54

If you want to run the two processes from the same console or xterm in the foreground, you could consider running them inside screen or tmux sessions, which are terminal multiplexers. The processes won't run under the same shell process then, but under separate shell processes.

E.g. using screen:

  • Start screen
  • Run the first process
  • Press Ctrl-a c to start a second shell session
  • Start the second process
  • Switch back and forth between the two subshells using ctrl-a ctrl-a

See screen manual for more info.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy