Whenever we work with the terminal we may start multiple processes with a single command by using the pipe system:
tail -10 alfa.txt | sort | uniq | wc –l
This, for instance, starts four processes that are linked together with the pipes. Therefore we need a way to refer to these commands. The is where the definition of the job comes in. A job is a task executed from the terminal. We can follow this with the jobs command inside the terminal.
If you read my earlier articles, you may recall that we used this command when learning the fg, bg commands and the & sign at the end of a command. We may suspend (send to background) a job with the Ctrl+Z key combination. To bring it back to the foreground we may use the fg command. This will have as argument the number of jobs to bring forth; if no number is given, the last one will be used.
If you add a & keyword at the end of the terminal, the programs will automatically start in the background, leaving for us the terminal's I/O. You can send a suspended process to the background with the bg command. This also will have as argument the number of jobs on which to act. Processes finalized while in the background will signal back when they finish. If you give a pid to the pstree, the tree will be drawn only for that pid and its child processes. All this you can observe in the example presented in the images below:
The jobs arguments:
-p => List only the process identifiers
-l => List everything
-r => List only running processes
-s => List only stopped processes
-x => command run
The wait command will wait for the job or process identified with the argument number. When done, it will return the exit value of the process or job. With this we can synchronize processes and order them to wait for one or another of them. Again, if you give no argument, it will wait for the last process started. For instance:
This will be all for today. Sit back and think over again what you just learned and supplement it with some practice inside a terminal. In no time you will fully comprehend it. However, if you have any other questions left feel free to ask them here in the comments. Next time I will focus on the signal being sent to the processes, so if you are interested, be sure to come back. Until then rate my article if you liked it and Live With Passion!
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.