"Nice" is actually a command

 

I'm Inusuki from the System Solutions Department, a jack-of-all-trades otaku who loves games and anime

This time I will write a blog about the "nice" command used in UNIX-based operating systems (such as Linux)

About the nice command

The nice command changes the relative priority of a process .

There is a similar command called "renice", but this changes what is currently running (the format is slightly different)

*We have deliberately not included it this time to avoid confusion

Priority & Option Summary

Regarding priority

The priority (nice value) adjusted from -20 to 19 .

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

To increase the priority , you need root privileges.

priority value remarks
high -20 Can only be changed by the root user
0 Initial value
low 19 Changes can be made by root user and general user

About options

Here are the nice command options

option explanation
-【value】 Increase or decrease the priority by the specified value
-n [value] Increase or decrease the priority by the specified value
(if no value is specified,
the default is 10)
--adjustment=[value]
--help Need more help?
--version Show Version

…Yes! There are a lot of things, but basically, just remember -n [value]

( Note: If you do not specify this explicitly, the default value (+10) will be used

 

For example, run a script called beyond.sh with a priority of -5, you would use the following format:

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

When compared to other processes, we 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-daemondaemon 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 reduced relatively !

 

...Hmm, I'm not sure what you mean

In human terms, I think it whether you are given important work in the midst of a busy schedule , or work that gives you a bit more time to do it

If you are given a job like the former, it will be too much for you to handle, and it will be difficult to rearrange your schedule

I think that if you are assigned work with a set priority, you will be able to handle it smoothly even while working on other tasks

The same is true for CPUs (I hope that helps)

When should you use it?

So, when would you use this nice command?

We've picked out a few uses for it!

  • Exporting large amounts of data with mysqldump
  • When grepping a large amount of logs with regular expressions for post-mortem investigation
  • When running heavy batch processing

And so on..

when you want to run a less urgent and heavy process without dragging down the processing of other processes !

 

What's nice about it?

Well, I've looked into the nice command in various ways so far, but

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

(I'm curious!)

I'm sure there are some curious people out there, so I'll leave it here as a column

I looked into the origin of the nice command!

When I looked into it, I found several theories,

How kind of you to lower your priority for other users!

" You're so kind to take care of your CPU. "

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

(I didn't know that this is why lowering the priority makes the value positive.)

summary

Here's a brief summary of what's covered today:

  • The basic format is nice -n【value】
  • You can change the priority and run it to reduce the load on the CPU
  • Negative values ​​indicate higher priority, positive values ​​indicate lower priority (nice value: -20 to 19)
  • Root privileges are required to increase priority

lastly

Thank you for reading this humble blog to the end

I will continue to post irregularly, so please keep checking back

I'll be off now

 

If you found this article useful, please click [Like]!
5
Loading...
5 votes, average: 1.00 / 15
9,786
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author

inusuki

He joined Beyond as a new graduate in April 2021.
He sometimes says strange things, perhaps because of his poor output.