1 00:00:00,000 --> 00:00:03,234 Hello again, and now, in this lesson, we 2 00:00:03,272 --> 00:00:06,658 are going to cover process management. 3 00:00:06,814 --> 00:00:08,686 What is a process management? 4 00:00:08,758 --> 00:00:11,514 Anything that has to do with the process, how 5 00:00:11,552 --> 00:00:13,794 we manage it, how we start it, how we 6 00:00:13,832 --> 00:00:16,074 stop it, and how we bring it in the 7 00:00:16,111 --> 00:00:18,574 foreground, how we bring it in the background, 8 00:00:18,682 --> 00:00:22,470 all that stuff is process management. 9 00:00:22,970 --> 00:00:24,958 Let's go over all the commands 10 00:00:24,994 --> 00:00:28,630 that are associated with managing processes, 11 00:00:28,690 --> 00:00:31,542 the first one, are the set of commands that 12 00:00:31,556 --> 00:00:34,830 we have, for putting a process in the background. 13 00:00:35,450 --> 00:00:37,738 So why do we need to put a process in the background? 14 00:00:37,774 --> 00:00:39,702 Let's talk about that first. 15 00:00:39,836 --> 00:00:44,190 When you run a process on your terminal, meaning when you 16 00:00:44,240 --> 00:00:47,218 open up a console or when you open up a PuTTY 17 00:00:47,254 --> 00:00:51,654 terminal, when you run a process on your terminal, it will 18 00:00:51,692 --> 00:00:54,860 run it and it will not give you your prompt back. 19 00:00:55,490 --> 00:00:59,598 So if you want to do certain other tasks or you 20 00:00:59,624 --> 00:01:03,090 want to exit out of it, it will stop your process. 21 00:01:03,260 --> 00:01:05,206 So how do you run it in the background? 22 00:01:05,337 --> 00:01:07,050 So the command is Ctrl-z. 23 00:01:07,099 --> 00:01:09,834 First you run the process, then type 24 00:01:09,872 --> 00:01:11,850 Ctrl-z, it will stop the process, 25 00:01:12,020 --> 00:01:15,010 then you type jobs to list what is being stopped, 26 00:01:15,070 --> 00:01:16,698 and then you type bg, which is 27 00:01:16,724 --> 00:01:18,690 to put the process in the background. 28 00:01:19,490 --> 00:01:21,118 Then we have Foreground. 29 00:01:21,274 --> 00:01:25,242 So if you are done with all other tasks that 30 00:01:25,256 --> 00:01:27,594 you're doing on your terminal and you want to bring 31 00:01:27,632 --> 00:01:31,882 the process that you had put in the background initially 32 00:01:32,026 --> 00:01:34,518 and you wanted to see the process of it, then 33 00:01:34,544 --> 00:01:39,440 you type fg for Foreground, and that process will become 34 00:01:39,770 --> 00:01:42,330 live again on your console. 35 00:01:43,190 --> 00:01:48,190 To run a process even after you exit, meaning 36 00:01:48,250 --> 00:01:51,142 if you exit or if you close your terminal 37 00:01:51,226 --> 00:01:53,802 while your process is running, what will happen? 38 00:01:53,996 --> 00:01:55,460 It will stop the process. 39 00:01:55,460 --> 00:01:57,618 So how do you prevent it? 40 00:01:57,704 --> 00:01:59,830 There's a command called nohup. 41 00:02:00,010 --> 00:02:04,062 It means don't send any kind of signal to stop it. 42 00:02:04,076 --> 00:02:07,350 So nohup, the process, and followed by the 43 00:02:07,400 --> 00:02:11,008 sign ampersand or and sign, which is on top 44 00:02:11,060 --> 00:02:14,190 of your number seven key on your keyboard. 45 00:02:14,810 --> 00:02:17,454 Another command for that is nohup, the 46 00:02:17,492 --> 00:02:21,954 process, and redirect sign, /dev/null, and two, 47 00:02:22,052 --> 00:02:24,666 redirect, ampersand, one, and ampersand 48 00:02:24,728 --> 00:02:26,790 again. Why do we have all that stuff there? 49 00:02:26,840 --> 00:02:29,422 Because certain times when you run certain commands, 50 00:02:29,446 --> 00:02:31,038 like nohup, it gives some kind of 51 00:02:31,064 --> 00:02:34,374 warning, some kind of information about that process. 52 00:02:34,532 --> 00:02:35,922 So you don't want to see that. 53 00:02:35,996 --> 00:02:39,542 You want all that garbage to go out to /dev/null. So 54 00:02:39,542 --> 00:02:43,782 /dev/null is kind of an output to a 55 00:02:43,796 --> 00:02:47,120 door that you don't want to see the output for. 56 00:02:48,050 --> 00:02:50,478 Then to kill a process by name, 57 00:02:50,624 --> 00:02:52,518 you guys already know how to kill 58 00:02:52,544 --> 00:02:54,390 a process by process ID, right? 59 00:02:54,500 --> 00:02:56,358 So you kill, followed by the 60 00:02:56,384 --> 00:02:57,882 process ID kills the process. 61 00:02:58,016 --> 00:03:00,390 What if you want to kill a process by its name? 62 00:03:00,440 --> 00:03:04,146 So let's say if you have a process called top, right? 63 00:03:04,208 --> 00:03:06,080 So you run the command top. 64 00:03:06,926 --> 00:03:09,714 If you want to kill it by the name, you 65 00:03:09,752 --> 00:03:14,262 just simply type pkill, followed by the name of 66 00:03:14,276 --> 00:03:19,722 the process which is top. Process priority nice, 67 00:03:19,916 --> 00:03:21,918 there is a command called nice. 68 00:03:22,064 --> 00:03:23,214 And why do we use it? 69 00:03:23,252 --> 00:03:25,710 Because if we want to prioritize 70 00:03:27,170 --> 00:03:30,402 by different level of process. 71 00:03:30,476 --> 00:03:33,858 Meaning if a process is very important then the other, it 72 00:03:33,884 --> 00:03:39,150 has to be prioritized right. So the niceness scale goes from 73 00:03:39,200 --> 00:03:43,770 -20 to 19. The lower the number the more priority that 74 00:03:43,820 --> 00:03:47,958 task gets. Now why do we really need to go with 75 00:03:47,984 --> 00:03:52,494 the priority of the task, doesn't that processor CPU takes care 76 00:03:52,532 --> 00:03:54,918 of that, right, it does take care of that. 77 00:03:54,944 --> 00:03:58,086 But remember one thing. If you have one 78 00:03:58,148 --> 00:04:01,878 CPU in your computer or laptop, it means 79 00:04:01,964 --> 00:04:05,660 it will process one program at a time. 80 00:04:06,110 --> 00:04:08,602 So think of it as a juggling. 81 00:04:08,686 --> 00:04:11,406 So if you have two hands and you have five 82 00:04:11,468 --> 00:04:14,910 balls to throw it in the air, what will happen? 83 00:04:15,080 --> 00:04:19,745 Can you hold all five balls in your hands? No, right. 84 00:04:19,868 --> 00:04:23,946 So think of balls as programs that you need to process. 85 00:04:24,067 --> 00:04:26,874 So you would have to juggle it one after the other. 86 00:04:26,971 --> 00:04:30,894 So what nice does is, nice tells hey, this ball or 87 00:04:30,932 --> 00:04:34,162 this process is important to me, so make sure this stays 88 00:04:34,186 --> 00:04:37,570 in the hand most often than any other balls. 89 00:04:37,750 --> 00:04:39,222 So that's what nice does. 90 00:04:39,356 --> 00:04:41,022 We'll talk about that when we open up 91 00:04:41,036 --> 00:04:44,418 our terminal session. Then how to monitor a process is 92 00:04:44,444 --> 00:04:47,658 by running the command top and how to list all 93 00:04:47,684 --> 00:04:50,202 the processes that you have running in your computer, 94 00:04:50,336 --> 00:04:53,178 the command is ps, followed by of course 95 00:04:53,264 --> 00:04:55,074 many different options that you could use. 96 00:04:55,172 --> 00:04:57,550 So anyway, let's get into our Linux machine. 97 00:04:57,670 --> 00:05:00,510 I have it open right here, our console. 98 00:05:00,950 --> 00:05:03,642 So I am logged in as myself. 99 00:05:03,836 --> 00:05:05,670 I want to become root. 100 00:05:05,990 --> 00:05:07,654 So become root. 101 00:05:07,762 --> 00:05:10,914 Now I want to log into the PuTTY section as well, 102 00:05:10,952 --> 00:05:14,178 so this way I could run certain different programs from one 103 00:05:14,204 --> 00:05:17,394 terminal and kill it on the other terminal. So for that 104 00:05:17,432 --> 00:05:19,170 to log into the PuTTY, what do I need? 105 00:05:19,220 --> 00:05:21,586 I need an IP address of my machine. 106 00:05:21,718 --> 00:05:26,086 The command is ip addr, or ifconfig whichever you prefer. 107 00:05:26,158 --> 00:05:29,214 But if you're running latest version of Linux, then 108 00:05:29,252 --> 00:05:31,686 you probably not going to have ifconfig, so run 109 00:05:31,748 --> 00:05:34,614 ip command, and the IP address you'll see right 110 00:05:34,652 --> 00:05:38,826 here it says 192.168.56.101. 111 00:05:38,948 --> 00:05:41,490 So go into your PuTTY terminal. 112 00:05:42,170 --> 00:05:45,666 I have my PuTTY terminal right here, and I'm going to type 113 00:05:45,728 --> 00:05:51,894 it in my IP address which is 192.168.56.101. 114 00:05:51,992 --> 00:05:54,870 Hit enter, login as root. 115 00:05:55,670 --> 00:05:57,234 The first one is Ctrl-z. 116 00:05:57,272 --> 00:05:59,410 So I'm going to run a command called sleep. 117 00:05:59,530 --> 00:06:04,378 sleep is a command which will delay certain programs. 118 00:06:04,414 --> 00:06:07,506 Let's say if you're running a program ABC, and you wanted 119 00:06:07,568 --> 00:06:09,822 to run a second set of program, but you wanted to 120 00:06:09,836 --> 00:06:12,634 wait for 10 seconds, so you put in a sleep command. 121 00:06:12,682 --> 00:06:15,466 That's what it does, it just waits for 10 seconds, 122 00:06:15,538 --> 00:06:18,022 and it goes to the next set of instructions. 123 00:06:18,106 --> 00:06:22,386 So let's say if I type sleep 5, it will wait 124 00:06:22,508 --> 00:06:24,834 for 5 seconds, until I got my prompt back. 125 00:06:24,872 --> 00:06:27,382 You'll see, I got the prompt 126 00:06:27,406 --> 00:06:29,062 back right away after 5 seconds. 127 00:06:29,146 --> 00:06:30,306 So that's what sleep does. 128 00:06:30,368 --> 00:06:32,802 Don't focus too much on sleep, focus on what 129 00:06:32,816 --> 00:06:34,978 I'm going to teach you about process management. 130 00:06:35,014 --> 00:06:37,650 So I'm going to leverage this 131 00:06:37,760 --> 00:06:39,766 lesson on the command sleep, 132 00:06:39,838 --> 00:06:41,694 so this way it will be easier to understand. 133 00:06:41,852 --> 00:06:46,686 So if I run the command sleep 100, what will happen? 134 00:06:46,808 --> 00:06:49,054 I will have to wait 100 seconds 135 00:06:49,102 --> 00:06:50,970 until I get my prompt back, right? 136 00:06:51,080 --> 00:06:53,494 So I want to run that process in the background. 137 00:06:53,542 --> 00:06:57,234 So I will do Ctrl-z, it has stopped the process. 138 00:06:57,332 --> 00:07:00,322 Now it's not running actually in the background. 139 00:07:00,466 --> 00:07:02,838 To run that in the background, you have to 140 00:07:02,864 --> 00:07:05,746 type the command bg, which is for background. 141 00:07:05,878 --> 00:07:10,950 But let's say if you are running multiple process, and 142 00:07:11,060 --> 00:07:13,290 all of them stop, some of them are running. 143 00:07:13,340 --> 00:07:16,470 So the command to check what other 144 00:07:16,520 --> 00:07:18,438 processes that are stopped right now, 145 00:07:18,524 --> 00:07:21,306 so you just type command jobs, and you 146 00:07:21,308 --> 00:07:23,862 will see there is one process that's been 147 00:07:23,936 --> 00:07:26,526 stopped by the command Ctrl-z. 148 00:07:26,648 --> 00:07:28,650 If you want to run that process, bring 149 00:07:28,700 --> 00:07:31,378 it and send it to the background, 150 00:07:31,534 --> 00:07:33,838 then you type bg. 151 00:07:33,994 --> 00:07:36,510 This will send this process in the background. 152 00:07:37,310 --> 00:07:39,018 See, it is running now. 153 00:07:39,104 --> 00:07:40,918 So when you type jobs, you'll 154 00:07:40,954 --> 00:07:43,018 see right here, it's not stopped. 155 00:07:43,114 --> 00:07:47,398 See, it's running, but it is running in the background. 156 00:07:47,494 --> 00:07:52,102 You could also check by ps -ef command, by typing sleep. 157 00:07:52,246 --> 00:07:55,710 And you'll see right here, sleep 100. 158 00:07:55,820 --> 00:07:57,042 The other sleep 60, 159 00:07:57,116 --> 00:07:59,610 you could ignore that, this runs all the time 160 00:07:59,660 --> 00:08:01,282 if you're running virtual machine. 161 00:08:01,426 --> 00:08:05,430 Anyway, sleep 100 is still running. 162 00:08:05,540 --> 00:08:07,074 What if you want to bring it 163 00:08:07,112 --> 00:08:10,290 in the foreground, then you type fg. 164 00:08:12,170 --> 00:08:15,886 See, it's brought back that process in the foreground. 165 00:08:15,958 --> 00:08:20,994 And now you notice I do not have my prompt back. Why? 166 00:08:21,152 --> 00:08:24,078 Because the process is still running. 167 00:08:24,224 --> 00:08:28,650 So what if I wanted to put it back in the background? 168 00:08:29,150 --> 00:08:31,710 Okay, so 100 seconds are done. 169 00:08:31,760 --> 00:08:33,390 I got my prompt back. 170 00:08:33,500 --> 00:08:34,866 If I wanted to put it back in 171 00:08:34,868 --> 00:08:38,394 the background, I would have type Ctrl-z. Okay? 172 00:08:38,491 --> 00:08:41,898 Now, the next one we have is about nohup process. 173 00:08:42,044 --> 00:08:43,222 What is a nohup. 174 00:08:43,306 --> 00:08:46,217 Well, nohup is if you run a 175 00:08:46,244 --> 00:08:48,717 process and then you close your terminal. 176 00:08:48,813 --> 00:08:52,126 Meaning this terminal, if I close it right now, let's 177 00:08:52,138 --> 00:08:56,778 say if I'm running a command sleep 100, it is 178 00:08:56,804 --> 00:08:58,854 going to run, or it's going to wait for 100. 179 00:08:58,892 --> 00:09:03,822 Now, if I go to my console, clear the screen 180 00:09:03,896 --> 00:09:09,034 and I type ps -ef and do grep sleep, 181 00:09:09,142 --> 00:09:12,042 you'll see the sleep 100 is running, right? 182 00:09:12,176 --> 00:09:14,678 So if I wanted to, 183 00:09:14,678 --> 00:09:17,656 [No audio] 184 00:09:17,656 --> 00:09:19,452 have this terminal, let's close 185 00:09:19,452 --> 00:09:21,840 this terminal, I have a couple of terminal open. 186 00:09:22,410 --> 00:09:28,778 So if I close this and then do ps -ef sleep, 187 00:09:28,814 --> 00:09:31,920 now you'll see both sleep 100 are gone. 188 00:09:32,310 --> 00:09:34,894 So every time you run a process, 189 00:09:34,992 --> 00:09:37,910 it attaches itself with that terminal. 190 00:09:38,030 --> 00:09:42,634 Now, how do we run a process without 191 00:09:42,792 --> 00:09:46,270 interrupting that process when we close a terminal? 192 00:09:46,770 --> 00:09:52,274 So, let's go to our PuTTY session 193 00:09:52,322 --> 00:09:58,354 again, to run a process in the background and when you 194 00:09:58,392 --> 00:10:02,410 close the terminal, it won't impact it, you run nohup, 195 00:10:02,410 --> 00:10:04,990 [No audio] 196 00:10:04,990 --> 00:10:09,880 and name of the process, which is sleep 100, 197 00:10:09,880 --> 00:10:12,710 [No audio] 198 00:10:12,710 --> 00:10:17,802 let's put a sleep 75 just to differentiate it, and ampersand. 199 00:10:17,876 --> 00:10:20,538 So you hit Enter, and you will see, 200 00:10:20,684 --> 00:10:23,350 now it's running that process in the background. 201 00:10:23,410 --> 00:10:25,986 So when you do jobs and you'll see 202 00:10:26,048 --> 00:10:30,958 it is running, but it is not impacting 203 00:10:31,114 --> 00:10:33,538 your terminal, it's not attached to your terminal. 204 00:10:33,574 --> 00:10:35,058 So even at this time, if you 205 00:10:35,084 --> 00:10:38,406 close it, this process will still run. 206 00:10:38,588 --> 00:10:40,354 Now, what about this, nohup: 207 00:10:40,402 --> 00:10:44,550 ignoring input and appending output to 'nohup.out'. 208 00:10:44,660 --> 00:10:48,766 Well, every time you run a nohup command, 209 00:10:48,898 --> 00:10:53,358 it actually creates a file called nohup.out. 210 00:10:53,504 --> 00:10:55,986 So what it has, it has all the 211 00:10:56,048 --> 00:10:58,282 information, all the things that you have typed 212 00:10:58,306 --> 00:11:01,182 in there, it will just record everything. 213 00:11:01,316 --> 00:11:05,190 But if you just don't want to see that message comes 214 00:11:05,240 --> 00:11:07,986 up on the screen, or many times when you run certain 215 00:11:08,048 --> 00:11:14,398 commands, it gives you a message that this command was created 216 00:11:14,434 --> 00:11:16,470 on this day, and so many other stuff. 217 00:11:16,580 --> 00:11:18,858 So to get rid of that message, you could 218 00:11:18,884 --> 00:11:28,520 type nohup sleep 73 just to differentiate again, 219 00:11:30,350 --> 00:11:34,234 greater than sign, /dev/null, two, 220 00:11:34,234 --> 00:11:39,792 greater than sign, ampersand, one, ampersand again, hit enter. 221 00:11:39,792 --> 00:11:42,130 And now you will see, you did not get 222 00:11:42,180 --> 00:11:44,338 all that stuff that you were getting it before. 223 00:11:44,424 --> 00:11:47,974 Because what we are saying is whatever that comes up 224 00:11:48,012 --> 00:11:51,946 on the screen, those error messages, a warning, or those 225 00:11:52,128 --> 00:11:55,425 even informational messages, just send it to /dev/null, 226 00:11:55,425 --> 00:11:57,180 don't send it on the screen. 227 00:11:57,570 --> 00:11:59,350 So send it in the back. 228 00:11:59,460 --> 00:12:01,390 Now, what if you want to 229 00:12:01,560 --> 00:12:03,130 see what's running in the back? 230 00:12:03,180 --> 00:12:05,806 We run jobs and you'll see this 231 00:12:05,928 --> 00:12:09,074 one process is running in the background. 232 00:12:09,182 --> 00:12:11,074 Now, again, what if you wanted to bring 233 00:12:11,112 --> 00:12:13,694 that process in the forefront in the foreground? 234 00:12:13,742 --> 00:12:18,418 Just type fg, and you'll see it is running and 235 00:12:18,444 --> 00:12:21,490 it is on the foreground and you will not get 236 00:12:21,540 --> 00:12:26,426 your prompt back until it waits for 73 seconds. 237 00:12:26,558 --> 00:12:28,298 Once it's finished in 73 seconds, 238 00:12:28,334 --> 00:12:30,480 then we'll get a prompt back. 239 00:12:31,350 --> 00:12:34,318 Anyway, let's Ctrl-c out of it, which 240 00:12:34,344 --> 00:12:37,598 will completely stop, because if you type jobs 241 00:12:37,634 --> 00:12:39,530 now, you won't see any jobs. 242 00:12:39,650 --> 00:12:44,102 The next lesson, or sorry, the next topic 243 00:12:44,186 --> 00:12:47,402 of this lesson is about process priority. 244 00:12:47,546 --> 00:12:52,334 Which means run a process with a certain priority. 245 00:12:52,382 --> 00:12:55,222 So you could just simply type nice, space, 246 00:12:55,296 --> 00:12:58,598 -n, and space, what's the priority 247 00:12:58,634 --> 00:13:01,762 you want to give it, -20 which is the 248 00:13:01,776 --> 00:13:03,874 highest one, no I don't want it to mess 249 00:13:03,912 --> 00:13:06,022 up my other processes in the system. 250 00:13:06,156 --> 00:13:08,254 So I'll just say, simply say five 251 00:13:08,412 --> 00:13:11,506 and then I'll do sleep -10. 252 00:13:11,688 --> 00:13:12,874 I hit Enter. 253 00:13:13,032 --> 00:13:14,290 Now what it's doing 254 00:13:14,340 --> 00:13:17,614 is, it's telling CPU, hey, CPU I have 255 00:13:17,652 --> 00:13:21,322 this process sleep ten and please give this 256 00:13:21,456 --> 00:13:25,150 process priority five, which is not too high, 257 00:13:25,200 --> 00:13:26,782 which is not too low, 258 00:13:26,916 --> 00:13:31,198 it will tell that to the CPU, and CPU will process 259 00:13:31,284 --> 00:13:37,870 that according to the command of nice, that's what nice is. 260 00:13:37,980 --> 00:13:40,690 If you want to run your process to be the top 261 00:13:40,740 --> 00:13:44,746 priority it is very important, then I would say run nice 262 00:13:44,808 --> 00:13:49,330 -n -20, the name of the process, hit enter, and 263 00:13:49,380 --> 00:13:53,230 CPU will say hey, that's it, I have nothing to do 264 00:13:53,280 --> 00:13:55,740 but to take care of this process right away. 265 00:13:56,310 --> 00:13:58,862 That's about the nice of the priority. 266 00:13:58,946 --> 00:14:01,642 The other ones we already covered, which is the process 267 00:14:01,716 --> 00:14:05,146 monitoring, and of course the list of process to see 268 00:14:05,208 --> 00:14:09,259 what other processes are available in the system.