{"id":191,"date":"2020-08-18T19:23:47","date_gmt":"2020-08-18T20:23:47","guid":{"rendered":"http:\/\/www.linux-tutorial.info\/?page_id=77"},"modified":"2020-08-22T19:26:02","modified_gmt":"2020-08-22T20:26:02","slug":"this-is-the-page-title-toplevel-25","status":"publish","type":"page","link":"http:\/\/www.linux-tutorial.info\/?page_id=191","title":{"rendered":"Job Control"},"content":{"rendered":"\n<title>Job Control<\/title>\n<question id=\"55\" text=\"Managing foreground and background processes is usually called what?\" \/>\n<p>\nJob control is the ability to move processes between the foreground and\nbackground. This is very useful when you need to do several things at once, but\nonly have one <glossary>terminal<\/glossary>. For example, let&#8217;s say there are several files spread out\nacross the system that we want to edit. Because we don&#8217;t know where they are, we can&#8217;t use full paths.\nBecause they don&#8217;t have anything common in their names, we can&#8217;t use find. So we try ls -R &gt; more.\n<\/p>\n<p>\nAfter a minute or two, we find the first file we want to edit. We can then\nsuspend this job by pressing <keyinput>Ctrl+Z<keyinput>. We then see something that looks like\nthis:<\/p>\n<p>\n<screenoutput>[1]+ Stopped ls -R | more<\/screenoutput>\n<\/p>\n<question id=\"54\" text=\"What symbol is used to put a command 'in the background'?\" \/>\n <question id=\"57\" text=\"A suspended job is a process running in the background.\" \/>\n<p>\nThis means that the process has been stopped or suspended. One very\nimportant thing to note is that this process is not in the background as if we\nhad put an &#8220;&amp;&#8221; at the end. When a process is suspended, it stops doing\nanything, unlike a process in the background, which keeps on working.\n<\/p>\n<question id=\"56\" text=\"Once a job is running in the background you can only stop it with the kill command.\" \/>\n<p>\nOnce the ls is in the background, we can run <command>vi<\/command>. When we are done with vi,\nwe can bring the ls command back with the fg (foreground) command.\n<\/p>\n<p>\nIf we wanted to, we could have more than just one job suspended. I have\nnever had the need to have more than two running like this, but I have gotten\nmore than ten during tests. One thing that this showed me was the meaning of the\nplus sign (+). This is the &#8220;current&#8221; job, or the one we suspended last.\n<\/p>\n<p>\nThe number in brackets is the process entry in the job table, which is\nsimply a table containing all of your jobs. Therefore, if we already had three\njobs, the next time we suspended a job, the entry would look like this:<\/p>\n<p>\n<screenoutput>[4]+ Stopped ls -R &gt;&gt; output<\/screenoutput>\n<\/p>\n<p>\nTo look at the entire job table, we simply enter the command jobs, which\nmight give us\n<\/p>\n<p>\n<screenoutput>\n[1] Stopped ls -R \/usr &gt;&gt; output.usr<br \/>\n[2] Stopped find \/ -print &gt; output.find<br \/>\n[3]- Stopped ls -R \/var &gt;&gt; output.var<br \/>\n[4]+ Stopped ls -R &gt;&gt; output.root<br \/>\n<\/screenoutput>\n<p>\nThe plus sign indicates the job that we suspended last. So this is the one\nthat gets called if we run fg without a job number. In this case, it was Job 4.\nNote that there is a minus sign (-) right after Job 3. This was the second to\nlast job that we suspended. Now, we bring Job 2 in the foreground with fg 2 and\nthen immediately suspend it again with Ctrl+Z. The table now looks like\nthis:<\/p>\n<p>\n<screenoutput>[1] Stopped ls -R \/usr &gt;&gt; output<br \/>\n[2]+ Stopped find \/ -print &gt; output.find<br \/>\n[3] Stopped ls -R \/var &gt;&gt; output<br \/>\n[4]- Stopped ls -R &gt;&gt; output<br \/>\n<\/screenoutput>\n<p>\nNote that Job 2 now has the plus sign following it and Job 4 has the minus\nsign.\n<\/p>\n<p>\nIn each of these cases, we suspended a job that was running in the\nforeground. If we had started a job and put it in the background from the\ncommand line, the table might have an entry that looked like this:<\/p>\n<p>\n<screenoutput>[3] Running ls -R \/var &gt;&gt; output &amp;<\/screenoutput>\n<\/p>\n<p>\nThis shows us that although we cannot see the process (because it is in the\nbackground), it is still running. We could call it to the foreground if we\nwanted by running fg 3. And, if we wanted, we could use the bg command to send\none of the stopped jobs to the background. So\n<\/p>\n<p>\n<commandexample command=\"bash\">bg {3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}1<\/commandexample>\n<\/p>\n<p>\nwould send Job 1 to the background just as if we had included &amp; from the\ncommand line.\n<\/p>\n<p>\nOne nice thing is that we don&#8217;t have to use just the job numbers when we are\npulling something into the foreground. Because we know that we started a process\nwith the find command, we can get it by using\n<\/p>\n<p>\n<commandexample>fg {3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}find <\/commandexample>\n<\/p>\n<p>\nActually, we could have used {3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}f or anything else that was not ambiguous. In\nthis case, we were looking for a process that started with the string we input.\nWe could even look for strings anywhere within the command. To do this, the\ncommand might be\n<\/p>\n<p>\n<commandexample command=\"bash\">fg {3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}?print<\/commandexample>\n<\/p>\n<p>\nwhich would have given us the same command. Or, if we had tried\n<\/p>\n<p>\n<commandexample command=\"bash\">fg {3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}?usr<\/commandexample>\n<\/p>\n<p>\nwe would have gotten Job 1 because it contains the string usr.\n<\/p>\n<p>\nIf we find that there is a job that we want to kill (stop completely), we\ncan use the kill command. This works the same way, so kill {3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}&lt;nr&gt; kills the\njob with number &lt;nr&gt;, kill {3f0b0cf5c640d99e599990c4a720721a04ec3a009b1323dd81fc335ceb655a63}&lt;string&gt; kills the job starting with\nstring, and so on.\n<\/p>\n<p>\nKeep in mind that process takes up resources whether they are in the\nforeground or not. That is, background processes take up resources,too.\n<\/p>\n<p>\nIf you do not remember the process ID of the last process that was placed in the background\nyou can reference it any time using the <keyinput>$!<keyinput> system variable. You can also\nuse the <command>wait<\/command> command to stop processing until the particular process is done.\nThe syntax is simply:\n<\/p>\n<commandexample command=\"wait\">wait PID<\/commandexample>\n<p>\nAlthough generally considered part of &#8220;job control&#8221; you can change the default priority a\nprocess has when it starts, as well as the process of a running process.\nDetails of this can be found in the <site id=\"85\">section on process scheduling<\/site>.\n<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Job Control Job control is the ability to move processes between the foreground and background. This is very useful when you need to do several things at once, but only have one terminal. For example, let&#8217;s say there are several &hellip; <a href=\"http:\/\/www.linux-tutorial.info\/?page_id=191\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-191","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=191"}],"version-history":[{"count":1,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/191\/revisions"}],"predecessor-version":[{"id":596,"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=\/wp\/v2\/pages\/191\/revisions\/596"}],"wp:attachment":[{"href":"http:\/\/www.linux-tutorial.info\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}