Running Several Commands All at Once
Problem
You need to run three commands, but they are independent of each other, and don’t need to wait for each other to complete.
Solution
You can run a command in the background by putting an ampersand (&) at the end of the command line. Thus, you could fire off all three jobs in rapid succession as follows:
$ long & [1] 4592 $ medium & [2] 4593 $ short $
Or better yet, you can do it all on one command line:
$ long & medium & short [1] 4592 2] 4593 $
Discussion
When we run a command in the background (there really is no such place in Linux), all that really means is that we disconnect keyboard input from the command and the shell doesn’t wait for the command to complete before it gives another prompt and accepts more command input. Output from the job (unless we take explicit action to do otherwise) will still come to the screen, so all three jobs will be interspersing output to the screen.
The odd bits of numerical output are the job number in square brackets, followed by the process ID of the command that we just started in the background. In our example, job 1 (process 4592) is the long command, and job 2 (process 4593) is medium.
We didn’t put short into the background since
we didn’t put an ampersand at the end of the line, so
bash will wait for it to complete before giving us
the shell prompt (the $).
The job number or process ID can be used to provide limited control over the
job. You can kill the long job with kill %1 (since its job number was ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access