To run commands concurrently you can use the &
command separator.
~$ command1 & command2 & command3
This will start command1
, then runs it in the background. The same with command2
. Then it starts command3
normally.
The output of all commands will be garbled together, but if that is not a problem for you, that would be the solution.
If you want to have a separate look at the output later, you can pipe the output of each command into tee
, which lets you specify a file to mirror the output to.
~$ command1 | tee 1.log & command2 | tee 2.log & command3 | tee 3.log
The output will probably be very messy. To counter that, you could give the output of every command a prefix using sed
.
~$ echo 'Output of command 1' | sed -e 's/^/[Command1] /'
[Command1] Output of command 1
So if we put all of that together we get:
~$ command1 | tee 1.log | sed -e 's/^/[Command1] /' & command2 | tee 2.log | sed -e 's/^/[Command2] /' & command3 | tee 3.log | sed -e 's/^/[Command3] /'
[Command1] Starting command1
[Command2] Starting command2
[Command1] Finished
[Command3] Starting command3
This is a highly idealized version of what you are probably going to see. But its the best I can think of right now.
If you want to stop all of them at once, you can use the build in trap
.
~$ trap 'kill %1; kill %2' SIGINT
~$ command1 & command2 & command3
This will execute command1
and command2
in the background and command3
in the foreground, which lets you kill it with Ctrl+C.
When you kill the last process with Ctrl+C the kill %1; kill %2
commands are executed, because we connected their execution with the reception of an INTerupt SIGnal, the thing sent by pressing Ctrl+C.
They respectively kill the 1st and 2nd background process (your command1
and command2
). Don't forget to remove the trap, after you're finished with your commands using trap - SIGINT
.
Complete monster of a command:
~$ trap 'kill %1; kill %2' SIGINT
~$ command1 | tee 1.log | sed -e 's/^/[Command1] /' & command2 | tee 2.log | sed -e 's/^/[Command2] /' & command3 | tee 3.log | sed -e 's/^/[Command3] /'
You could, of course, have a look at screen. It lets you split your console into as many separate consoles as you want. So you can monitor all commands separately, but at the same time.
The two processes are sudo
on the one hand, and cp
on the other. When you run
sudo cp source destination &
the shell starts sudo
with the full command line; then sudo
(which runs as root
because it is setuid root
) checks that you're allowed to run cp
like that, and forks and starts cp
. So while cp
is running you see both sudo
and cp
processes.
Best Answer
When you say priority, you probably mean the nice-level of the process. To quote Wikipedia:
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:
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:
...which will redirect normal output into the void. Errors will still be visible.