L61.linux命令每日一练 -- 第九章 Linux进程管理命令 -- renice和nohup

9.9 renice:调整运行中的进程的优先级

9.9.1 命令详解

【命令星级】 ★★★☆☆

【功能说明】

​ nice命令常用于修改未运行的程序运行时的优先级,但是对于正在运行的进程,若想要修改其优先级,就需要用到renice命令。

【语法格式】

renice [option]
renice [选项]

​ **说明:**在renice命令及后面的选项里,每个元素之间都至少要有一个空格。

​ 【选项说明】

​ 表9-11针对该命令的参数选项进行了说明。

​ 表9-11 renice命令的参数选项及说明
在这里插入图片描述

9.9.2 使用范例

​ **范例9-32:**修改指定进程号的优先级。

[root@centos7 ~]# ps -l		#使用9.8.3节知识点的命令结果。
F S   UID    PID   PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0   2146   2140  0  80   0 - 29150 do_wai pts/0    00:00:00 bash
0 T     0  10283   2146  0  80   0 - 36798 do_sig pts/0    00:00:00 vim
4 T     0  10375   2146  0  70 -10 - 36798 do_sig pts/0    00:00:00 vim		#vim进程的PRI为70,NI为-10。
0 R     0  10658   2146  0  80   0 - 38337 -      pts/0    00:00:00 ps
[root@centos7 ~]# renice -n 5 -p 10375	#使用renice的-p参数指定值为10375的进程,将其NI值调整为5。
10375 (process ID) old priority -10, new priority 5
[root@centos7 ~]# ps -l
F S   UID    PID   PPID  C PRI  NI ADDR SZ WCHAN  TTY          TIME CMD
4 S     0   2146   2140  0  80   0 - 29150 do_wai pts/0    00:00:00 bash
0 T     0  10283   2146  0  80   0 - 36798 do_sig pts/0    00:00:00 vim
4 T     0  10375   2146  0  85   5 - 36798 do_sig pts/0    00:00:00 vim		#vim进程的PRI为85,NI为5。
0 R     0  10700   2146  0  80   0 - 38337 -      pts/0    00:00:00 ps

​ 通过测试可以发现,PRI值并不是在上一次的基础上进行变化的,而是一直在初始默认值80这个值之上变动。

9.10 nohup:用户退出系统进程继续工作

9.10.1 命令详解

【命令星级】 ★★★★☆

【功能说明】

​ nohup命令可以将程序以忽略挂起信号的方式运行起来,被运行程序的输出信息将不会显示到终端。

​ 无论是否将nohup命令的输出重定向到终端,输出都将写入到当前目录的nohup.out文件中。如果当前目录的nohup.out文件不可写,则输出重定向到$HOME/nohup.out文件中。

【语法格式】

nohup [option]
nohup [选项]

​ **说明:**在nohup命令及后面的选项里,每个元素之间都至少要有一个空格。

9.10.2 使用范例

​ **范例9-33:**让执行的命令在当前会话终止后继续保持运行。

​ 正常情况下,如果用户退出登录或会话终止,则用户正在执行并可持续一段时间的命令(非守护进程)将自动终止。使用nohup命令可以实现在用户退出或当前会话终止后继续保持运行,具体的例子如下:

[root@centos7 ~]# nohup ping www.baidu.com
nohup: ignoring input and appending output to ‘nohup.out’
#当前终端已经hang住,此时强制关闭当前终端(例如关闭该标签或者SSH客户端工具,这个ping命令依然会在后台运行。

​ 这里我们先关闭Xshell工具对应的标签,然后重新进入新的终端进行查看,会发现命令还在运行。

[root@centos7 ~]# ps -ef |grep ping
root      11283      1  0 19:50 ?        00:00:00 ping www.baidu.com
root      11424  11396  0 19:53 pts/0    00:00:00 grep --color=autoping
[root@centos7 ~]# tail -f nohup.out		#查看重定向文件的内容。
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=151 ttl=128 time=38.4 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=152 ttl=128 time=40.3 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=153 ttl=128 time=34.9 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=154 ttl=128 time=34.8 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=155 ttl=128 time=34.8 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=156 ttl=128 time=34.8 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=157 ttl=128 time=34.8 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=158 ttl=128 time=35.0 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=159 ttl=128 time=34.8 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=160 ttl=128 time=41.1 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=161 ttl=128 time=34.9 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=162 ttl=128 time=34.9 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=163 ttl=128 time=35.2 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=164 ttl=128 time=34.9 ms
64 bytes from 14.215.177.39 (14.215.177.39): icmp_seq=165 ttl=128 time=35.0 ms
^C
[root@centos7 ~]# kill 11283	#终止这个命令。

​ 在工作中我们一般会配合&符运行nohup命令,让程序直接在后台运行:

[root@centos7 ~]# nohup ping www.baidu.com &
[1] 11545
[root@centos7 ~]# nohup: ignoring input and appending output to ‘nohup.out’
[root@centos7 ~]# tail -f nohup.out
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=22 ttl=128 time=35.6 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=23 ttl=128 time=35.4 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=24 ttl=128 time=35.5 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=25 ttl=128 time=38.6 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=26 ttl=128 time=38.7 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=27 ttl=128 time=35.2 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=28 ttl=128 time=35.2 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=29 ttl=128 time=35.4 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=30 ttl=128 time=35.8 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=31 ttl=128 time=35.3 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=32 ttl=128 time=35.3 ms
64 bytes from 14.215.177.38 (14.215.177.38): icmp_seq=33 ttl=128 time=35.3 ms

​ **提示:**类似功能的命令还有screen和直接使用&符。

猜你喜欢

转载自blog.csdn.net/qq_25599925/article/details/126202555
今日推荐