nproc 命令详解

| 选择喜欢的代码风格  

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

nproc 命令安装:


-bash/zsh: nproc command not found

#Debian
apt-get install coreutils

#Ubuntu
apt-get install coreutils

#Alpine
apk add coreutils

#Arch Linux
pacman -S coreutils

#Kali Linux
apt-get install coreutils

#CentOS
yum install coreutils

#Fedora
dnf install coreutils

#OS X
brew install coreutils

#Raspbian
apt-get install coreutils

#Docker
docker run cmd.cat/nproc nproc

nproc 命令语法:


nproc [OPTION]...

nproc 命令选项:


--all  print the number of installed processors

--ignore=N
      if possible, exclude N processing units

--help display this help and exit

--version
      output version information and exit

nproc 命令实例:


默认执行 nproc,显示该机器的 CPU 核数(可用 CPU 处理单元的数量):

$ nproc

8

nproc 显示已安装的处理单元的数量,包括任何不活动的处理单元:

nproc --all

如果可能,nproc 从返回值中减去给定数量的单位:

nproc --ignore count

nproc 相关命令实例:


用 ps 命令查看所有用户创建的进程数,使用命令:

ps h -Led -o user | sort | uniq -c | sort -n

      1 chrony
      1 dbus
      4 nginx
      7 polkitd
     32 mysql
    144 root

用 ps 命令查看 mysql 用户创建的进程数,使用命令:

ps -o nlwp,pid,lwp,args -u mysql | sort -n    

NLWP   PID   LWP COMMAND
  32  7371  7371 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid

好了,那么问题来了,何时需要修改这个nproc 呢?当日志出现以下情况中的一种时,需要考虑这个 nproc

1. Cannot create GC thread. Out of system resources  

2. java.lang.OutOfMemoryError: unable to create new native thread

修改 nproc 默认值:


注意2.6 版本的内核默认是在 /etc/security/limits.d/90-nproc.conf3.1 版本是 /etc/security/limits.d/20-nproc.conf 是会覆盖 /etc/security/limits.conf 里的配置:

$ cat 20-nproc.conf 


# Default limit for number of user's processes to prevent
# accidental fork bombs.
# See rhbz #432903 for reasoning.

*          soft    nproc     4096
root       soft    nproc     unlimited

在控制台执行 ulimit -n [数字] (修改这个不需要重启):

ulimit -u 655350

检测 nproc 限制修改是否生效,在控制台切到该用户下执行:

ulimit -u

nproc 命令扩展阅读:




nproc 命令评论

共收录到 491Linux 命令