 Last week, we introduced some basic commands to get around in the Linux operating system. So we'll continue today and look at some different things, look at running processes, connecting to other computers, and a little bit about permissions, who can do what on a computer. The first thing we'll look at is processes. When we run a command, LS, for example, LS is actually a program on this computer. We know that if we use which, LS, LS is actually an executable installed on the computer in the bin directory. So when I type LS, that the program is executed, it does something, that is at least the files or the directories, and then it stops executing. So that's common with most commands. It starts the application, it does something, and it stops. We talk about that it starts a software process, a process runs, does something, and then ends. So we want to look at, in a terminal, normally we just run one process at a time. I type LS, I press enter, LS executes, the process runs, stops, then I do another command. So just sequential. We can do more complex than that. We can run some processes, leave them running, and then run some other processes. So we'll introduce how to do that. The concept is that we can have one process that we can run and interact with by typing things in, and then other processes we say are running in the background. They're running, but we don't interact with them. So let's see how we do that and give some examples of why it's useful. First, let's see one of our commands we did last week, LS. If we want to list all of our files on our hard disk, LS minus L shows us in the long format, and there's a minus R option, recursive. And we want to list everything on the hard disk, so let's list all files in long format recursively starting from the root directory. Recursively means list the files in that directory and then all of its subdirectories and all of its subdirectories and so on. So essentially list everything on the hard disk. Run it. See what happens. So LS minus L minus uppercase R forward slash to mean list in the root directory. And when we run it, you see it prints all the files on the hard disk. There's some we can't see. I want to stop. So the process is running. I can't interact with the terminal. I can't run other processes while this one's running. It stopped. Okay. Now I can run other commands, the next command. So that process took a long time or some things we may want to do. Maybe I run it and I realize I'm not so patient. So I run it again. How do I stop it before it finishes? One way is control C. So you don't see the command I press, but note down here it showed I pressed control C character there, the two keys. So that stops it or that's what we say stops or cancels a process. Sometimes we say we kill the process. We talk about the process was running to stop it before it completes normally. Control C will do that, cancel the process. So that's useful when you have something that you see is going to run for a long time and you realize you don't want to wait for the end or if you've done something wrong or typed in the wrong command and it's sitting there waiting for input, you've got no idea what to do, then control C will cancel or kill that process and you can return to the terminal and interact. Just to show that because doing that, to demonstrate that we need a process that will run a long time. The LS minus LR does run for a long time. Let's find another process just for the demonstration. I'll just clear that. Try yes. What does yes do? Just prints wise forever. This process will run for a long time. It will run until the computer shuts down. So it's just for testing. So just prints wise forever. So how do we stop it? Well we can control C to cancel or kill the process. So we just use yes just as an easy demo of manipulating processes. So when the program runs, when I run yes, it's running, I can kill or cancel the process with control C and the operating system stops the process and deletes any information about it. But we can also pause the process or suspend the process, put it to sleep and to put a process to sleep, suspend control Z. Control C kills control Z, suspends or puts it to sleep. That is the operating system still has some information about this process in memory. It's just not executing at this stage. It's stopped that says on the command line. It's not running, but note with it suspended we can still interact with the terminal. We would like to maybe, if it's paused, to restart, to resume. So what we can do to see the set of processes that are stopped or paused from this bash shell that I refer to as jobs. What jobs are paused? If we type jobs, it says the jobs that are paused or suspended or stopped at this stage. Yes, it's stopped and it's job number one. So that's a paused process. We can bring it back and start it again and we say that there's always one process that can run with interactivity with the terminal and we say that process is in the foreground. So to bring that process back, we can say foreground followed by the job number is job number one, FG1. Or if you just type FG on its own, it will still work because it brings the most recent one paused back. But FG1 will bring our process, yes, back to the foreground, that is, it's running again. Control Z will stop it again and we see it's listed as the jobs. Foreground will bring it back. FG followed by the number or if you want the most recent one just FG is now in the foreground. A process in the foreground, when I do something on the terminal, I'm interacting with that process in the foreground. There's nothing to interact with this program, it just prints on the screen. So I can't run other programs at the moment. We can Control C, kill the process. There's no jobs suspended now. So jobs list those that have been suspended or stopped. FG brings, can you be used to bring one that was suspended back to foreground and have it running again. So that's useful if you want to pause something and then go do something else in the meantime and then come back to that process. Let's try that in some different ways. Let's run yes and let's output that to a file. Remember redirection from last week, the greater than sign says instead of print the output of yes on the screen, printer inside a file. Try that. Then it doesn't take up the screen. It's running. The process is in the foreground. I can't do anything. I can't issue commands. You can try and type but I can't run anything. It's running. We can suspend it. Control Z, it's stopped. Just check the file. How big is it? That's one of the options for LS. If you want human friendly, H, human friendly gives the prefixes. It's 2.1 gigabytes mine. Note that what yes does is just prints wise to a file and does as fast as possible. So it filled up two gigabytes in those 10 or 20 seconds when I ran it. Note that it's not getting any bigger. That process is suspended. It's not running. There's some state information about it but it's not running. If I want to start it again, bring it back to the foreground. Just check the jobs. There's only one. I can do FG1 to bring job number one back to the foreground. And it's running again. I'll suspend it again and look at the file size. Mine's now up to 2.6 gigabytes. So that just illustrates that something was added to the file there. It started again. It's suspended so it's not doing anything at this stage. We say it's stopped. What we can do is take that process instead of having it paused, have it run. But have it run in the background, we say. Have it run such that it's executing but we can still interact with the terminal. And to put a process into the background, we use BG. BG followed by the process number or simply BG because there's only one job to deal with. BG puts that process into the background. Let's check the jobs. So now that job, yes, redirect to the out.txt file is running. And let's check our file size. Mine's 4.7 gigabytes. If I check again, it's now 5.2 gigabytes. So just to illustrate that it is running now but I can still interact with the terminal because that process we say is in the background. And we can have as many processes as we like running in the background. How do I stop it? I'm going to fill up the hard disk soon. So how do I stop my yes process? We could bring it back to the foreground. It's in the background, running. To bring it back to the foreground, simply FG or FG followed by the number. There's only one, so FG is fine. It's running again and now it's in the foreground and I can interact with it by the terminal. The commands I issue affect that process so I can control C to kill the process. Look at the file size. There's no jobs running now and that process is stopped. The file's not getting any better. It's gone. So really three states of our process. The process is in the foreground. It's running and in the foreground means that when we interact with it by the terminal, when we issue commands, it's interacting with that process in the foreground. The process can be paused or suspended with Control Z. Means it's not running but there's still some, the operating system stores some information about the process so we can resume if we like. And the third state is the process is in the background. It is running but we cannot interact with it by the terminal. It's running in the background, we say. Let's delete that file so I don't fill up my hard disk and I suggest you do the same. Let's do similar again but we'll introduce different ways to do that. Yes, redirect to our file. It's running. Control Z just pause it, BG puts it into the background so it's running again. It's a gigabyte, it's 1.5 gigabytes, it's two gigabytes. So it's running there in the background. So that's common. If you start a process normally and then you realize let's put it in the background. Let's have a running but in the background then a quick way to do that. Control Z suspends and then BG puts it into the background. So it's still running. You want it back, foreground and it's back. Back to the background, Control Z, BG. So we usually combine those two, Control Z followed by BG puts it into the background. It's getting big, the file. We said to stop the process to kill it or cancel, we use Control C but there's another way. So we see the jobs that yes, program is running. We can also see the processes, PS, lists the processes. A little bit more detail. It's the processes that you're running in this terminal and there are three listed. So this program PS runs a process that tells us what processes are running. Which processes did it list? It listed itself, that is when it runs it records that it itself is a process running, PS. Yes is a process that our operating system is running, the program that we've got running. And bash, bash is the software that interprets your commands on the command line. That's the shell software, we call it. The born again shell, it handles the commands that we can issue. So you always see bash there at the output. You almost always see PS and you may see other processes that you've got running in the background like yes in this case. It tells us something about how long it's running and more importantly or useful for now is the process ID. The operating system assigns a process ID. We know yes as the process ID 5575. How big is my file? Only 19 gig, so we better stop it. Rather than bring it back to the foreground we can kill the process without Control C but using kill. Followed by the process ID. So that's why we use PS there to learn the process ID and now kill 5575. Make sure you choose the correct process ID, yours will be different than mine. PS shows that yes is not there. We also got a nice information saying that yes has been terminated. Sorry I did a mistake there. What do I do when I get a mistake? What's happened here, it's waiting for more input. I hit the wrong key, I hit the backslash key. Control C will get me out of that. What I want is this. So another way to kill the process is if you know the process ID issue the kill command. Control C is the keyboard shortcut. The kill command will do it based on process ID. Let me delete the file. So different ways to interact with processes. Try them, they're useful. Let's say I want to run my yes command and I know I want to run it in the background. I don't want to have it in the foreground. I don't want to have to suspend and type BG. If you want to run a process immediately in the background then add the ampersand character at the end. This means run the process and immediately put it into the background. So we don't have to control ZBG, just add the ampersand character. And it's running in the background already. Jobs, this it's running, PS shows it's running, process ID 5606. The file is 2GB, 2.5GB and so on. So it's getting larger. The process is running and we run it in the background using the ampersand character. And you can do this for any command. We're using yes as a simple demo. Since I know the process ID, I can kill that. Now when we run kill, it tries to what we say gracefully shut down the process. Tries to end it normally and it's terminated. Let's delete the file just to clean up. So kill tries to stop the process like if think of say in Windows you try to close a program and it will try and save the file before it exits. That's what kill does, a graceful shut down. But sometimes we can't do that. Maybe something's not responding. So maybe in Windows you try and close a program and it says it's not responding. Do you want to force a shut down or stopping? So we can do that with kill if we run our process again in the background. Process 5618. If something will not stop using kill, then you can try even harder using minus nine. That really means even if you can't save any information, just stop the process for sure. So this is just used if something's not responding. Even with a normal kill that's not responding, kill minus nine means really try hard to kill it. And it usually works. So that's useful if something's hung. So that killed the process, slightly different than terminating, slightly worse than terminating a process. So we can interact. We saw PS list the processes and only by default shows the processes in my, this terminal running by me, bash and PS, so there's not much interesting there. If you want to see more, because our operating system is running processes for different users, if you want to see them all, PS minus E will show lots of processes and I'll pipe it into less. Remember the vertical bar means take the output of PS minus E and send that into the program less and less just shows me the output page by page. So it shows me all the processes running on my operating system. Starting with process ID one, the initialization process that starts everything. And many operating system processes for starting networking, all the different tasks. And we scroll down, we may see some we recognize there's a MySQL server running, Apache web servers running, there's processes for that. If we scroll down, we get to the, we may see Firefox running, secure shell server is running on my computer, because all of you have logged into my computer. So there's a process handling the login. And to view the demo, we're using software called T-Mark. So that's running on my computer, yours will be different. And if we get to the bottom, all right, PS and less are running. So PS minus E shows all processes, quit that, or if you want even more details, PS minus F uppercase A, there are other ways as well to show. PS has many options. This shows all the processes, but it also gives things like who is the user that's running that process? The user ID, the student user is running these processes. The root user is running, the instructor user on my computer is running some processes. Because I'm logged in as the instructor, the root user is running some processes, the core operating system. PS shows us process information. One last way to see process information, we can see a real time feedback is to use top to show the top processes. Try top. This gives us every one second an updated list of the processes. Plus the top, the first five lines show us some summary statistics about the processes on our operating system, those that are running, or the total number of processes. There's one running and there's many sleeping. Something about the CPU utilization, the amount of memory. We have 16 gigabytes. How much is used? The amount of swap space and so on. And then the list of processes. And usually it's, well by default, it's sorted by the amount of CPU they're using. Yours will be slightly different from mine because I'm running some software in the background. I'm recording the video in audio and the process, sorry it doesn't show here, but on yours you'll see on the right side the process name, mine's running some recording software. So there are many options with top. You can change which items you sort by, but we'll not go through them. If you want to see the processes interactively, use top. In Windows, this is like, what? In Windows, the task manager. You can bring up the task manager and see a similar output. Queue to quit. So I'm just showing you how to find this information. So that's the main commands we use to interact with processes. There are others, but that's the most common ones. We can do this also with processes which start graphical programs. So we're doing everything on the command line. But from the command line, we can start GUI-based programs. We know that we have Firefox installed, the PDF viewer. So we don't have Acrobat. It's called evince. I know that evince is a program that opens PDFs. So they have two programs that open up a separate graphical window. We can interact with them as processes as well. To demonstrate this, I need to switch to another window where I'm logged in as instructor on my computer. So let's do that. I'll just close my instance of Firefox and then we'll start it on the command line. And to start Firefox, we type the command Firefox and opens up the web browser. And note that that process is in the foreground. From our terminal's perspective, we can no longer issue commands. Firefox is the process running in the foreground. I can't do other things while Firefox is running. So that process is in the foreground. But I would like to do other things. I'd like to have both windows open. Well, we can close it. We can interact with it. Same with any other process. So control C kills Firefox. We can run it in the background by adding ampersand. And there was a bit of an error or a warning in my case. But it is running just to show I can do LS now. So I can now run commands on the terminal and at the same time use Firefox. So this is useful when we have graphical programs started from the terminal. We start them in the background so that we actually have both of them running. We can bring it back to the foreground. And Firefox is still running. But I can't interact with it in the shell, in the terminal. I can suspend it, control Z. I can now run commands. Can I use Firefox? The process is there, but it's suspended or paused. And the window manager has shown that it's grayed out because I can't do anything on Firefox now because it's not running. It's paused. So I can't do anything. I'd have to put it into the background if I want to use it at the same time. Now I can do something. So remember the foreground means that the process is running and it's got control of the terminal. Paused or suspended means it's not running, but there's some state information stored about it so we can resume later. Background means it's running, but we don't have control of it via the terminal. We can run other commands. Firefox is running. It's also started another process, maybe some plug-in for plug-ins on Firefox. If we kill the Firefox process ID, it kills Firefox. It produces some error, but let's ignore that. It kills its children as well. So when we killed Firefox process, the children process of Firefox were also killed, so we lost that plug-in container as well. So we can interact with processes using control C, control Z. There are some other ones as well, FG, BG, foreground and background, jobs, PS, and the ampersand character to start a process in the background.