[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Osaka/Yokohama/Tokushima] Looking for infrastructure/server side engineers!

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Deployed by over 500 companies] AWS construction, operation, maintenance, and monitoring services

[Successor to CentOS] AlmaLinux OS server construction/migration service

[Successor to CentOS] AlmaLinux OS server construction/migration service

[For WordPress only] Cloud server “Web Speed”

[For WordPress only] Cloud server “Web Speed”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Cheap] Website security automatic diagnosis “Quick Scanner”

[Reservation system development] EDISONE customization development service

[Reservation system development] EDISONE customization development service

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Registration of 100 URLs is 0 yen] Website monitoring service “Appmill”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[Compatible with over 200 countries] Global eSIM “Beyond SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[If you are traveling, business trip, or stationed in China] Chinese SIM service “Choco SIM”

[Global exclusive service] Beyond's MSP in North America and China

[Global exclusive service] Beyond's MSP in North America and China

[YouTube] Beyond official channel “Biyomaru Channel”

[YouTube] Beyond official channel “Biyomaru Channel”

"Nice" is actually a command.

 

I'm inusuki from the System Solutions Department, and I'm an omnivorous nerd who loves games and anime.

This time, I will write a blog about the "nice" command used in UNIX-based OS (Linux, etc.).

About the nice command

The nice command a command that changes the relative priority of a process .

There is a similar command called "renice", but this one changes what is being executed (the format is also slightly different)

*This time, to avoid confusion, I have deliberately not included it.

Priority & option summary

About priorities

The priority (nice value) adjusted with a value from -20 to 19 ,

The more negative it goes, the higher the priority , and the more positive it goes, the lower the priority (why...)

Note that are required to increase the priority

priority value remarks
high -20 Modifiable only by root user
0 Initial value
low 19 Can be changed by root user or general user

About options

Here are nice command options

option explanation
-【value】 Increase or decrease the priority by a specified value
-n [value] Increase or decrease the priority by a specified value
(*If no value is specified,
10 will be applied by default)
--adjustment=[value]
--help Show help
--version Show version

…yes! There are various things, but basically you should be able to remember -n [value]

(* Please note that if you do not specify it explicitly, the default value (+10) will be specified.

 

For example, run a script called beyond.sh with priority -5, the format is as follows.

# nice -n -5 /opt/beyond.sh

If you compare it with other processes, you can see that it is being executed with a priority (NI) of -5.

 
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2818 root 15 -5 13004 1416 1208 S 0.0 0.3 0:00.05 /bin/bash /opt/beyond.sh

How to check the nice value

Confirmation method (example)

You can check the relative priority (NI) of currently running processes ps -l or top -c

ps -l

# ps -l FS UID PID PPID C PRI NI ADDR SZ WCHAN TTY TIME CMD 4 S 0 2526 2503 0 80 0 - 35262 poll_s pts/0 00:00:00 sudo 4 S 0 2528 2526 0 80 0 - 22938 do_wai pts/ 0 00:00:00 su 4 S 0 2529 2528 0 80 0 - 3946 do_wai pts/0 00:00:00 bash 0 R 0 3315 2529 0 80 0 - 13267 - pts/0 00:00:00 ps

top -c

#top -c PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 571 root 20 0 474024 17456 6156 S 0.0 3.5 0:05.50 /usr/bin/python2 -Es /usr/sbin/tuned -l -P 341 polkitd 20 0 613016 9184 5076 S 0.0 1.8 0:00.20 /usr/lib/polkit-1/polkitd --no-debug 1474 root 20 0 447996 8968 6708 S 0.0 1.8 0:01.16 /usr/sbin/NetworkManager --no- daemon 1 root 20 0 127992 6612 4128 S 0.0 1.3 0:03.88 /usr/lib/systemd/systemd --switched-root --system --deserialize 21 1499 root 20 0 102904 5512 3456 S 0.0 1.1 0:00.02 /sbin/ dhclient -d -q -sf /usr/libexec/nm-dhcp-helper -pf /var/run/dhclient-eth0.pid -lf /var/lib/NetworkManager/dhclient-+ 2818 root 15 -5 13004 1416 1208 S 0.0 0.3 0:00.05 /bin/bash /opt/beyond.sh

Benefits of changing

By explicitly specifying the priority, you can allocate CPU resources to other processes with higher priority.

In other words, the load on the CPU can be relatively reduced (apparently)!

 

...Hmm, I don't really understand even if you say that.

From a human perspective, I think it's either you're assigned an important job while you're busy , or you're assigned a job that gives you a little more leeway

If you're given a job like the former, it's hard to handle, and it's hard to rearrange your schedule.

I think if you are assigned tasks with a set priority, you can handle them more smoothly even if you are doing other tasks at the same time

The same goes for the CPU (I hope I can convey that)

When do you use it?

Now, when should we use this nice command?

I have picked up some useful items!

  • When exporting large amounts of data using mysqldump
  • When greping from a large amount of logs using regular expressions for post-mortem investigation
  • When performing heavy batch processing

And so on...

It can be used when you want to pass non-urgent and heavy processing without pulling the processing of other processes

 

Which part is nice?

Well, I've done a lot of research on the nice command so far, but...

What's so nice about this nice command...?

(I'm curious!)

I'm sure there are some people who like this, so I'll leave it here as a column.

I looked into the origin of the nice command!

When I looked into it, there were various theories,

How kind of you to lower your priority for the sake of another user!

" It's very kind of you to take care of the CPU. "

It seems to be a command that expresses kindness or praise for the user

(That's why the value becomes positive when the priority is lowered...I didn't know that.)

summary

A brief summary of the contents of this article is as follows.

  • The basic format is nice -n [value]
  • You can change the priority and execute to reduce the load on the CPU.
  • If it is negative, the priority is high, and if it is positive, the priority is low (nice value: -20~19)
  • Root privileges are required to raise the priority

lastly

Thank you for reading this humble blog to the end.

I will continue to post, albeit irregularly, so please check back.

Please excuse me then.

 

If you found this article helpful , please give it a like!
4
Loading...
4 votes, average: 1.00 / 14
7,057
X facebook Hatena Bookmark pocket
[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[2025.6.30 Amazon Linux 2 support ended] Amazon Linux server migration solution

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

[Osaka/Yokohama] Actively recruiting infrastructure engineers and server side engineers!

The person who wrote this article

About the author

inusuki

Joined Beyond as a new graduate in April 2021.
Maybe it's because I have a problem with my output, but sometimes I say strange things.