Linux环境下进程发生异常而挂掉,通常很难查找原因,但是一般Linux内核给我们提供的核心文件,记录了进程在崩溃时候的信息,在C语言类的大型项目中,有助于深入定位。其配置流程如下:
1 查看生成core文件开关是否开启 ulimit -a
c
core file size (blocks, -c) 0 // 表明未开启
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 2061196
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 1048576
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 1048576
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
2 ulimit -c [kbytes]可以设置系统允许生成的core文件大小;
c
ulimit -c unlimited // 不限制core文件的大小
core file size (blocks, -c) unlimited
data seg size (kbytes, -d) unlimited
scheduling priority (-e) 0
file size (blocks, -f) unlimited
pending signals (-i) 2061406
max locked memory (kbytes, -l) 64
max memory size (kbytes, -m) unlimited
open files (-n) 524288
pipe size (512 bytes, -p) 8
POSIX message queues (bytes, -q) 819200
real-time priority (-r) 0
stack size (kbytes, -s) 8192
cpu time (seconds, -t) unlimited
max user processes (-u) 131072
virtual memory (kbytes, -v) unlimited
file locks (-x) unlimited
上述方式不是永久生效的,要使其永久生效,可以进行如下设置
1 vi /etc/security/limits.d/20-nproc.conf
c
* soft nproc unlimited
root soft nproc unlimited
postgres soft core unlimited
postgres hard core unlimited
2 指定core文件的名字与生成路径
执行# vim /etc/sysctl.conf,进入编辑模式,加入下面两行
c
kernel.core_pattern=/var/core/core_%e_%p
kernel.core_uses_pid=0
注释: %e dump文件名 %p 进程号
3 执行sysctl -p /etc/sysctl.conf,使其修生效。
参考:https://blog.csdn.net/luronggui/article/details/128728235