"Nice" is actually a command

table of contents
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 is usedto change the relative priority of a process before executing it.
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.
higher the priority, thethe further you go into the positive, the lower the priority(why...?)
Please note thatroot privileges are required to increase the priority.
| 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
for the nice commandthe optionsHere are
| option | explanation |
| -【value】 | Increase or decrease the priority by the specified value |
| -n [value] | Specify the priority value to increase or decrease it (if no value is specified, the default value of 10 will be applied). |
| --adjustment=[value] | |
| --help | Need more help? |
| --version | Show Version |
...Yes! There are various things to remember, but basically,-n[value]if you remember
(If you don't explicitly specify a value, the default value (+10) will be usedNote:
For example,run the script beyond.sh with priority -5,the format would be as follows:
# 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)
the relative priority (NI) of currently running processes `ps -l` or `top -c` You can check
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,this can relatively reduce the load on the CPU!
...Hmm, I'm not sure what you mean
From a human perspective,whether you're assigned important tasks while being busy, ortasks that are relatively less demandingI think 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
being assigned tasks with predetermined priorities allows for smoother processing, even when juggling them with other tasksI think that
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 relatively heavy process that isn't urgent, without interfering with 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!」
"Taking care of the CPU... you're so kind."
.a command that expresses kindness or praise for the userIt appears to be
(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
5
