Linux Essentials - The ps Command

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello and welcome back to my linux essentials series in today's video we're going to check out the ps command the ps command allows us to see what's actually running on our server the processes that are running on our server so that way we can see what's taking up memory what's running things like that it's a very awesome command to learn it's an essential command to learn so i guess it's a good fit for my linux essentials series and we're going to take a look at it right now thankfully the ps command is actually really simple we're going to be adding additional options to the command but for right now let's run ps by itself and see what it looks like when we give it no options at all in the output we have several columns we have pid tty time and cmd short for command but what exactly does this mean and what are these processes right here first of all for the process id that's what pid stands for we have a process id of 2925 for this process right here so each line is its own process this is process 2925 and the command is bash then we have another line right here we have a process id of 12 968 so as you can see the process id is unique between the two and that's not a coincidence each process on a linux system is going to have its own unique pid because no two processes can have the same pid when a process runs it's assigned a pid and that's the pid that it has and a linux system is intelligent enough to know how to not assign the same process id to more than one process now the tty field right here that actually refers to the terminal that the process is running inside of going over ttys is beyond the scope of today's video maybe that's something i'll cover in the future but for right now it just helps us understand which terminal a process is running inside of now time is a bit confusing here because time is all zeros for each of these two processes so does that mean that each of these processes were running for exactly zero seconds well no actually that's not what it means time refers to cpu time how much time the process has been utilizing the cpu so that's not something that's ultimately useful to us right now so we're going to ignore that but just keep in mind that time in this context is not related to how much time the process has been running but just how much time the process has been seeking the attention of the cpu so that's what it means and command cmd right here shows us the actual command that is running as part of that process so here we have bash and that's the shell that i'm running here in my terminal but we also have a process for ps itself so when i ran ps it was assigned a process id and a showing in the list now ps when you run it just shows you output and then it closes so ps isn't still running but when it did run it was given a process id even though it finishes and doesn't have to do anything else after it shows output so that was the ps command by itself and by the way i almost forgot to mention when i run ps by itself with no options it's only going to show the processes that are running as part of this particular terminal session so as you can see i don't really have a whole lot going on here but at its default that's what ps does it just shows you the processes that are running in your current session so that means you'll actually get different output between different terminals depending on what you're running inside those terminals now before we go into the options that are associated with the ps command i want to make sure that you guys are aware of the fact that there's different styles of options with a ps command and it might be a little confusing for newcomers so i'm going to keep this as simple as i can first we have unix style options and one example of this is ps dash aux unix style options include a dash in front of the options and then you could chain other options together so these are actually three different options right here a u and x but instead of typing a dash right in front of each option we just combine the three options together and add one single dash now another style is the bsd style which is also recognized by the ps command and the difference is simple with bsd style there's no dash now the output from these various options might show differently depending on the distribution you're running but the only point right now to keep in mind is that there's different styles of options bsd style and unix style so for the next variation i'll just run ps and what i'm going to do is show you options in the gnu style which is the third style and that looks something like this with the canoe style we have two dashes in front of the option instead of just one and instead of the option being just one character it's kind of like a word so yeah it takes up more space but it might give you a better indication as far as what it's actually doing and i just added a process id of this one right here this bash process because i wanted to use a process id that actually exists on the system so i'll press enter and all it did was just narrow down the list to that one process right there nothing too useful about that but i wanted to show you guys an example of the gnu option style which i've just done now we're going to stick to the bsd and unix styles for the most part but i at least wanted you guys to be aware of the different styles and when you get more advanced in linux you'll understand when to use one style versus another but anyway what i'm going to do right now is just carry on the tutorial and show you some other examples of the ps command now like i mentioned if you run ps by itself it just gives you a list of processes that are running in that particular terminal session but what should you do if you want to see all processes that you're running not just the processes that are in that particular terminal window but system wide for that we can run ps and then x just like that and as you can see there's quite a few more things being shown here so we have firefox running for example gnome terminal it's basically showing every process that is running as my user now sometimes we have a question mark here on this side right here and then here we have pts this actually right here is going to be a column that represents the tty or the terminal if a process is started as part of a specific terminal it's going to show that terminal here but if the process was started by the system it's going to show a question mark instead now within the output here we have a new column this time and that column is called stat now there's a high number of possibilities that could be underneath stat and i won't go over all of them in this video but one thing that you'll see is a lowercase s and if you see that that means that it's actually a process later or a root process because sometimes there's a process that starts another process creating kind of like a process tree so a lowercase s means that it's actually a process leader if there's a capital s that means that the process is running in uninterruptable sleep which essentially means that it's waiting for some sort of user input and can't be disturbed until it receives that if we see a capital r that means it's actively running so we see any r's in here not sure if i will but we'll check here's one so again if we see a capital r that means that the process is actively running so the process is not in uninterruptable sleep it's actually running if we see a capital t that means the process is stopped and that's similar to when you press control and z to background to process so i don't see any values so far that are capital t unless i missed something i'll just double check so far i'm not seeing that but if you do see a capital t that means the process has stopped and that typically happens when you background a process for example holding control and pressing z now let's go ahead and take a look at another variation of the ps command this time i'm going to type ps and then dash capital h and then lowercase e let's see what happens now what's pretty cool about this is that we have a sort of process hierarchy showing here because you see we have some commands that are aligned all the way to the left and some that are tabbed over so that helps us see the process relationship because sometimes you'll have a process that spins off another process so it's a lot easier to see which one is the parent process i think that's really cool now as far as the options are concerned the lowercase e option tells ps to list all the processes that are running on the system regardless of the controlling terminal the capital h option formats the command column to display the parent child relationship between the processes and that's why we see the output displayed as it is here however i can make that look a little bit better so what i'm going to do is show you another variation so this time i'm going to type ps ax jf let's see what happens and the output is a bit large so what i'm going to do is shrink the font size a bit let's try that again so at this point it should be easier to see the process relationship because this is going to show the process relationship like before but it's going to show these characters which actually help us understand the relationship because the processes are actually going to have lines that point to their parent process and that makes it even easier to understand probably even better if you don't have a ginormous font like i do but it is what it is you should at least be able to see the value of this particular variation so let's take a look at the different fields that we have with this variation so i'll scroll up so we actually have several additional fields here we have ppid we didn't have that before pgid we didn't have that either so we have some new fields here what do these actually mean the ppid column refers to the parent process id so it makes it really easy to find the process id of the process that spawned that process so not only do we see lines on the right hand side but we also have the pid of the parent listed as well which is definitely useful the sid column refers to the session id and this is usually the same as the process that started the chain pgid is a column that refers to the parent group process id and tpg id this column refers to the terminal session id with which the process is associated if there isn't a terminal that's associated with the process then a negative one will be displayed here instead and we can see various negative ones already there in that column finally we have the uid that's the user id that started that particular process uid 0 is going to be root that's the most powerful user but if i scroll down we have some other uids here and myuid is uid 1000 so anytime you see something running as uid 1000 that's most likely your user account unless you have other user accounts on your system that's going to be at least the first non-system user that's been created and since my user is the only non-system user on this particular computer then 1000 refers to me in particular so at this point let's take a look at a variation of the ps command that is arguably the most popular variation or at least the one that i find the most useful and that is ps aux just like that i'll lower the font size so i'm going to scroll up because what i want to do is show you guys the columns that are associated with this variation and we have that right here at the top now i think one of the coolest things about this variation is that it replaces the uid column with the user column which we see right here and that's easier for us to understand because we're not going to memorize the uids of all the users on the system i mean you can but there's really no value in doing that at all whatsoever so with this variation of the ps command we are actually seeing the user for each of these processes and it's the actual username which is easier for us humans to remember so we know who root is that's the most powerful user on the system and if i scroll down you'll start to see other names here including my own now scrolling back up we can also see the cpu percent and the memory percent that's being used by that process so if the system is running slow for example we could see which of the processes are actually using the most cpu and the most memory and that's certainly helpful if we want to find out which process is making the system slow down maybe a process is going out of control or something like that if that's the case maybe we'll see a really high number here it's definitely a very easy way to check that now we also see the start time right here for when that particular process was started so this laptop has been running since yesterday i rebooted yesterday so as you can see we have some processes that started october the 27th and the reason why this is useful is because if you have a process that's supposed to be running all the time and maybe your server has been on for 30 days straight but the process hasn't actually been running for 30 days well something must have happened because why would a process that's supposed to be running all the time show a date that's different than the time the server was booted typically what that means is that the process may be restarted or something like that and a process isn't going to restart unless you yourself restart it or maybe there's a problem and the process had to respond if that's the case it might be worth looking into it and finding out why that happened maybe the system ran out of memory and had to restart the process there's all kinds of reasons why this might happen but i think it's a very useful troubleshooting tactic to look at the time a process was started because at the time isn't what you were expecting that might actually allude you to the fact that there was some sort of problem and it had to restart so there you go this was a simpler tutorial but a necessary one i want to make sure you guys have the basics of the ps command and there's always going to be more that we can go over in the future but for now i think the basics are going to serve you quite well in knowing what's running on your server especially if the only takeaway you have here is ps aux that's the most important definitely commit that to memory either way thank you guys so much for watching i really appreciate it and i'll see you again next time [Music] my [Music] you
Info
Channel: LearnLinuxTV
Views: 15,205
Rating: undefined out of 5
Keywords: Linux, Tutorial, Howto, Guide, Distribution, Learn Linux, open-source, open source, gnu/linux, LearnLinuxTV, ps, ps command, ps aux, ps -aux, processes, linux tutorial, linux commands, linux tutorial for beginners, linux command line tutorial, linux command line, linux basic commands, linux terminal, linux for beginners, linux essentials, command line, linux commands for beginners, linux (operating system), gnu/linux (operating system), unix commands, ps tutorial, linux tutorial 2021
Id: wYwGNgsfN3I
Channel Id: undefined
Length: 15min 18sec (918 seconds)
Published: Wed Dec 01 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.