Managing processes in Linux is a fundamental concept that every good system administrator should be familiar with. A majority of the work you do with these processes consists of basic, repetitive tasks such as starting and stopping processes, searching for them, killing them, etc.
In some less frequent cases, you may need to re-order your processes due to system resource allocation. When these situations arise, many of us will take to our favorite search engine to figure out the most efficient way to do this. Now that the search algorithm has brought you here to Enable Sysadmin, let's serve up the answers you are looking for.
I will break down how priority is determined and then show you how to manipulate those values below.
Process priority and relativity
Linux systems routinely run more processes than there are processing units in the computer. Because of this, the process scheduler will quickly jump between processes on a single core, thus creating the illusion that we are running multiple, simultaneous processes.
In reality, each process is assigned a process scheduling policy. Within this policy, there is a 40 point scale used to measure the nice levels of a process. The nice level is a unit of measure that describes the relative priority of the process. When discussing niceness levels, note that the scale is -20 (highest priority) to 19 (lowest priority), and that a process inherits its nice level from the parent (most often 0).
[ You might also enjoy: Linux Command Basics: 7 commands for process management ]
Now, if a process has a lower niceness level (sub-zero), then it will be less likely to give up its CPU usage (not very nice, is it?). On the flip side, a process with a high niceness value (exceeding zero) will be more likely to give up CPU usage.
An easy way to remember this is if someone (or a process) is really nice, they are more inclined to share with others. If they are not very nice, they tend to be more protective over what belongs to them (CPU resources, in this case).
Also, if there is no shortage of resources, even processes with high niceness levels will use all available CPU time. The only time a process will yield its resources is when there is a bandwidth shortage.
Priority report
Now that we have all of the context surrounding process priority, let's look at some of the ways to view this information at the terminal.
The quickest, most basic way to view niceness information is to use the top command. It displays this information by default in the fourth column (left to right) and therefore doesn't require any additional options or flags.
The other and slightly more involved method is to use the ps command with a generous helping of formatting options. The following command will display processes with their process IDs, name, niceness level, and scheduling class (sorted descending):
[tcarrigan@localhost ~]$ ps axo pid,comm,nice,cls --sort=-nice
Now in real time:
Manipulating existing niceness
We now know how to view an existing process's niceness, but what if that nice level doesn't suit our needs? How can we change those values to get the most out of our system? Well, if you have a particular process that is eating up resources that you need elsewhere, you can lower the nice level of that specific process. To do this, we will use the renice command.
Note: only the root user may reduce a niceness level (increase priority). Unprivileged users may increase nice levels but cannot decrease them without root permissions.
Take a look at this sleep 500 process. It has a PID of 23990 and a default nice level of 0.
[tcarrigan@localhost ~]$ ps -o pid,comm,nice 23990
PID COMMAND NI
23990 sleep 0
If I wanted to make that process less of a priority, I would use the following renice command:
To make the process more important (lowering the nice level), you would use the same command syntax as a privileged user.
[ Free cheat sheet: Kubernetes glossary ]
One last thing
Okay, before we wrap up, there is one more trick I wanted to include here. If you are going to start a process that usually has a default nice level that is undesirable, you can actually set that parameter as you spawn the process. We will use the sleep command again. However, this time, we will create it with the nice command (which defaults the process to nice level 10).
You can set a specific nice level by using the same command with the following syntax:
[tcarrigan@localhost ~]$ nice -n 19 sleep 500 &
The above command sets the background sleep 500 to a nice level of 19.
These commands were a ton of fun to learn and show off. In the future, when I run into issues with system resources or hungry processes, I can modify the resources allocated to them.
저자 소개
Tyler is the Sr. Community Manager at Enable Sysadmin, a submarine veteran, and an all-round tech enthusiast! He was first introduced to Red Hat in 2012 by way of a Red Hat Enterprise Linux-based combat system inside the USS Georgia Missile Control Center. Now that he has surfaced, he lives with his wife and son near Raleigh, where he worked as a data storage engineer before finding his way to the Red Hat team. He has written numerous technical documents, from military procedures to knowledgebase articles and even some training curricula. In his free time, he blends a passion for hiking, climbing, and bushcraft with video games and computer building. He is loves to read and enjoy a scotch or bourbon. Find him on Twitter or on LinkedIn.
유사한 검색 결과
Behind the scenes of RHEL 10, part 3
Alliander modernises its electricity grid with Red Hat for long-term reliability in balance with rapid innovation
The Overlooked Operating System | Compiler: Stack/Unstuck
Linux, Shadowman, And Open Source Spirit | Compiler
채널별 검색
오토메이션
기술, 팀, 인프라를 위한 IT 자동화 최신 동향
인공지능
고객이 어디서나 AI 워크로드를 실행할 수 있도록 지원하는 플랫폼 업데이트
오픈 하이브리드 클라우드
하이브리드 클라우드로 더욱 유연한 미래를 구축하는 방법을 알아보세요
보안
환경과 기술 전반에 걸쳐 리스크를 감소하는 방법에 대한 최신 정보
엣지 컴퓨팅
엣지에서의 운영을 단순화하는 플랫폼 업데이트
인프라
세계적으로 인정받은 기업용 Linux 플랫폼에 대한 최신 정보
애플리케이션
복잡한 애플리케이션에 대한 솔루션 더 보기
가상화
온프레미스와 클라우드 환경에서 워크로드를 유연하게 운영하기 위한 엔터프라이즈 가상화의 미래